[Mono-dev] [PATCH] New shutdown code

Rodrigo Kumpera kumpera at gmail.com
Thu Dec 13 12:01:40 EST 2007


Hey Mark,

@@ -762,7 +774,8 @@

     THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT"
(handle %p)", __func__, tid, thread_handle));

-    handle_store(thread);
+    may_start = handle_store(thread);
+    g_assert (may_start);

Why are you asserting here? Shouldn't the thread just play dead is may_start
is false?




On Dec 13, 2007 12:14 PM, Mark Probst <mark.probst at gmail.com> wrote:

> Hi Paolo, Dick, and everybody else!
>
> Here's the new shutdown code which should fix bugs #337383 and #347676.
>
> We have two shutdown paths in Mono:
>
> * Implicit exit: All threads shut down normally
> * Explicit exit: Some thread calls System.Environment.Exit()
>
> In implicit exit the main thread shuts Mono down.  In explicit exit
> whichever thread executes Exit() shuts down Mono.
>
> Problem: Two (or more) threads might attempt shutdown at the same time.
> That's what mono_threads_set_shutting_down() is for: Make sure only one
> thread gets to shut Mono down, the others just die or play dead.
>
> Before shutting down Mono all threads must quit or must be suspended.
> To avoid deadlock (two threads waiting for each other's suspension or
> death, which can happen if the main thread is finished and waits for the
> other threads and another thread wants to shut down via Exit) a thread
> about to shut Mono down must set its status to finished (so that the
> main thread will wake up from its waiting loop, realize that someone
> wants to shut down, and get out of the game), and a thread wanting to
> shut down, but failing (because another thread came first) must either
> quit or pretend, as well.
>
> This is what mono_threads_set_shutting_down() does, atomically (it
> acquires the threads lock):
>
> mono_threads_set_shutting_down (bool may_abort) {
>  if (shutting_down) {
>    // somebody else is already shutting down
>    if (may_abort)
>      call Thread.Abort() on self
>    else {
>      play dead;
>    }
>  } else {
>    shutting_down = TRUE;
>    set self status to finished, but don't actually quit;
>  }
> }
>
> We make sure (via the threads lock) that no thread can start (although
> they can still be created) after mono_threads_set_shutting_down() has
> been called for the first time.
>
> Here are the two shutdown paths:
>
> Main thread, after Main() has exited:
>
> do {
>  wait for threads to change state;
>  // some threads changed state
>  if (somebody else is exiting)
>    play dead;
> } while (!all threads finished);
> // all threads are finished now, but some threads might still start in
> // this window
> mono_threads_set_shutting_down(FALSE);
> // we're allowed to shut down, and no more threads can start now
> abort all other threads;
> shut down;
>
> Some thread (could be main thread), from Exit():
>
> mono_threads_set_shutting_down(TRUE);
> // we're allowed to shut down, and no more threads can start now
> suspend all other threads;
> shut down;
>
> Does the patch look ok?  Any cases I've missed?
>
> Mark
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071213/a27b624b/attachment.html 


More information about the Mono-devel-list mailing list