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