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(__OpenBSD__)
19 # define mcontext libthread_mcontext
20 # define mcontext_t libthread_mcontext_t
21 # define ucontext libthread_ucontext
22 # define ucontext_t libthread_ucontext_t
23 # if defined __i386__
24 # include "386-ucontext.h"
25 # elif defined __amd64__
26 # include "x86_64-ucontext.h"
27 # else
28 # include "power-ucontext.h"
29 # endif
30 extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
31 #endif
33 #if defined(__arm__)
34 int mygetmcontext(ulong*);
35 void mysetmcontext(const ulong*);
36 #define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0)
37 #define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0)
38 #endif
41 typedef struct Context Context;
42 typedef struct Execjob Execjob;
43 typedef struct Proc Proc;
44 typedef struct _Procrendez _Procrendez;
46 typedef struct Jmp Jmp;
47 struct Jmp
48 {
49 p9jmp_buf b;
50 };
52 enum
53 {
54 STACK = 8192
55 };
57 struct Context
58 {
59 ucontext_t uc;
60 };
62 struct Execjob
63 {
64 int *fd;
65 char *cmd;
66 char **argv;
67 char *dir;
68 Channel *c;
69 };
71 struct _Procrendez
72 {
73 Lock *l;
74 int asleep;
75 #ifdef PLAN9PORT_USING_PTHREADS
76 pthread_cond_t cond;
77 #else
78 int pid;
79 #endif
80 };
82 struct _Thread
83 {
84 _Thread *next;
85 _Thread *prev;
86 _Thread *allnext;
87 _Thread *allprev;
88 Context context;
89 void (*startfn)(void*);
90 void *startarg;
91 uint id;
92 #ifdef PLAN9PORT_USING_PTHREADS
93 pthread_t osprocid;
94 #else
95 int osprocid;
96 #endif
97 uchar *stk;
98 uint stksize;
99 int exiting;
100 Proc *proc;
101 char name[256];
102 char state[256];
103 void *udata;
104 Alt *alt;
105 _Procrendez schedrend;
106 };
108 extern void _procsleep(_Procrendez*);
109 extern void _procwakeup(_Procrendez*);
110 extern void _procwakeupandunlock(_Procrendez*);
112 struct Proc
114 Proc *next;
115 Proc *prev;
116 char msg[128];
117 #ifdef PLAN9PORT_USING_PTHREADS
118 pthread_t osprocid;
119 #else
120 int osprocid;
121 #endif
122 Lock lock;
123 int nswitch;
124 _Thread *thread0;
125 _Thread *thread;
126 _Thread *pinthread;
127 _Threadlist runqueue;
128 _Threadlist idlequeue;
129 _Threadlist allthreads;
130 uint nthread;
131 uint sysproc;
132 _Procrendez runrend;
133 Lock schedlock;
134 _Thread *schedthread;
135 Context schedcontext;
136 void *udata;
137 Jmp sigjmp;
138 int mainproc;
139 };
141 #define proc() _threadproc()
143 extern Proc *_threadprocs;
144 extern Lock _threadprocslock;
145 extern Proc *_threadexecproc;
146 extern Channel *_threadexecchan;
147 extern QLock _threadexeclock;
148 extern Channel *_dowaitchan;
150 extern void _procstart(Proc*, void (*fn)(Proc*));
151 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
152 extern void _procexit(void);
153 extern Proc *_threadproc(void);
154 extern void _threadsetproc(Proc*);
155 extern int _threadlock(Lock*, int, ulong);
156 extern void _threadunlock(Lock*, ulong);
157 extern void _pthreadinit(void);
158 extern int _threadspawn(int*, char*, char**, char*);
159 extern int _runthreadspawn(int*, char*, char**, char*);
160 extern void _threadsetupdaemonize(void);
161 extern void _threaddodaemonize(char*);
162 extern void _threadpexit(void);
163 extern void _threaddaemonize(void);
164 extern void *_threadstkalloc(int);
165 extern void _threadstkfree(void*, int);
166 extern void _threadpthreadmain(Proc*, _Thread*);
167 extern void _threadpthreadstart(Proc*, _Thread*);
169 #define USPALIGN(ucp, align) \
170 (void*)((((uintptr)(ucp)->uc_stack.ss_sp+(ucp)->uc_stack.ss_size)-(align))&~((align)-1))