[D-runtime] A cooperative suspension API

Alex Rønne Petersen xtzgzorex at gmail.com
Sat May 5 08:12:56 PDT 2012


Argh:

>     final void suspendIfNeeded()
>     {
>         if (g_shouldSuspend) // can only race against
> thread_suspendAll() setting it to false, which is ok
>             suspend(this);
>     }

was meant to say "true", not "false".

Regards,
Alex

On Sat, May 5, 2012 at 6:50 AM, Alex Rønne Petersen <xtzgzorex at gmail.com> wrote:
> Oops, didn't send the whole thing:
>
> __gshared bool g_shouldSuspend; // set by thread_suspendAll()
>
> 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;
>         }
>     }
>
>     @property static bool shouldSuspend()
>     {
>         return g_shouldSuspend;
>     }
>
>     final void suspendIfNeeded()
>     {
>         if (g_shouldSuspend) // can only race against
> thread_suspendAll() setting it to false, which is ok
>             suspend(this);
>     }
> }
>
> 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