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.
27 typedef struct Thread Thread;
28 typedef struct Proc Proc;
29 typedef struct Tqueue Tqueue;
30 typedef struct Pqueue Pqueue;
31 typedef struct Rgrp Rgrp;
32 typedef struct Execargs Execargs;
34 /* must match list in sched.c */
61 Thread *hash[RENDHASH];
64 struct Tqueue /* Thread queue */
73 Lock lock; /* protects thread data structure */
74 Label sched; /* for context switches */
75 int id; /* thread id */
76 int grp; /* thread group */
77 int moribund; /* thread needs to die */
78 State state; /* run state */
79 State nextstate; /* next run state */
80 uchar *stk; /* top of stack (lowest address of stack) */
81 uint stksize; /* stack size */
82 Thread *next; /* next on ready queue */
84 Proc *proc; /* proc of this thread */
85 Thread *nextt; /* next on list of threads in this proc */
86 Thread *prevt; /* prev on list of threads in this proc */
87 int ret; /* return value for Exec, Fork */
89 char *cmdname; /* ptr to name of thread */
92 Thread *rendhash; /* Trgrp linked list */
93 ulong rendtag; /* rendezvous tag */
94 ulong rendval; /* rendezvous value */
95 int rendbreak; /* rendezvous has been taken */
97 Chanstate chan; /* which channel operation is current */
98 Alt *alt; /* pointer to current alt structure (debugging) */
101 void* udata[NPRIV]; /* User per-thread data pointer */
115 Label sched; /* for context switches */
116 Proc *link; /* in proctab */
117 int pid; /* process id */
118 int splhi; /* delay notes */
119 Thread *thread; /* running thread */
120 Thread *idle; /* idle thread */
123 Execargs exec; /* exec argument */
124 Proc *newproc; /* fork argument */
125 char exitstr[ERRMAX]; /* exit status */
129 Tqueue threads; /* All threads of this proc */
130 Tqueue ready; /* Runnable threads */
133 char printbuf[Printsize];
134 int blocked; /* In a rendezvous */
135 int pending; /* delayed note pending */
136 int nonotes; /* delay notes */
137 uint nextID; /* ID of most recently created thread */
138 Proc *next; /* linked list of Procs */
140 void *arg; /* passed between shared and unshared stk */
141 char str[ERRMAX]; /* used by threadexits to avoid malloc */
142 char errbuf[ERRMAX]; /* errstr */
145 void* udata; /* User per-proc data pointer */
148 struct Pqueue { /* Proc queue */
159 long (*op)(va_list*);
166 void _gotolabel(Label*);
167 int _setlabel(Label*);
168 void _freeproc(Proc*);
169 Proc* _newproc(void(*)(void*), void*, uint, char*, int, int);
170 int _procsplhi(void);
173 int _schedexec(Execargs*);
174 void _schedexecwait(void);
175 void _schedexit(Proc*);
176 int _schedfork(Proc*);
177 void _schedinit(void*);
178 void _systhreadinit(void);
179 void _threadassert(char*);
180 void _threadbreakrendez(void);
181 void __threaddebug(ulong, char*, ...);
182 #define _threaddebug if(!_threaddebuglevel){}else __threaddebug
183 void _threadexitsall(char*);
184 void _threadflagrendez(Thread*);
185 Proc* _threadgetproc(void);
186 extern void _threadmultiproc(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 void _threadidle(void);
194 ulong _threadrendezvous(ulong, ulong);
195 void _threadsignal(void);
196 void _threadsysfatal(char*, va_list);
199 void _threadremove(Proc*, Thread*);
200 void threadstatus(void);
202 extern int _threaddebuglevel;
203 extern char* _threadexitsallstatus;
204 extern Pqueue _threadpq;
205 extern Channel* _threadwaitchan;
206 extern Rgrp _threadrgrp;
207 extern void _stackfree(void*);
209 #define DBGAPPL (1 << 0)
210 #define DBGSCHED (1 << 16)
211 #define DBGCHAN (1 << 17)
212 #define DBGREND (1 << 18)
213 /* #define DBGKILL (1 << 19) */
214 #define DBGNOTE (1 << 20)
215 #define DBGEXEC (1 << 21)
217 #define ioproc_arg(io, type) (va_arg((io)->arg, type))
218 extern int _threadgetpid(void);
219 extern void _threadmemset(void*, int, int);
220 extern void _threaddebugmemset(void*, int, int);
221 extern int _threadprocs;
222 extern void _threadstacklimit(void*);