MPS issue job003559

TitleMultiple thread registration does not work on all platforms
Assigned userRichard Brooksby
DescriptionIt's not clear from the documentation or code whether registering a thread more than once is permitted, or whether it needs to be supported.

(Originally reported by Tony Mann [2].)
Analysisrequest.dylan.160252 [7] shows that this feature is needed to support the situation where a program that does not use the MPS is calling into MPS-using code from multiple threads. On entry to the MPS-using code, the thread can be registered, but it may not be possible to ensure that the thread is deregistered on exit, because control may be transferred by some non-local mechanism such as an exception or longjmp. It would not be acceptable to insist that the client program keep a table of threads it has registered, because maintaining the table might require allocation, which might provoke a collection. In addition, each entry point might be at a different position in the stack and so require a root with a different stack base.

Does multiple registration work? Here's an analysis:

On XC, we use the Mach calls thread_suspend and thread_resume [3], which increment and decrement the thread's suspend count, so it does not matter how many times a thread is registered (all that happens is that there are some wasted calls). However, the actual registration fails with

protxc.c:341: MPS ASSERTION FAILED: old_exception_ports == MACH_PORT_NULL

On W3, we use SuspendThread [4] and ResumeThread [5] which also increment and decrement the thread's suspend count.

On LI and FR, we take care not to suspend and resume a thread more than once (threads with the same thread id go onto a idRing when suspended). See pthrdext.c [6]. Note that this means that thread suspension is O(n^2).

On AN, the second attempt to register the thread causes an assertion failure. (Fixed in change 187354.)
How foundinspection
Evidence[1] "Better snowmen: proposed solution" <>
Observed in1.111.0
Created byRichard Brooksby
Created on2013-07-17 14:34:52
Last modified byGareth Rees
Last modified on2016-09-04 14:23:07
History2013-07-17 RB Created.


Change Effect Date User Description
192127 closed 2016-09-04 14:23:07 Gareth Rees Support registering a thread with the MPS multiple times on OS X.
Restore test case.
187354 open 2014-10-22 20:42:56 Gareth Rees Complete design.mps.thread-manager and move it from old to current.
Better description of protection mutator context module in the "porting" chapter.
The generic thread manager mustn't assert that there is only one thread -- this would break design.mps.thread-manager.req.register.multi.
183114 open 2013-07-17 14:38:05 Richard Brooksby Removing false comment about multiple thread registration, but see job003559.

Imported from Git
 Author: Richard Brooksby <> 1374068285 +0100
 Committer: Richard Brooksby <> 1374068285 +0100
 sha1: 06d95510792c4555a071f3f020d25ad6a25982a4