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 _Threadlist runqueue;
142 _Threadlist idlequeue;
143 _Threadlist allthreads;
144 uint nthread;
145 uint sysproc;
146 _Procrendez runrend;
147 Context schedcontext;
148 void *udata;
149 Jmp sigjmp;
150 int mainproc;
151 };
153 #define proc() _threadproc()
154 #define setproc(p) _threadsetproc(p)
156 extern Proc *_threadprocs;
157 extern Lock _threadprocslock;
158 extern Proc *_threadexecproc;
159 extern Channel *_threadexecchan;
160 extern QLock _threadexeclock;
161 extern Channel *_dowaitchan;
163 extern void _procstart(Proc*, void (*fn)(Proc*));
164 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
165 extern void _threadexit(void);
166 extern Proc *_threadproc(void);
167 extern void _threadsetproc(Proc*);
168 extern int _threadlock(Lock*, int, ulong);
169 extern void _threadunlock(Lock*, ulong);
170 extern void _pthreadinit(void);
171 extern int _threadspawn(int*, char*, char**);
172 extern int _runthreadspawn(int*, char*, char**);
173 extern void _threadsetupdaemonize(void);
174 extern void _threaddodaemonize(char*);
175 extern void _threadpexit(void);
176 extern void _threaddaemonize(void);