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 33 of 44
..; -- stack is in overflow condition
@CurThread := @NextThread;
GTHREAD_LONGJMP_(CurThread,1d,GTH_NULL_PROCADDR_);
END;
A master protocol is similar, except the work is split:
Suspend current thread (executed in a thread):
IF GTHREAD_SETJMP_(CurThread,GTH_NULL_PROCADDR) = 0d THEN
BEGIN
IF GTH_STACK_CHECK_THREAD2_(CurThread, 0, 0) THEN
..; -- stack overflowed
GTHREAD_LONGJMP_(MainCB,1d,GTH_NULL_PROCADDR_);
END;
Dispatch a thread (executed in unthreaded state):
@CurThread := {next thread selected to run};
GTHREAD_LONGJMP_(CurThread,1d,GTH_NULL_PROCADDR_);
3.2. Swapped Master
The swapped model with master protocol is somewhat more complex. The following sketch illustrates
how a fairly typical thread package could use the primitives. The package exports four principal
interfaces:
Thread^Module^Init: initializes the package
Thread^Create: establishes a new thread
Thread^Dispatch: executes all threads ready to run
Thread^Suspend: suspends thread; resumes unthreaded
Other identifiers named Thread^... are internal to the package. The package maintains several thread lists,
including a "ready" list of threads ready for execution.
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