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 48 struct Proc { 49 Lock lock; 50 int pid; 51 52 jmp_buf oldenv; // jump buf for returning to original stack 53 54 int nthreads; 55 Tqueue threads; // All threads of this proc 56 Tqueue runnable; // Runnable threads 57 Thread *curthread; // Running thread 58 59 int blocked; // In a rendezvous 60 uint nextID; // ID of most recently created thread 61 Proc *next; // linked list of Procs 62 63 void *arg; // passed between shared and unshared stk 64 char str[ERRLEN]; // used by threadexits to avoid malloc 65 66 ulong udata; // User per-proc data pointer 67 }; 68 69 typedef struct Newproc { 70 uchar *stack; 71 uint stacksize; 72 ulong *stackptr; 73 ulong launcher; 74 int grp; 75 } Newproc; 76 77 typedef struct Execproc { 78 Proc *procp; 79 char *file; 80 char **arg; 81 char data[4096]; 82 } Execproc; 83 84 struct Pqueue { // Proc queue 85 Lock lock; 86 Proc *head; 87 Proc *tail; 88 }; 89 90 extern struct Pqueue pq; // Linked list of procs 91 extern Proc **procp; // Pointer to pointer to proc's Proc structure 92 93 int tas(int*); 94 int inc(int*, int); 95 int cas(Lock *lock, Lock old, Lock new); 96 ulong _threadrendezvous(ulong, ulong); 97 void *_threadmalloc(long size); 98 void _xinc(long*); 99 long _xdec(long*); 100