[gambit-list] Are REPL:s thread-terminate!-safe?

Marc Feeley feeley at iro.umontreal.ca
Wed May 18 11:07:44 EDT 2011

On 2011-05-18, at 10:41 AM, Marc Feeley wrote:

> As far as I can tell it is safe to terminate a thread T in a REPL with thread-terminate! .  The thread T will have locked the  REPL channel owner mutex (to acquire ownership of the REPL channel), so when T is terminated it will set the mutex to the "abandoned" state, so the next thread competing for the REPL channel will lock the mutex and raise the exception "abandoned mutex" which will start a nested REPL.  I think it all works out, and the tests I have done (with the latest 4.6.1) seem to confirm that.
> Perhaps it would be more graceful to ignore "abandoned mutex" exceptions in this case.  I'll have to think about the implications.

Here's a test:

% gsi -:dar
Gambit v4.6.1

> (thread-start! (make-thread (lambda () (thread-sleep! 1) (+ 1 xxx))))
#<thread #2>
> (begin (thread-sleep! 1) (+ 2 yyy))
------------- REPL is now in #<thread #2> -------------
*** ERROR IN #<procedure #3>, (console)@1.63 -- Unbound variable: xxx
> 12
------------- REPL is now in #<thread #1 primordial> -------------
*** ERROR IN (console)@2.31 -- Unbound variable: yyy
1> (thread-terminate! #2)
*** ERROR IN (console)@2.31 -- MUTEX was abandoned
1> ,b
0  (interaction)           (console)@2:31          yyy

It seems to work fine.  The only mild strangeness is the "MUTEX was abandoned" message, but that is because thread #2 acquired the REPL channel mutex when (thread-terminate! #2) was ***entered*** (but not yet executed) at the REPL in thread #1.  So the (thread-terminate! #2) was executed by thread #1 while thread #2 was holding the mutex.  Thread #2 terminated and thread #1 tried to acquire the (abandoned) mutex for the next REPL interaction, yielding an "MUTEX was abandoned" error.


More information about the Gambit-list mailing list