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(__APPLE__)
19 /*
20 * OS X before 10.5 (Leopard) does not provide
21 * swapcontext nor makecontext, so we have to use our own.
22 * In theory, Leopard does provide them, but when we use
23 * them, they seg fault. Maybe we're using them wrong.
24 * So just use our own versions, even on Leopard.
25 */
26 # define mcontext libthread_mcontext
27 # define mcontext_t libthread_mcontext_t
28 # define ucontext libthread_ucontext
29 # define ucontext_t libthread_ucontext_t
30 # define swapcontext libthread_swapcontext
31 # define makecontext libthread_makecontext
32 # if defined(__i386__)
33 # include "386-ucontext.h"
34 # elif defined(__x86_64__)
35 # include "x86_64-ucontext.h"
36 # elif defined(__ppc__) || defined(__power__)
37 # include "power-ucontext.h"
38 # else
39 # error "unknown architecture"
40 # endif
41 #endif
43 #if defined(__OpenBSD__)
44 # define mcontext libthread_mcontext
45 # define mcontext_t libthread_mcontext_t
46 # define ucontext libthread_ucontext
47 # define ucontext_t libthread_ucontext_t
48 # if defined __i386__
49 # include "386-ucontext.h"
50 # elif defined __amd64__
51 # include "x86_64-ucontext.h"
52 # else
53 # include "power-ucontext.h"
54 # endif
55 extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
56 #endif
58 #if defined(__arm__)
59 int mygetmcontext(ulong*);
60 void mysetmcontext(const ulong*);
61 #define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0)
62 #define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0)
63 #endif
66 typedef struct Context Context;
67 typedef struct Execjob Execjob;
68 typedef struct Proc Proc;
69 typedef struct _Procrendez _Procrendez;
71 typedef struct Jmp Jmp;
72 struct Jmp
73 {
74 p9jmp_buf b;
75 };
77 enum
78 {
79 STACK = 8192
80 };
82 struct Context
83 {
84 ucontext_t uc;
85 #ifdef __APPLE__
86 /*
87 * On Snow Leopard, etc., the context routines exist,
88 * so we use them, but apparently they write past the
89 * end of the ucontext_t. Sigh. We put some extra
90 * scratch space here for them.
91 */
92 uchar buf[1024];
93 #endif
94 };
96 struct Execjob
97 {
98 int *fd;
99 char *cmd;
100 char **argv;
101 char *dir;
102 Channel *c;
103 };
105 struct _Procrendez
107 Lock *l;
108 int asleep;
109 #ifdef PLAN9PORT_USING_PTHREADS
110 pthread_cond_t cond;
111 #else
112 int pid;
113 #endif
114 };
116 struct _Thread
118 _Thread *next;
119 _Thread *prev;
120 _Thread *allnext;
121 _Thread *allprev;
122 Context context;
123 void (*startfn)(void*);
124 void *startarg;
125 uint id;
126 #ifdef PLAN9PORT_USING_PTHREADS
127 pthread_t osprocid;
128 #else
129 int osprocid;
130 #endif
131 uchar *stk;
132 uint stksize;
133 int exiting;
134 Proc *proc;
135 char name[256];
136 char state[256];
137 void *udata;
138 Alt *alt;
139 _Procrendez schedrend;
140 };
142 extern void _procsleep(_Procrendez*);
143 extern void _procwakeup(_Procrendez*);
144 extern void _procwakeupandunlock(_Procrendez*);
146 struct Proc
148 Proc *next;
149 Proc *prev;
150 char msg[128];
151 #ifdef PLAN9PORT_USING_PTHREADS
152 pthread_t osprocid;
153 #else
154 int osprocid;
155 #endif
156 Lock lock;
157 int nswitch;
158 _Thread *thread0;
159 _Thread *thread;
160 _Thread *pinthread;
161 _Threadlist runqueue;
162 _Threadlist idlequeue;
163 _Threadlist allthreads;
164 uint nthread;
165 uint sysproc;
166 _Procrendez runrend;
167 Lock schedlock;
168 _Thread *schedthread;
169 Context schedcontext;
170 void *udata;
171 Jmp sigjmp;
172 int mainproc;
173 };
175 #define proc() _threadproc()
177 extern Proc *_threadprocs;
178 extern Lock _threadprocslock;
179 extern Proc *_threadexecproc;
180 extern Channel *_threadexecchan;
181 extern QLock _threadexeclock;
182 extern Channel *_dowaitchan;
184 extern void _procstart(Proc*, void (*fn)(Proc*));
185 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
186 extern void _procexit(void);
187 extern Proc *_threadproc(void);
188 extern void _threadsetproc(Proc*);
189 extern int _threadlock(Lock*, int, ulong);
190 extern void _threadunlock(Lock*, ulong);
191 extern void _pthreadinit(void);
192 extern int _threadspawn(int*, char*, char**, char*);
193 extern int _runthreadspawn(int*, char*, char**, char*);
194 extern void _threadsetupdaemonize(void);
195 extern void _threaddodaemonize(char*);
196 extern void _threadpexit(void);
197 extern void _threaddaemonize(void);
198 extern void *_threadstkalloc(int);
199 extern void _threadstkfree(void*, int);
200 extern void _threadpthreadmain(Proc*, _Thread*);
201 extern void _threadpthreadstart(Proc*, _Thread*);
203 #define USPALIGN(ucp, align) \
204 (void*)((((uintptr)(ucp)->uc_stack.ss_sp+(ucp)->uc_stack.ss_size)-(align))&~((align)-1))