[D-runtime] A cooperative suspension API

Alex Rønne Petersen xtzgzorex at gmail.com
Mon May 7 10:16:18 PDT 2012


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