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 32 of 44
3. Usage
The following sections use sketched pTAL/EpTAL examples to illustrate usage of the GThread primitives
for several multithreading models. All models declare one GThread_CB structure named MainCB for the
original thread.
3.1. Native Static
The static model is the simplest to implement. The bare essentials of a simple native-only, unprotected
stack implementation are sketched.
Initialization:
IF GTHREAD_INITIALIZE_(GTHREAD_VERSION,MainCB) THEN
...; -- Error: version mismatch
GTHREAD_SETJMP_(MainCB,GTH_NULL_PROCADDR);
IF GTHREAD_ORIGIN_SET_(MainCB) THEN
...; -- Unexpected error: program failure
Setting the Stack Origin in MainCB is essential for swapped threads; it is necessary for static threads if
GTH_STACK_USED_ or any of the four GTH_STACK_CHECK⦠queries will be called with MainCB.
Initiation:
{allocate GThread_CB};
{allocate stack area};
IF GTHREAD_INITIATE_(CB -- designates GThread_CB
,MainCB -- designates initial CB
,Start,Length -- designate stack area
,GTS_STATIC -- specifies static thread
,@InitialProc -- designates proc for thread
,ArgStart,ArgLen -- designate argument list
,@FinalProc -- designate termination proc
) THEN -- error
In a peer protocol, the thread switching logic might be:
@NextThread := {next thread selected to run};
IF @NextThread <> @CurThread THEN
IF GTHREAD_SETJMP_(CurThread,GTH_NULL_PROCADDR) = 0d THEN
BEGIN
IF GTH_STACK_CHECK_THREAD2_(CurThread, 0, 0) THEN