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 7 of 46
2. Thread Primitives
The primitives defined below constitute a facility called "GThread." Procedure and structure identifiers
begin with "GTHREAD" or “GTH” (for "Guardian Thread"), in contrast with the "pthread" identifiers
specified for POSIX threads, and to avoid conflict with identifiers in existing thread packages. Defines
and literals begin "GT".
These facilities are regarded as primitives in two senses: they perform low-level functions, and they
typically are hidden from the client application by a higher-level thread package. The "application" and
the "thread package" (if any) are collectively called the "program" in this document.
The GThread Primitives support user-level threads for unprivileged processes using either the master or
peer protocol and either static or swapped stacks (or a mixture of both). For multithreading purposes, a
process is unprivileged if its code is unprivileged whenever one of the thread primitives is called.
2.1. Thread Context
Multithreading requires three operations on process context:
- Saving the context when suspending a thread,
- Restoring the context when resuming a thread,
- Creating the initial context of a new thread.
For static threads, this context is just the contents of certain processor registers. For swapped threads,
the context also includes the procedure activation records (stack frames) for procedures currently active
in the thread. GThread primitives maintain the register context and provide the address ranges of the
stack frames, and they provide a function to copy the stack data.
Thread register context is maintained in "jump buffers," the data structures used by the standard C
setjmp() and longjmp() functions. The jump buffer is augmented with other needed thread data.
2.2. Declarations
Declarations and statements must occur in the program to set up and initialize the GThread facilities.
The primitives are exported to the program as declaration files including structures, literals, defines, and
external procedures. For pTAL and EpTAL programs, the header file HGTHREAD must be sourced in.
A program in C or C++ must include the header file GTHREADH (gthread.h) instead. The same header
files serve both TNS and native TNS/R or TNS/E programs; the appropriate text is selected at compile
time on the basis of the target architecture.