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 # else
44 # include "power-ucontext.h"
45 # endif
46 #endif
48 #if defined(__OpenBSD__)
49 # define mcontext libthread_mcontext
50 # define mcontext_t libthread_mcontext_t
51 # define ucontext libthread_ucontext
52 # define ucontext_t libthread_ucontext_t
53 # if defined __i386__
54 # include "386-ucontext.h"
55 # else
56 # include "power-ucontext.h"
57 # endif
58 extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
59 #endif
61 /* THIS DOES NOT WORK! Don't do this!
62 (At least, not on Solaris. Maybe this is right for Linux,
63 in which case it should say if defined(__linux__) && defined(__sun__),
64 but surely the latter would be defined(__sparc__).
66 #if defined(__sun__)
67 # define mcontext libthread_mcontext
68 # define mcontext_t libthread_mcontext_t
69 # define ucontext libthread_ucontext
70 # define ucontext_t libthread_ucontext_t
71 # include "sparc-ucontext.h"
72 #endif
73 */
75 #if defined(__arm__)
76 int getmcontext(mcontext_t*);
77 void setmcontext(const mcontext_t*);
78 #define setcontext(u) setmcontext(&(u)->uc_mcontext)
79 #define getcontext(u) getmcontext(&(u)->uc_mcontext)
80 #endif
83 typedef struct Context Context;
84 typedef struct Execjob Execjob;
85 typedef struct Proc Proc;
86 typedef struct _Procrendez _Procrendez;
88 typedef struct Jmp Jmp;
89 struct Jmp
90 {
91 p9jmp_buf b;
92 };
94 enum
95 {
96 STACK = 8192
97 };
99 struct Context
101 ucontext_t uc;
102 };
104 struct Execjob
106 int *fd;
107 char *cmd;
108 char **argv;
109 Channel *c;
110 };
112 struct _Thread
114 _Thread *next;
115 _Thread *prev;
116 _Thread *allnext;
117 _Thread *allprev;
118 Context context;
119 uint id;
120 uchar *stk;
121 uint stksize;
122 int exiting;
123 void (*startfn)(void*);
124 void *startarg;
125 Proc *proc;
126 char name[256];
127 char state[256];
128 void *udata;
129 Alt *alt;
130 };
132 struct _Procrendez
134 Lock *l;
135 int asleep;
136 #ifdef PLAN9PORT_USING_PTHREADS
137 pthread_cond_t cond;
138 #else
139 int pid;
140 #endif
141 };
143 extern void _procsleep(_Procrendez*);
144 extern void _procwakeup(_Procrendez*);
145 extern void _procwakeupandunlock(_Procrendez*);
147 struct Proc
149 Proc *next;
150 Proc *prev;
151 char msg[128];
152 #ifdef PLAN9PORT_USING_PTHREADS
153 pthread_t osprocid;
154 #else
155 int osprocid;
156 #endif
157 Lock lock;
158 int nswitch;
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 Context schedcontext;
168 void *udata;
169 Jmp sigjmp;
170 int mainproc;
171 };
173 #define proc() _threadproc()
174 #define setproc(p) _threadsetproc(p)
176 extern Proc *_threadprocs;
177 extern Lock _threadprocslock;
178 extern Proc *_threadexecproc;
179 extern Channel *_threadexecchan;
180 extern QLock _threadexeclock;
181 extern Channel *_dowaitchan;
183 extern void _procstart(Proc*, void (*fn)(Proc*));
184 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
185 extern void _threadexit(void);
186 extern Proc *_threadproc(void);
187 extern void _threadsetproc(Proc*);
188 extern int _threadlock(Lock*, int, ulong);
189 extern void _threadunlock(Lock*, ulong);
190 extern void _pthreadinit(void);
191 extern int _threadspawn(int*, char*, char**);
192 extern int _runthreadspawn(int*, char*, char**);
193 extern void _threadsetupdaemonize(void);
194 extern void _threaddodaemonize(char*);
195 extern void _threadpexit(void);
196 extern void _threaddaemonize(void);