[MLton] cvs commit: critical sections during thread switch

Stephen Weeks MLton@mlton.org
Sat, 3 Apr 2004 17:53:53 -0800


I notice with this checkin that GC_switchToThread was changed to use
the slow implementation of thread switching. 

>  void GC_switchToThread (GC_state s, GC_thread t) {
>  	if (DEBUG_THREADS)
>  		fprintf (stderr, "GC_switchToThread (0x%08x)\n", (uint)t);
> -	if (FALSE) {
> +	if (TRUE) {
>  		/* This branch is slower than the else branch, especially 
>  		 * when debugging is turned on, because it does an invariant
>  		 * check on every thread switch.
> @@ -3070,6 +3088,11 @@
>  		 */
>  	 	enter (s);
>  	  	switchToThread (s, t);
> +		s->canHandle--;
> +		if (0 == s->canHandle and s->signalIsPending) {
> +			startHandler(s);
> +			switchToThread(s, s->signalHandler);
> +		}
>  	 	leave (s);

Would it be possible to add the same logic to the fast (FALSE) branch
and use that?

One annoying thing about the slow implementation is that when
compiling -debug true, which is how we normally run the regressions,
there is an invariant check on every thread switch.  This slows
prodcons down on my machine from about 5 seconds to about 10 minutes.

More seriously, though, enter and leave both make system calls and do
other unnecessary stuff, even when compiling -debug false.

The hope is that thread switching should be just a few instructions
(10 or so) so that we could imagine implementing it in the backend
someday without a C call.  In fact, maybe this is a good time to
consider doing that, since it would address the problem that atomicEnd
is implemented in two different places.