User-Level Native Thread Primitives (GThread) Library White Paper (G06.27+, H06.03+, J06.03+)

User-Level Native Thread Primitives (GThread Library) 02/15/2012
540065-004 Page 34 of 44
Setting the swap origin address correctly will allow enough space for the unthreaded (master) code
activations (see "Thread Model Variations," Section 1.3). The approach used here is based on the
following observations and rules:
1. Activation of the Thread^Dispatch procedure (sketched subsequently) defines the tip of the
unthreaded stack at rest.
2. The application can arrange that the stack is as deep at the call to Thread^Module^Init as at any
call to Thread^Dispatch.
3. The assertion that the resting unthreaded stack does not overlap an active thread stack is
equivalent to the assertion that mem and reg (TNS/E) stack used values are less than or equal to 0
at rest.
4. Thread^Dispatch should enforce rule 2, using test 3.
5. GTHREAD_INITIALIZE_ sets MainCB.StackOrigin to an invalid value so that
GTHREAD_SETJMP_(MainCB) will set MainCB.StackUsed to a value that fails test 3.
6. A call to GTHREAD_ORIGIN_SET_ or GTHREAD_ORIGIN_SET_MOD_ in the failure path
succeeds the first time but fails on any subsequent call. Being behind the enforcement test, the
initialization code has little cost.
So Thread^Module^Init includes a call to Thread^Dispatch, which has code to set up swap origin and
return immediately. (The effect of this stratagem is that there is a cushion of extra master stack space,
equal to the stack frame size of Thread^Module^Init.)
Thread^Module^Init also creates one thread, called the Undertaker, which participates in thread
termination.
Initiation:
Thread^Create(InitialProc,P1,P2,P3) is the thread initiation procedure. All thread initial procedures
accept exactly three 32-bit arguments, ignoring any that are not needed.
INT .EXT Thread (ThreadImp^Struct); -- the new thread,
-- an extended GThread_CB
INT(32) Arg[0:2];
{allocate space for Thread and a stack area};
Arg[0] := P1;
Arg[1] := P2;
Arg[3] := P3;
IF GTHREAD_INITIATE_
(Thread -- designates GThread_CB
,MainCB -- master CB
,{swap-area address} -- Start