[D-runtime] A cooperative suspension API
Alex Rønne Petersen
xtzgzorex at gmail.com
Mon May 7 10:18:29 PDT 2012
Forgot to mention, it depends on my critical regions patch, so that
one will have to be pulled in before I send a pull request for this
one.
Regards,
Alex
On Mon, May 7, 2012 at 7:16 PM, Alex Rønne Petersen <xtzgzorex at gmail.com> wrote:
> I have an initial version for this functionality done here:
> https://github.com/alexrp/druntime/commit/5b60e88ace41d2126c5754dab1eacb149a3895b3
>
> Suggestions for improvements welcome! (And yes, I know the sleep
> heuristic sucks; I'll fix that later.)
>
> The idea is simple:
>
> Thread.getThis().isCooperative = true;
>
> while (doWork)
> {
> /* work goes here */
>
> if (thread_shouldSuspend())
> thread_cooperativeSuspendSync(); // or
> thread_cooperativeSuspend() to just blindly continue doing work and
> let thread_suspendAll() suspend at any point
> }
>
> Regards,
> Alex
>
> On Sat, May 5, 2012 at 6:45 AM, Alex Rønne Petersen <xtzgzorex at gmail.com> wrote:
>> Hi,
>>
>> Another feature I want to implement in core.thread is cooperative
>> suspension. In this model, the thread_suspendAll() routine flips a global
>> variable that notifies all threads that they need to suspend. Now, a thread
>> that has marked itself as cooperative is then expected to regularly check
>> this variable and, when it notices that it needs to suspend, does so. This
>> means that the suspension machinery trusts cooperative threads to read this
>> global variable as fast as possible (note that races in reading the variable
>> are acceptable).
>>
>> The question is how the API should work. I have something like this in mind:
>>
>> class Thread
>> {
>> // ...
>>
>> private bool m_isCooperative;
>>
>> @property final bool isCooperative()
>> {
>> return m_isCooperative;
>> }
>>
>> @property final void isCooperative(bool value)
>> {
>> synchronized (slock) // needed because changing this value can
>> affect the entire suspension process
>> {
>> m_isCooperative = value;
>> }
>> }
>> }
>>
>> thread_suspendAll() then trivially checks Thread.m_isCooperative and makes
>> appropriate decisions (i.e. suspend all cooperative threads first and so
>> on).
>>
>> Any thoughts?
>>
>> Regards,
>> Alex
More information about the D-runtime
mailing list