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 35 of 46
IF @NextThread <> @CurThread THEN
IF GTHREAD_SETJMP_(CurThread,GTH_NULL_PROCADDR) = 0d THEN
BEGIN
IF GTH_STACK_CHECK_THREAD2_(CurThread, 0, 0) THEN
..; -- 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.