User-Level Native Thread Primitives (GThread) Library White Paper (G06.26+)
User-Level Native Thread Primitives (GThread Library) 03/31/2005
Hewlett-Packard Company--540065-001 Page 36 of 46
Initialization:
Thread^Module^Init(...) is the procedure to initialize the thread package. Its actions include
initializing GThread:
IF GTHREAD_INITIALIZE_(GTHREAD_VERSION,MainCB) THEN
...; -- Version mismatch
Thread^Dispatch; -- see following text
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 MainCB.StackUsed <= 0d 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_ 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};