1 #include "thread.h" 2 3 typedef enum { 4 Running, 5 Runnable, 6 Rendezvous, 7 } State; 8 9 typedef enum { 10 Callnil, 11 Callalt, 12 Callsnd, 13 Callrcv, 14 } Callstate; 15 16 enum { 17 DOEXEC = 1, 18 DOEXIT = 2, 19 DOPROC = 3, 20 }; 21 22 struct Tqueue { // Thread queue 23 Lock lock; 24 Thread *head; 25 Thread *tail; 26 }; 27 28 struct Thread { 29 Lock lock; // protects thread data structure 30 int id; // thread id 31 int grp; // thread group 32 State state; // state of thread 33 int exiting; // should it die? 34 Callstate call; // which `system call' is current 35 char *cmdname; // ptr to name of thread 36 Thread *next; // next on queue (run, rendezvous) 37 Thread *nextt; // next on list of all theads 38 Proc *proc; // proc of this thread 39 ulong tag; // rendez-vous tag 40 Alt *alt; // pointer to alt structure 41 ulong value; // rendez-vous value 42 Thread *garbage; // ptr to thread to clean up 43 jmp_buf env; // jump buf for launching or switching threads 44 uchar *stk; // top of stack (lowest address of stack) 45 uint stksize; // stack size 46 47 ulong udata; // User per-thread data pointer 48 }; 49 50 struct Proc { 51 Lock lock; 52 int pid; 53 54 jmp_buf oldenv; // jump buf for returning to original stack 55 56 int nthreads; 57 Tqueue threads; // All threads of this proc 58 Tqueue runnable; // Runnable threads 59 Thread *curthread; // Running thread 60 61 int blocked; // In a rendezvous 62 uint nextID; // ID of most recently created thread 63 Proc *next; // linked list of Procs 64 65 void *arg; // passed between shared and unshared stk 66 char str[ERRLEN]; // used by threadexits to avoid malloc 67 68 ulong udata; // User per-proc data pointer 69 }; 70 71 typedef struct Newproc { 72 uchar *stack; 73 uint stacksize; 74 ulong *stackptr; 75 ulong launcher; 76 int grp; 77 int rforkflag; 78 } Newproc; 79 80 typedef struct Execproc { 81 Proc *procp; 82 char *file; 83 char **arg; 84 char data[4096]; 85 } Execproc; 86 87 struct Pqueue { // Proc queue 88 Lock lock; 89 Proc *head; 90 Proc *tail; 91 }; 92 93 extern struct Pqueue pq; // Linked list of procs 94 extern Proc **procp; // Pointer to pointer to proc's Proc structure 95 96 int tas(int*); 97 int inc(int*, int); 98 int cas(Lock *lock, Lock old, Lock new); 99 ulong _threadrendezvous(ulong, ulong); 100 void *_threadmalloc(long size); 101 void _xinc(long*); 102 long _xdec(long*); 103