Listing 2

/*********** THREAD.H COPYRIGHT 1989 GREGORY COLVIN ************
This program may be distributed free with this copyright notice.
***************************************************************/
#ifndef THREAD
#define THREAD

#include <assert.h>
#include <setjmp.h>
#include <stdio.h>

typedef struct {
   jmp_buf exec;                 /* state of thread for exec   */
   jmp_buf jump;                 /* state of thread for jump   */
   jmp_buf exit;                 /* state of thread for exit   */
   int parent;                   /* id of parent thread        */
   int nchildren;                /* number of children         */
   int free;                     /* id of next free thread     */
   int next;                     /* id of next thread to run   */
   char *stack;                  /* top of stack for thread    */
} thread;

extern thread *Threads;          /* table of threads            */
extern int  ThCurr;              /* current thread              */

#define ThProbe() { char p; assert(Threads[ThCurr].stack < &p);}

#define ThId() ThCurr

thread *ThInit(int n,int size);  /* create n size byte threads  */
void ThFree(void);               /* free the thread table       */
int ThNew(void (*root)(void));   /* fork and exec new thread    */
int ThJump(int id);              /* jump to another thread      */
void ThExit(void);               /* exit to parent              */

#endif