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 # define mcontext libthread_mcontext
26 # define mcontext_t libthread_mcontext_t
27 # define ucontext libthread_ucontext
28 # define ucontext_t libthread_ucontext_t
29 # if defined(__i386__)
30 # include "386-ucontext.h"
31 # else
32 # include "power-ucontext.h"
33 # endif
34 #endif
36 #if defined(__OpenBSD__)
37 # define mcontext libthread_mcontext
38 # define mcontext_t libthread_mcontext_t
39 # define ucontext libthread_ucontext
40 # define ucontext_t libthread_ucontext_t
41 # if defined __i386__
42 # include "386-ucontext.h"
43 # else
44 # include "power-ucontext.h"
45 # endif
46 extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
47 #endif
49 #if defined(__sun__)
50 # define mcontext libthread_mcontext
51 # define mcontext_t libthread_mcontext_t
52 # define ucontext libthread_ucontext
53 # define ucontext_t libthread_ucontext_t
54 # include "sparc-ucontext.h"
55 #endif
57 #if defined(__arm__)
58 int getmcontext(mcontext_t*);
59 void setmcontext(const mcontext_t*);
60 #define setcontext(u) setmcontext(&(u)->uc_mcontext)
61 #define getcontext(u) getmcontext(&(u)->uc_mcontext)
62 #endif
65 typedef struct Context Context;
66 typedef struct Execjob Execjob;
67 typedef struct Proc Proc;
68 typedef struct _Procrendez _Procrendez;
70 typedef struct Jmp Jmp;
71 struct Jmp
72 {
73 p9jmp_buf b;
74 };
76 enum
77 {
78 STACK = 8192
79 };
81 struct Context
82 {
83 ucontext_t uc;
84 };
86 struct Execjob
87 {
88 int *fd;
89 char *cmd;
90 char **argv;
91 Channel *c;
92 };
94 struct _Thread
95 {
96 _Thread *next;
97 _Thread *prev;
98 _Thread *allnext;
99 _Thread *allprev;
100 Context context;
101 uint id;
102 uchar *stk;
103 uint stksize;
104 int exiting;
105 void (*startfn)(void*);
106 void *startarg;
107 Proc *proc;
108 char name[256];
109 char state[256];
110 void *udata;
111 };
113 struct _Procrendez
115 Lock *l;
116 int asleep;
117 #ifdef PLAN9PORT_USING_PTHREADS
118 pthread_cond_t cond;
119 #else
120 int pid;
121 #endif
122 };
124 extern void _procsleep(_Procrendez*);
125 extern void _procwakeup(_Procrendez*);
126 extern void _procwakeupandunlock(_Procrendez*);
128 struct Proc
130 Proc *next;
131 Proc *prev;
132 char msg[128];
133 #ifdef PLAN9PORT_USING_PTHREADS
134 pthread_t osprocid;
135 #else
136 int osprocid;
137 #endif
138 Lock lock;
139 int nswitch;
140 _Thread *thread;
141 _Thread *pinthread;
142 _Threadlist runqueue;
143 _Threadlist idlequeue;
144 _Threadlist allthreads;
145 uint nthread;
146 uint sysproc;
147 _Procrendez runrend;
148 Context schedcontext;
149 void *udata;
150 Jmp sigjmp;
151 int mainproc;
152 };
154 #define proc() _threadproc()
155 #define setproc(p) _threadsetproc(p)
157 extern Proc *_threadprocs;
158 extern Lock _threadprocslock;
159 extern Proc *_threadexecproc;
160 extern Channel *_threadexecchan;
161 extern QLock _threadexeclock;
162 extern Channel *_dowaitchan;
164 extern void _procstart(Proc*, void (*fn)(Proc*));
165 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
166 extern void _threadexit(void);
167 extern Proc *_threadproc(void);
168 extern void _threadsetproc(Proc*);
169 extern int _threadlock(Lock*, int, ulong);
170 extern void _threadunlock(Lock*, ulong);
171 extern void _pthreadinit(void);
172 extern int _threadspawn(int*, char*, char**);
173 extern int _runthreadspawn(int*, char*, char**);
174 extern void _threadsetupdaemonize(void);
175 extern void _threaddodaemonize(char*);
176 extern void _threadpexit(void);
177 extern void _threaddaemonize(void);