Blob


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