1 d54ead7f 2004-12-28 devnull #include "u.h"
2 d54ead7f 2004-12-28 devnull #include <errno.h>
3 d54ead7f 2004-12-28 devnull #include <sys/time.h>
4 d54ead7f 2004-12-28 devnull #include <sys/types.h>
5 d54ead7f 2004-12-28 devnull #include <sys/wait.h>
6 d54ead7f 2004-12-28 devnull #include <sched.h>
7 d54ead7f 2004-12-28 devnull #include <signal.h>
8 9d654ebc 2005-07-19 devnull #if !defined(__OpenBSD__)
9 fa662c95 2009-09-11 russcox # if defined(__APPLE__)
10 fa662c95 2009-09-11 russcox # define _XOPEN_SOURCE /* for Snow Leopard */
12 cea10000 2005-05-01 devnull # include <ucontext.h>
14 7a2c8850 2005-01-17 devnull #include <sys/utsname.h>
15 d54ead7f 2004-12-28 devnull #include "libc.h"
16 d54ead7f 2004-12-28 devnull #include "thread.h"
18 48ca8d21 2005-11-01 devnull #if defined(__FreeBSD__) && __FreeBSD__ < 5
19 1e05fdf9 2005-11-04 devnull extern int getmcontext(mcontext_t*);
20 1e05fdf9 2005-11-04 devnull extern void setmcontext(mcontext_t*);
21 1e05fdf9 2005-11-04 devnull #define setcontext(u) setmcontext(&(u)->uc_mcontext)
22 1e05fdf9 2005-11-04 devnull #define getcontext(u) getmcontext(&(u)->uc_mcontext)
23 d54ead7f 2004-12-28 devnull extern int swapcontext(ucontext_t*, ucontext_t*);
24 d54ead7f 2004-12-28 devnull extern void makecontext(ucontext_t*, void(*)(), int, ...);
27 04e0a9bb 2011-10-14 rsc #if defined(__APPLE__)
29 2640996a 2007-11-05 rsc * OS X before 10.5 (Leopard) does not provide
30 2640996a 2007-11-05 rsc * swapcontext nor makecontext, so we have to use our own.
31 2640996a 2007-11-05 rsc * In theory, Leopard does provide them, but when we use
32 2640996a 2007-11-05 rsc * them, they seg fault. Maybe we're using them wrong.
33 2640996a 2007-11-05 rsc * So just use our own versions, even on Leopard.
35 b8f742db 2005-01-11 devnull # define mcontext libthread_mcontext
36 b8f742db 2005-01-11 devnull # define mcontext_t libthread_mcontext_t
37 b8f742db 2005-01-11 devnull # define ucontext libthread_ucontext
38 b8f742db 2005-01-11 devnull # define ucontext_t libthread_ucontext_t
39 2640996a 2007-11-05 rsc # define swapcontext libthread_swapcontext
40 2640996a 2007-11-05 rsc # define makecontext libthread_makecontext
41 110c707d 2006-04-21 devnull # if defined(__i386__)
42 110c707d 2006-04-21 devnull # include "386-ucontext.h"
43 04e0a9bb 2011-10-14 rsc # elif defined(__x86_64__)
44 04e0a9bb 2011-10-14 rsc # include "x86_64-ucontext.h"
45 813b3eea 2011-10-23 rsc # elif defined(__ppc__) || defined(__power__)
46 110c707d 2006-04-21 devnull # include "power-ucontext.h"
48 fc567f47 2011-08-02 rsc # error "unknown architecture"
52 cea10000 2005-05-01 devnull #if defined(__OpenBSD__)
53 b4b68641 2005-07-20 devnull # define mcontext libthread_mcontext
54 b4b68641 2005-07-20 devnull # define mcontext_t libthread_mcontext_t
55 b4b68641 2005-07-20 devnull # define ucontext libthread_ucontext
56 b4b68641 2005-07-20 devnull # define ucontext_t libthread_ucontext_t
57 9d654ebc 2005-07-19 devnull # if defined __i386__
58 9d654ebc 2005-07-19 devnull # include "386-ucontext.h"
60 9d654ebc 2005-07-19 devnull # include "power-ucontext.h"
62 cea10000 2005-05-01 devnull extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
65 528dba4d 2007-05-01 devnull /* THIS DOES NOT WORK! Don't do this!
66 528dba4d 2007-05-01 devnull (At least, not on Solaris. Maybe this is right for Linux,
67 528dba4d 2007-05-01 devnull in which case it should say if defined(__linux__) && defined(__sun__),
68 528dba4d 2007-05-01 devnull but surely the latter would be defined(__sparc__).
70 17558fbe 2006-06-04 devnull #if defined(__sun__)
71 17558fbe 2006-06-04 devnull # define mcontext libthread_mcontext
72 17558fbe 2006-06-04 devnull # define mcontext_t libthread_mcontext_t
73 17558fbe 2006-06-04 devnull # define ucontext libthread_ucontext
74 17558fbe 2006-06-04 devnull # define ucontext_t libthread_ucontext_t
75 17558fbe 2006-06-04 devnull # include "sparc-ucontext.h"
79 33a102fc 2005-11-04 devnull #if defined(__arm__)
80 9cf92718 2011-05-18 rsc int mygetmcontext(ulong*);
81 9cf92718 2011-05-18 rsc void mysetmcontext(const ulong*);
82 9cf92718 2011-05-18 rsc #define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0)
83 9cf92718 2011-05-18 rsc #define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0)
87 619085f0 2004-12-25 devnull typedef struct Context Context;
88 e127e40b 2004-12-27 devnull typedef struct Execjob Execjob;
89 619085f0 2004-12-25 devnull typedef struct Proc Proc;
90 619085f0 2004-12-25 devnull typedef struct _Procrendez _Procrendez;
92 e1dc7e45 2004-12-27 devnull typedef struct Jmp Jmp;
93 e1dc7e45 2004-12-27 devnull struct Jmp
95 e1dc7e45 2004-12-27 devnull p9jmp_buf b;
100 619085f0 2004-12-25 devnull STACK = 8192
103 619085f0 2004-12-25 devnull struct Context
105 619085f0 2004-12-25 devnull ucontext_t uc;
106 fc567f47 2011-08-02 rsc #ifdef __APPLE__
108 fc567f47 2011-08-02 rsc * On Snow Leopard, etc., the context routines exist,
109 fc567f47 2011-08-02 rsc * so we use them, but apparently they write past the
110 fc567f47 2011-08-02 rsc * end of the ucontext_t. Sigh. We put some extra
111 fc567f47 2011-08-02 rsc * scratch space here for them.
113 04e0a9bb 2011-10-14 rsc uchar buf[1024];
117 e127e40b 2004-12-27 devnull struct Execjob
119 e127e40b 2004-12-27 devnull int *fd;
120 e127e40b 2004-12-27 devnull char *cmd;
121 e127e40b 2004-12-27 devnull char **argv;
122 e127e40b 2004-12-27 devnull Channel *c;
125 619085f0 2004-12-25 devnull struct _Thread
127 619085f0 2004-12-25 devnull _Thread *next;
128 619085f0 2004-12-25 devnull _Thread *prev;
129 619085f0 2004-12-25 devnull _Thread *allnext;
130 619085f0 2004-12-25 devnull _Thread *allprev;
131 619085f0 2004-12-25 devnull Context context;
132 fc567f47 2011-08-02 rsc void (*startfn)(void*);
133 fc567f47 2011-08-02 rsc void *startarg;
134 619085f0 2004-12-25 devnull uint id;
135 619085f0 2004-12-25 devnull uchar *stk;
136 619085f0 2004-12-25 devnull uint stksize;
137 619085f0 2004-12-25 devnull int exiting;
138 619085f0 2004-12-25 devnull Proc *proc;
139 619085f0 2004-12-25 devnull char name[256];
140 619085f0 2004-12-25 devnull char state[256];
141 8dd8a81f 2005-03-18 devnull void *udata;
145 619085f0 2004-12-25 devnull struct _Procrendez
147 619085f0 2004-12-25 devnull Lock *l;
148 619085f0 2004-12-25 devnull int asleep;
149 4dbefdd4 2004-12-27 devnull #ifdef PLAN9PORT_USING_PTHREADS
150 619085f0 2004-12-25 devnull pthread_cond_t cond;
152 4dbefdd4 2004-12-27 devnull int pid;
156 619085f0 2004-12-25 devnull extern void _procsleep(_Procrendez*);
157 619085f0 2004-12-25 devnull extern void _procwakeup(_Procrendez*);
158 a0a331aa 2005-01-06 devnull extern void _procwakeupandunlock(_Procrendez*);
160 619085f0 2004-12-25 devnull struct Proc
162 4dbefdd4 2004-12-27 devnull Proc *next;
163 4dbefdd4 2004-12-27 devnull Proc *prev;
164 4dbefdd4 2004-12-27 devnull char msg[128];
165 4dbefdd4 2004-12-27 devnull #ifdef PLAN9PORT_USING_PTHREADS
166 4dbefdd4 2004-12-27 devnull pthread_t osprocid;
168 669a062b 2005-01-30 devnull int osprocid;
170 619085f0 2004-12-25 devnull Lock lock;
171 8cbd854a 2004-12-27 devnull int nswitch;
172 619085f0 2004-12-25 devnull _Thread *thread;
173 ff63eeb1 2006-06-26 devnull _Thread *pinthread;
174 619085f0 2004-12-25 devnull _Threadlist runqueue;
175 615e0f9f 2006-02-07 devnull _Threadlist idlequeue;
176 619085f0 2004-12-25 devnull _Threadlist allthreads;
177 619085f0 2004-12-25 devnull uint nthread;
178 619085f0 2004-12-25 devnull uint sysproc;
179 619085f0 2004-12-25 devnull _Procrendez runrend;
180 619085f0 2004-12-25 devnull Context schedcontext;
181 619085f0 2004-12-25 devnull void *udata;
182 e1dc7e45 2004-12-27 devnull Jmp sigjmp;
183 8ee6ad4d 2005-02-14 devnull int mainproc;
186 619085f0 2004-12-25 devnull #define proc() _threadproc()
187 619085f0 2004-12-25 devnull #define setproc(p) _threadsetproc(p)
189 4dbefdd4 2004-12-27 devnull extern Proc *_threadprocs;
190 4dbefdd4 2004-12-27 devnull extern Lock _threadprocslock;
191 955a2ca7 2004-12-27 devnull extern Proc *_threadexecproc;
192 955a2ca7 2004-12-27 devnull extern Channel *_threadexecchan;
193 955a2ca7 2004-12-27 devnull extern QLock _threadexeclock;
194 955a2ca7 2004-12-27 devnull extern Channel *_dowaitchan;
196 73722a8b 2004-12-27 devnull extern void _procstart(Proc*, void (*fn)(Proc*));
197 619085f0 2004-12-25 devnull extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
198 619085f0 2004-12-25 devnull extern void _threadexit(void);
199 619085f0 2004-12-25 devnull extern Proc *_threadproc(void);
200 619085f0 2004-12-25 devnull extern void _threadsetproc(Proc*);
201 619085f0 2004-12-25 devnull extern int _threadlock(Lock*, int, ulong);
202 619085f0 2004-12-25 devnull extern void _threadunlock(Lock*, ulong);
203 e1dc7e45 2004-12-27 devnull extern void _pthreadinit(void);
204 e127e40b 2004-12-27 devnull extern int _threadspawn(int*, char*, char**);
205 e127e40b 2004-12-27 devnull extern int _runthreadspawn(int*, char*, char**);
206 2c87dda8 2004-12-28 devnull extern void _threadsetupdaemonize(void);
207 2c87dda8 2004-12-28 devnull extern void _threaddodaemonize(char*);
208 1d2533d0 2004-12-28 devnull extern void _threadpexit(void);
209 c7acb53e 2005-01-04 devnull extern void _threaddaemonize(void);