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 _Thread
107 _Thread *next;
108 _Thread *prev;
109 _Thread *allnext;
110 _Thread *allprev;
111 Context context;
112 void (*startfn)(void*);
113 void *startarg;
114 uint id;
115 uchar *stk;
116 uint stksize;
117 int exiting;
118 Proc *proc;
119 char name[256];
120 char state[256];
121 void *udata;
122 Alt *alt;
123 };
125 struct _Procrendez
127 Lock *l;
128 int asleep;
129 #ifdef PLAN9PORT_USING_PTHREADS
130 pthread_cond_t cond;
131 #else
132 int pid;
133 #endif
134 };
136 extern void _procsleep(_Procrendez*);
137 extern void _procwakeup(_Procrendez*);
138 extern void _procwakeupandunlock(_Procrendez*);
140 struct Proc
142 Proc *next;
143 Proc *prev;
144 char msg[128];
145 #ifdef PLAN9PORT_USING_PTHREADS
146 pthread_t osprocid;
147 #else
148 int osprocid;
149 #endif
150 Lock lock;
151 int nswitch;
152 _Thread *thread;
153 _Thread *pinthread;
154 _Threadlist runqueue;
155 _Threadlist idlequeue;
156 _Threadlist allthreads;
157 uint nthread;
158 uint sysproc;
159 _Procrendez runrend;
160 Context schedcontext;
161 void *udata;
162 Jmp sigjmp;
163 int mainproc;
164 };
166 #define proc() _threadproc()
168 extern Proc *_threadprocs;
169 extern Lock _threadprocslock;
170 extern Proc *_threadexecproc;
171 extern Channel *_threadexecchan;
172 extern QLock _threadexeclock;
173 extern Channel *_dowaitchan;
175 extern void _procstart(Proc*, void (*fn)(Proc*));
176 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
177 extern void _procexit(void);
178 extern Proc *_threadproc(void);
179 extern void _threadsetproc(Proc*);
180 extern int _threadlock(Lock*, int, ulong);
181 extern void _threadunlock(Lock*, ulong);
182 extern void _pthreadinit(void);
183 extern int _threadspawn(int*, char*, char**, char*);
184 extern int _runthreadspawn(int*, char*, char**, char*);
185 extern void _threadsetupdaemonize(void);
186 extern void _threaddodaemonize(char*);
187 extern void _threadpexit(void);
188 extern void _threaddaemonize(void);
189 extern void *_threadstkalloc(int);
190 extern void _threadstkfree(void*, int);
192 #define USPALIGN(ucp, align) \
193 (void*)((((uintptr)(ucp)->uc_stack.ss_sp+(ucp)->uc_stack.ss_size)-(align))&~((align)-1))