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 # elif defined __amd64__
60 # include "x86_64-ucontext.h"
61 # else
62 # include "power-ucontext.h"
63 # endif
64 extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
65 #endif
67 /* THIS DOES NOT WORK! Don't do this!
68 (At least, not on Solaris. Maybe this is right for Linux,
69 in which case it should say if defined(__linux__) && defined(__sun__),
70 but surely the latter would be defined(__sparc__).
72 #if defined(__sun__)
73 # define mcontext libthread_mcontext
74 # define mcontext_t libthread_mcontext_t
75 # define ucontext libthread_ucontext
76 # define ucontext_t libthread_ucontext_t
77 # include "sparc-ucontext.h"
78 #endif
79 */
81 #if defined(__arm__)
82 int mygetmcontext(ulong*);
83 void mysetmcontext(const ulong*);
84 #define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0)
85 #define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0)
86 #endif
89 typedef struct Context Context;
90 typedef struct Execjob Execjob;
91 typedef struct Proc Proc;
92 typedef struct _Procrendez _Procrendez;
94 typedef struct Jmp Jmp;
95 struct Jmp
96 {
97 p9jmp_buf b;
98 };
100 enum
102 STACK = 8192
103 };
105 struct Context
107 ucontext_t uc;
108 #ifdef __APPLE__
109 /*
110 * On Snow Leopard, etc., the context routines exist,
111 * so we use them, but apparently they write past the
112 * end of the ucontext_t. Sigh. We put some extra
113 * scratch space here for them.
114 */
115 uchar buf[1024];
116 #endif
117 };
119 struct Execjob
121 int *fd;
122 char *cmd;
123 char **argv;
124 Channel *c;
125 };
127 struct _Thread
129 _Thread *next;
130 _Thread *prev;
131 _Thread *allnext;
132 _Thread *allprev;
133 Context context;
134 void (*startfn)(void*);
135 void *startarg;
136 uint id;
137 uchar *stk;
138 uint stksize;
139 int exiting;
140 Proc *proc;
141 char name[256];
142 char state[256];
143 void *udata;
144 Alt *alt;
145 };
147 struct _Procrendez
149 Lock *l;
150 int asleep;
151 #ifdef PLAN9PORT_USING_PTHREADS
152 pthread_cond_t cond;
153 #else
154 int pid;
155 #endif
156 };
158 extern void _procsleep(_Procrendez*);
159 extern void _procwakeup(_Procrendez*);
160 extern void _procwakeupandunlock(_Procrendez*);
162 struct Proc
164 Proc *next;
165 Proc *prev;
166 char msg[128];
167 #ifdef PLAN9PORT_USING_PTHREADS
168 pthread_t osprocid;
169 #else
170 int osprocid;
171 #endif
172 Lock lock;
173 int nswitch;
174 _Thread *thread;
175 _Thread *pinthread;
176 _Threadlist runqueue;
177 _Threadlist idlequeue;
178 _Threadlist allthreads;
179 uint nthread;
180 uint sysproc;
181 _Procrendez runrend;
182 Context schedcontext;
183 void *udata;
184 Jmp sigjmp;
185 int mainproc;
186 };
188 #define proc() _threadproc()
189 #define setproc(p) _threadsetproc(p)
191 extern Proc *_threadprocs;
192 extern Lock _threadprocslock;
193 extern Proc *_threadexecproc;
194 extern Channel *_threadexecchan;
195 extern QLock _threadexeclock;
196 extern Channel *_dowaitchan;
198 extern void _procstart(Proc*, void (*fn)(Proc*));
199 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
200 extern void _threadexit(void);
201 extern Proc *_threadproc(void);
202 extern void _threadsetproc(Proc*);
203 extern int _threadlock(Lock*, int, ulong);
204 extern void _threadunlock(Lock*, ulong);
205 extern void _pthreadinit(void);
206 extern int _threadspawn(int*, char*, char**);
207 extern int _runthreadspawn(int*, char*, char**);
208 extern void _threadsetupdaemonize(void);
209 extern void _threaddodaemonize(char*);
210 extern void _threadpexit(void);
211 extern void _threaddaemonize(void);