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 # include <ucontext.h>
10 #endif
11 #include <sys/utsname.h>
12 #include "libc.h"
13 #include "thread.h"
15 #if defined(__FreeBSD__) && __FreeBSD__ < 5
16 extern int getmcontext(mcontext_t*);
17 extern void setmcontext(mcontext_t*);
18 #define setcontext(u) setmcontext(&(u)->uc_mcontext)
19 #define getcontext(u) getmcontext(&(u)->uc_mcontext)
20 extern int swapcontext(ucontext_t*, ucontext_t*);
21 extern void makecontext(ucontext_t*, void(*)(), int, ...);
22 #endif
24 #if defined(__APPLE__)
25 /*
26 * OS X before 10.5 (Leopard) does not provide
27 * swapcontext nor makecontext, so we have to use our own.
28 * In theory, Leopard does provide them, but when we use
29 * them, they seg fault. Maybe we're using them wrong.
30 * So just use our own versions, even on Leopard.
31 */
32 # define mcontext libthread_mcontext
33 # define mcontext_t libthread_mcontext_t
34 # define ucontext libthread_ucontext
35 # define ucontext_t libthread_ucontext_t
36 # define swapcontext libthread_swapcontext
37 # define makecontext libthread_makecontext
38 # if defined(__i386__)
39 # include "386-ucontext.h"
40 # else
41 # include "power-ucontext.h"
42 # endif
43 #endif
45 #if defined(__OpenBSD__)
46 # define mcontext libthread_mcontext
47 # define mcontext_t libthread_mcontext_t
48 # define ucontext libthread_ucontext
49 # define ucontext_t libthread_ucontext_t
50 # if defined __i386__
51 # include "386-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 /* THIS DOES NOT WORK! Don't do this!
59 (At least, not on Solaris. Maybe this is right for Linux,
60 in which case it should say if defined(__linux__) && defined(__sun__),
61 but surely the latter would be defined(__sparc__).
63 #if defined(__sun__)
64 # define mcontext libthread_mcontext
65 # define mcontext_t libthread_mcontext_t
66 # define ucontext libthread_ucontext
67 # define ucontext_t libthread_ucontext_t
68 # include "sparc-ucontext.h"
69 #endif
70 */
72 #if defined(__arm__)
73 int getmcontext(mcontext_t*);
74 void setmcontext(const mcontext_t*);
75 #define setcontext(u) setmcontext(&(u)->uc_mcontext)
76 #define getcontext(u) getmcontext(&(u)->uc_mcontext)
77 #endif
80 typedef struct Context Context;
81 typedef struct Execjob Execjob;
82 typedef struct Proc Proc;
83 typedef struct _Procrendez _Procrendez;
85 typedef struct Jmp Jmp;
86 struct Jmp
87 {
88 p9jmp_buf b;
89 };
91 enum
92 {
93 STACK = 8192
94 };
96 struct Context
97 {
98 ucontext_t uc;
99 };
101 struct Execjob
103 int *fd;
104 char *cmd;
105 char **argv;
106 Channel *c;
107 };
109 struct _Thread
111 _Thread *next;
112 _Thread *prev;
113 _Thread *allnext;
114 _Thread *allprev;
115 Context context;
116 uint id;
117 uchar *stk;
118 uint stksize;
119 int exiting;
120 void (*startfn)(void*);
121 void *startarg;
122 Proc *proc;
123 char name[256];
124 char state[256];
125 void *udata;
126 Alt *alt;
127 };
129 struct _Procrendez
131 Lock *l;
132 int asleep;
133 #ifdef PLAN9PORT_USING_PTHREADS
134 pthread_cond_t cond;
135 #else
136 int pid;
137 #endif
138 };
140 extern void _procsleep(_Procrendez*);
141 extern void _procwakeup(_Procrendez*);
142 extern void _procwakeupandunlock(_Procrendez*);
144 struct Proc
146 Proc *next;
147 Proc *prev;
148 char msg[128];
149 #ifdef PLAN9PORT_USING_PTHREADS
150 pthread_t osprocid;
151 #else
152 int osprocid;
153 #endif
154 Lock lock;
155 int nswitch;
156 _Thread *thread;
157 _Thread *pinthread;
158 _Threadlist runqueue;
159 _Threadlist idlequeue;
160 _Threadlist allthreads;
161 uint nthread;
162 uint sysproc;
163 _Procrendez runrend;
164 Context schedcontext;
165 void *udata;
166 Jmp sigjmp;
167 int mainproc;
168 };
170 #define proc() _threadproc()
171 #define setproc(p) _threadsetproc(p)
173 extern Proc *_threadprocs;
174 extern Lock _threadprocslock;
175 extern Proc *_threadexecproc;
176 extern Channel *_threadexecchan;
177 extern QLock _threadexeclock;
178 extern Channel *_dowaitchan;
180 extern void _procstart(Proc*, void (*fn)(Proc*));
181 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
182 extern void _threadexit(void);
183 extern Proc *_threadproc(void);
184 extern void _threadsetproc(Proc*);
185 extern int _threadlock(Lock*, int, ulong);
186 extern void _threadunlock(Lock*, ulong);
187 extern void _pthreadinit(void);
188 extern int _threadspawn(int*, char*, char**);
189 extern int _runthreadspawn(int*, char*, char**);
190 extern void _threadsetupdaemonize(void);
191 extern void _threaddodaemonize(char*);
192 extern void _threadpexit(void);
193 extern void _threaddaemonize(void);