2 * Some notes on locking:
4 * All the locking woes come from implementing
5 * threadinterrupt (and threadkill).
7 * _threadgetproc()->thread is always a live pointer.
8 * p->threads, p->ready, and _threadrgrp also contain
9 * live thread pointers. These may only be consulted
10 * while holding p->lock or _threadrgrp.lock; in procs
11 * other than p, the pointers are only guaranteed to be live
12 * while the lock is still being held.
14 * Thread structures can only be freed by the proc
15 * they belong to. Threads marked with t->inrendez
16 * need to be extracted from the _threadrgrp before
19 * _threadrgrp.lock cannot be acquired while holding p->lock.
32 typedef struct Thread Thread;
33 typedef struct Proc Proc;
34 typedef struct Tqueue Tqueue;
35 typedef struct Pqueue Pqueue;
36 typedef struct Rgrp Rgrp;
37 typedef struct Execargs Execargs;
39 /* must match list in sched.c */
66 Thread *hash[RENDHASH];
69 struct Tqueue /* Thread queue */
78 Lock lock; /* protects thread data structure */
79 Label sched; /* for context switches */
80 int id; /* thread id */
81 int grp; /* thread group */
82 int moribund; /* thread needs to die */
83 State state; /* run state */
84 State nextstate; /* next run state */
85 uchar *stk; /* top of stack (lowest address of stack) */
86 uint stksize; /* stack size */
87 Thread *next; /* next on ready queue */
89 Proc *proc; /* proc of this thread */
90 Thread *nextt; /* next on list of threads in this proc */
91 Thread *prevt; /* prev on list of threads in this proc */
92 int ret; /* return value for Exec, Fork */
94 char *cmdname; /* ptr to name of thread */
97 Thread *rendhash; /* Trgrp linked list */
98 ulong rendtag; /* rendezvous tag */
99 ulong rendval; /* rendezvous value */
100 int rendbreak; /* rendezvous has been taken */
102 Chanstate chan; /* which channel operation is current */
103 Alt *alt; /* pointer to current alt structure (debugging) */
105 void* udata[NPRIV]; /* User per-thread data pointer */
118 Label sched; /* for context switches */
119 Proc *link; /* in proctab */
120 int pid; /* process id */
121 int splhi; /* delay notes */
122 Thread *thread; /* running thread */
125 Execargs exec; /* exec argument */
126 Proc *newproc; /* fork argument */
127 char exitstr[ERRMAX]; /* exit status */
131 Tqueue threads; /* All threads of this proc */
132 Tqueue ready; /* Runnable threads */
135 char printbuf[Printsize];
136 int blocked; /* In a rendezvous */
137 int pending; /* delayed note pending */
138 int nonotes; /* delay notes */
139 uint nextID; /* ID of most recently created thread */
140 Proc *next; /* linked list of Procs */
142 void *arg; /* passed between shared and unshared stk */
143 char str[ERRMAX]; /* used by threadexits to avoid malloc */
144 char errbuf[ERRMAX]; /* errstr */
146 void* udata; /* User per-proc data pointer */
149 struct Pqueue { /* Proc queue */
160 long (*op)(va_list*);
167 void _gotolabel(Label*);
168 int _setlabel(Label*);
169 void _freeproc(Proc*);
170 Proc* _newproc(void(*)(void*), void*, uint, char*, int, int);
171 int _procsplhi(void);
174 int _schedexec(Execargs*);
175 void _schedexecwait(void);
176 void _schedexit(Proc*);
177 int _schedfork(Proc*);
178 void _schedinit(void*);
179 void _systhreadinit(void);
180 void _threadassert(char*);
181 void _threadbreakrendez(void);
182 void __threaddebug(ulong, char*, ...);
183 #define _threaddebug if(!_threaddebuglevel){}else __threaddebug
184 void _threadexitsall(char*);
185 void _threadflagrendez(Thread*);
186 Proc* _threadgetproc(void);
187 Proc* _threaddelproc(void);
188 void _threadsetproc(Proc*);
189 void _threadinitstack(Thread*, void(*)(void*), void*);
190 void* _threadmalloc(long, int);
191 void _threadnote(void*, char*);
192 void _threadready(Thread*);
193 ulong _threadrendezvous(ulong, ulong);
194 void _threadsignal(void);
195 void _threadsysfatal(char*, va_list);
198 void _threadremove(Proc*, Thread*);
200 extern int _threaddebuglevel;
201 extern char* _threadexitsallstatus;
202 extern Pqueue _threadpq;
203 extern Channel* _threadwaitchan;
204 extern Rgrp _threadrgrp;
205 extern void _stackfree(void*);
207 #define DBGAPPL (1 << 0)
208 #define DBGSCHED (1 << 16)
209 #define DBGCHAN (1 << 17)
210 #define DBGREND (1 << 18)
211 /* #define DBGKILL (1 << 19) */
212 #define DBGNOTE (1 << 20)
213 #define DBGEXEC (1 << 21)
215 #define ioproc_arg(io, type) (va_arg((io)->arg, type))
216 extern int _threadgetpid(void);
217 extern void _threadmemset(void*, int, int);
218 extern void _threaddebugmemset(void*, int, int);