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 2640996a 2007-11-05 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"
44 110c707d 2006-04-21 devnull # include "power-ucontext.h"
48 cea10000 2005-05-01 devnull #if defined(__OpenBSD__)
49 b4b68641 2005-07-20 devnull # define mcontext libthread_mcontext
50 b4b68641 2005-07-20 devnull # define mcontext_t libthread_mcontext_t
51 b4b68641 2005-07-20 devnull # define ucontext libthread_ucontext
52 b4b68641 2005-07-20 devnull # define ucontext_t libthread_ucontext_t
53 9d654ebc 2005-07-19 devnull # if defined __i386__
54 9d654ebc 2005-07-19 devnull # include "386-ucontext.h"
56 9d654ebc 2005-07-19 devnull # include "power-ucontext.h"
58 cea10000 2005-05-01 devnull extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
61 528dba4d 2007-05-01 devnull /* THIS DOES NOT WORK! Don't do this!
62 528dba4d 2007-05-01 devnull (At least, not on Solaris. Maybe this is right for Linux,
63 528dba4d 2007-05-01 devnull in which case it should say if defined(__linux__) && defined(__sun__),
64 528dba4d 2007-05-01 devnull but surely the latter would be defined(__sparc__).
66 17558fbe 2006-06-04 devnull #if defined(__sun__)
67 17558fbe 2006-06-04 devnull # define mcontext libthread_mcontext
68 17558fbe 2006-06-04 devnull # define mcontext_t libthread_mcontext_t
69 17558fbe 2006-06-04 devnull # define ucontext libthread_ucontext
70 17558fbe 2006-06-04 devnull # define ucontext_t libthread_ucontext_t
71 17558fbe 2006-06-04 devnull # include "sparc-ucontext.h"
75 33a102fc 2005-11-04 devnull #if defined(__arm__)
76 9cf92718 2011-05-18 rsc int mygetmcontext(ulong*);
77 9cf92718 2011-05-18 rsc void mysetmcontext(const ulong*);
78 9cf92718 2011-05-18 rsc #define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0)
79 9cf92718 2011-05-18 rsc #define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0)
83 619085f0 2004-12-25 devnull typedef struct Context Context;
84 e127e40b 2004-12-27 devnull typedef struct Execjob Execjob;
85 619085f0 2004-12-25 devnull typedef struct Proc Proc;
86 619085f0 2004-12-25 devnull typedef struct _Procrendez _Procrendez;
88 e1dc7e45 2004-12-27 devnull typedef struct Jmp Jmp;
89 e1dc7e45 2004-12-27 devnull struct Jmp
91 e1dc7e45 2004-12-27 devnull p9jmp_buf b;
96 619085f0 2004-12-25 devnull STACK = 8192
99 619085f0 2004-12-25 devnull struct Context
101 619085f0 2004-12-25 devnull ucontext_t uc;
104 e127e40b 2004-12-27 devnull struct Execjob
106 e127e40b 2004-12-27 devnull int *fd;
107 e127e40b 2004-12-27 devnull char *cmd;
108 e127e40b 2004-12-27 devnull char **argv;
109 e127e40b 2004-12-27 devnull Channel *c;
112 619085f0 2004-12-25 devnull struct _Thread
114 619085f0 2004-12-25 devnull _Thread *next;
115 619085f0 2004-12-25 devnull _Thread *prev;
116 619085f0 2004-12-25 devnull _Thread *allnext;
117 619085f0 2004-12-25 devnull _Thread *allprev;
118 619085f0 2004-12-25 devnull Context context;
119 619085f0 2004-12-25 devnull uint id;
120 619085f0 2004-12-25 devnull uchar *stk;
121 619085f0 2004-12-25 devnull uint stksize;
122 619085f0 2004-12-25 devnull int exiting;
123 619085f0 2004-12-25 devnull void (*startfn)(void*);
124 619085f0 2004-12-25 devnull void *startarg;
125 619085f0 2004-12-25 devnull Proc *proc;
126 619085f0 2004-12-25 devnull char name[256];
127 619085f0 2004-12-25 devnull char state[256];
128 8dd8a81f 2005-03-18 devnull void *udata;
132 619085f0 2004-12-25 devnull struct _Procrendez
134 619085f0 2004-12-25 devnull Lock *l;
135 619085f0 2004-12-25 devnull int asleep;
136 4dbefdd4 2004-12-27 devnull #ifdef PLAN9PORT_USING_PTHREADS
137 619085f0 2004-12-25 devnull pthread_cond_t cond;
139 4dbefdd4 2004-12-27 devnull int pid;
143 619085f0 2004-12-25 devnull extern void _procsleep(_Procrendez*);
144 619085f0 2004-12-25 devnull extern void _procwakeup(_Procrendez*);
145 a0a331aa 2005-01-06 devnull extern void _procwakeupandunlock(_Procrendez*);
147 619085f0 2004-12-25 devnull struct Proc
149 4dbefdd4 2004-12-27 devnull Proc *next;
150 4dbefdd4 2004-12-27 devnull Proc *prev;
151 4dbefdd4 2004-12-27 devnull char msg[128];
152 4dbefdd4 2004-12-27 devnull #ifdef PLAN9PORT_USING_PTHREADS
153 4dbefdd4 2004-12-27 devnull pthread_t osprocid;
155 669a062b 2005-01-30 devnull int osprocid;
157 619085f0 2004-12-25 devnull Lock lock;
158 8cbd854a 2004-12-27 devnull int nswitch;
159 619085f0 2004-12-25 devnull _Thread *thread;
160 ff63eeb1 2006-06-26 devnull _Thread *pinthread;
161 619085f0 2004-12-25 devnull _Threadlist runqueue;
162 615e0f9f 2006-02-07 devnull _Threadlist idlequeue;
163 619085f0 2004-12-25 devnull _Threadlist allthreads;
164 619085f0 2004-12-25 devnull uint nthread;
165 619085f0 2004-12-25 devnull uint sysproc;
166 619085f0 2004-12-25 devnull _Procrendez runrend;
167 619085f0 2004-12-25 devnull Context schedcontext;
168 619085f0 2004-12-25 devnull void *udata;
169 e1dc7e45 2004-12-27 devnull Jmp sigjmp;
170 8ee6ad4d 2005-02-14 devnull int mainproc;
173 619085f0 2004-12-25 devnull #define proc() _threadproc()
174 619085f0 2004-12-25 devnull #define setproc(p) _threadsetproc(p)
176 4dbefdd4 2004-12-27 devnull extern Proc *_threadprocs;
177 4dbefdd4 2004-12-27 devnull extern Lock _threadprocslock;
178 955a2ca7 2004-12-27 devnull extern Proc *_threadexecproc;
179 955a2ca7 2004-12-27 devnull extern Channel *_threadexecchan;
180 955a2ca7 2004-12-27 devnull extern QLock _threadexeclock;
181 955a2ca7 2004-12-27 devnull extern Channel *_dowaitchan;
183 73722a8b 2004-12-27 devnull extern void _procstart(Proc*, void (*fn)(Proc*));
184 619085f0 2004-12-25 devnull extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
185 619085f0 2004-12-25 devnull extern void _threadexit(void);
186 619085f0 2004-12-25 devnull extern Proc *_threadproc(void);
187 619085f0 2004-12-25 devnull extern void _threadsetproc(Proc*);
188 619085f0 2004-12-25 devnull extern int _threadlock(Lock*, int, ulong);
189 619085f0 2004-12-25 devnull extern void _threadunlock(Lock*, ulong);
190 e1dc7e45 2004-12-27 devnull extern void _pthreadinit(void);
191 e127e40b 2004-12-27 devnull extern int _threadspawn(int*, char*, char**);
192 e127e40b 2004-12-27 devnull extern int _runthreadspawn(int*, char*, char**);
193 2c87dda8 2004-12-28 devnull extern void _threadsetupdaemonize(void);
194 2c87dda8 2004-12-28 devnull extern void _threaddodaemonize(char*);
195 1d2533d0 2004-12-28 devnull extern void _threadpexit(void);
196 c7acb53e 2005-01-04 devnull extern void _threaddaemonize(void);