1 f1e8706d 2004-12-27 devnull #include "u.h"
2 f1e8706d 2004-12-27 devnull #include <errno.h>
3 f1e8706d 2004-12-27 devnull #include <sys/types.h>
4 f1e8706d 2004-12-27 devnull #include <sys/wait.h>
5 f1e8706d 2004-12-27 devnull #include <sched.h>
6 f1e8706d 2004-12-27 devnull #include <signal.h>
7 f1e8706d 2004-12-27 devnull #include "libc.h"
8 f1e8706d 2004-12-27 devnull #include "thread.h"
9 f1e8706d 2004-12-27 devnull #include "threadimpl.h"
12 f1e8706d 2004-12-27 devnull * spin locks
14 f1e8706d 2004-12-27 devnull extern int _tas(int*);
17 f1e8706d 2004-12-27 devnull _threadunlock(Lock *l, ulong pc)
19 f1e8706d 2004-12-27 devnull USED(pc);
21 f1e8706d 2004-12-27 devnull l->held = 0;
25 f1e8706d 2004-12-27 devnull _threadlock(Lock *l, int block, ulong pc)
29 f1e8706d 2004-12-27 devnull USED(pc);
31 f1e8706d 2004-12-27 devnull /* once fast */
32 f1e8706d 2004-12-27 devnull if(!_tas(&l->held))
33 f1e8706d 2004-12-27 devnull return 1;
34 f1e8706d 2004-12-27 devnull if(!block)
35 f1e8706d 2004-12-27 devnull return 0;
37 f1e8706d 2004-12-27 devnull /* a thousand times pretty fast */
38 f1e8706d 2004-12-27 devnull for(i=0; i<1000; i++){
39 f1e8706d 2004-12-27 devnull if(!_tas(&l->held))
40 f1e8706d 2004-12-27 devnull return 1;
41 f1e8706d 2004-12-27 devnull sched_yield();
43 f1e8706d 2004-12-27 devnull /* now nice and slow */
44 f1e8706d 2004-12-27 devnull for(i=0; i<1000; i++){
45 f1e8706d 2004-12-27 devnull if(!_tas(&l->held))
46 f1e8706d 2004-12-27 devnull return 1;
47 f1e8706d 2004-12-27 devnull usleep(100*1000);
49 f1e8706d 2004-12-27 devnull /* take your time */
50 f1e8706d 2004-12-27 devnull while(_tas(&l->held))
51 f1e8706d 2004-12-27 devnull usleep(1000*1000);
52 f1e8706d 2004-12-27 devnull return 1;
56 f1e8706d 2004-12-27 devnull * sleep and wakeup
58 f1e8706d 2004-12-27 devnull static void
59 f1e8706d 2004-12-27 devnull ign(int x)
64 f1e8706d 2004-12-27 devnull static void /*__attribute__((constructor))*/
65 f1e8706d 2004-12-27 devnull ignusr1(int restart)
67 f1e8706d 2004-12-27 devnull struct sigaction sa;
69 f1e8706d 2004-12-27 devnull memset(&sa, 0, sizeof sa);
70 f1e8706d 2004-12-27 devnull sa.sa_handler = ign;
71 f1e8706d 2004-12-27 devnull sigemptyset(&sa.sa_mask);
72 f1e8706d 2004-12-27 devnull sigaddset(&sa.sa_mask, SIGUSR1);
73 f1e8706d 2004-12-27 devnull if(restart)
74 f1e8706d 2004-12-27 devnull sa.sa_flags = SA_RESTART;
75 f1e8706d 2004-12-27 devnull sigaction(SIGUSR1, &sa, nil);
79 f1e8706d 2004-12-27 devnull _procsleep(_Procrendez *r)
81 f1e8706d 2004-12-27 devnull sigset_t mask;
84 f1e8706d 2004-12-27 devnull * Go to sleep.
86 f1e8706d 2004-12-27 devnull * Block USR1, set the handler to interrupt system calls,
87 f1e8706d 2004-12-27 devnull * unlock the vouslock so our waker can wake us,
88 f1e8706d 2004-12-27 devnull * and then suspend.
90 f1e8706d 2004-12-27 devnull r->asleep = 1;
91 f1e8706d 2004-12-27 devnull r->pid = getpid();
93 f1e8706d 2004-12-27 devnull sigprocmask(SIG_SETMASK, nil, &mask);
94 f1e8706d 2004-12-27 devnull sigaddset(&mask, SIGUSR1);
95 f1e8706d 2004-12-27 devnull sigprocmask(SIG_SETMASK, &mask, nil);
96 f1e8706d 2004-12-27 devnull ignusr1(0);
97 f1e8706d 2004-12-27 devnull unlock(r->l);
98 f1e8706d 2004-12-27 devnull sigdelset(&mask, SIGUSR1);
99 f1e8706d 2004-12-27 devnull sigsuspend(&mask);
102 f1e8706d 2004-12-27 devnull * We're awake. Make USR1 not interrupt system calls.
104 f1e8706d 2004-12-27 devnull ignusr1(1);
105 f1e8706d 2004-12-27 devnull assert(r->asleep == 0);
106 f1e8706d 2004-12-27 devnull lock(r->l);
110 f1e8706d 2004-12-27 devnull _procwakeup(_Procrendez *r)
112 f1e8706d 2004-12-27 devnull if(r->asleep){
113 f1e8706d 2004-12-27 devnull r->asleep = 0;
114 f1e8706d 2004-12-27 devnull assert(r->pid >= 1);
115 f1e8706d 2004-12-27 devnull kill(r->pid, SIGUSR1);
120 f1e8706d 2004-12-27 devnull * process creation and exit
122 f1e8706d 2004-12-27 devnull typedef struct Stackfree Stackfree;
123 f1e8706d 2004-12-27 devnull struct Stackfree
125 f1e8706d 2004-12-27 devnull Stackfree *next;
126 f1e8706d 2004-12-27 devnull int pid;
128 f1e8706d 2004-12-27 devnull static Lock stacklock;
129 f1e8706d 2004-12-27 devnull static Stackfree *stackfree;
131 f1e8706d 2004-12-27 devnull static void
132 f1e8706d 2004-12-27 devnull delayfreestack(uchar *stk)
134 f1e8706d 2004-12-27 devnull Stackfree *sf;
136 f1e8706d 2004-12-27 devnull sf = (Stackfree*)stk;
137 f1e8706d 2004-12-27 devnull sf->pid = getpid();
138 f1e8706d 2004-12-27 devnull lock(&stacklock);
139 f1e8706d 2004-12-27 devnull sf->next = stackfree;
140 f1e8706d 2004-12-27 devnull stackfree = sf;
141 f1e8706d 2004-12-27 devnull unlock(&stacklock);
144 f1e8706d 2004-12-27 devnull static void
145 f1e8706d 2004-12-27 devnull dofreestacks(void)
147 f1e8706d 2004-12-27 devnull Stackfree *sf, *last, *next;
149 f1e8706d 2004-12-27 devnull if(stackfree==nil || !canlock(&stacklock))
152 f1e8706d 2004-12-27 devnull for(last=nil,sf=stackfree; sf; last=sf,sf=next){
153 f1e8706d 2004-12-27 devnull next = sf->next;
154 f1e8706d 2004-12-27 devnull if(sf->pid >= 1 && kill(sf->pid, 0) < 0 && errno == ESRCH){
155 f1e8706d 2004-12-27 devnull free(sf);
156 f1e8706d 2004-12-27 devnull last->next = next;
157 f1e8706d 2004-12-27 devnull sf = last;
162 f1e8706d 2004-12-27 devnull static int
163 f1e8706d 2004-12-27 devnull startprocfn(void *v)
165 f1e8706d 2004-12-27 devnull void **a;
166 f1e8706d 2004-12-27 devnull uchar *stk;
167 f1e8706d 2004-12-27 devnull void (*fn)(void*);
168 f1e8706d 2004-12-27 devnull Proc *p;
170 f1e8706d 2004-12-27 devnull a = (void**)v;
171 f1e8706d 2004-12-27 devnull fn = a[0];
172 f1e8706d 2004-12-27 devnull p = a[1];
173 f1e8706d 2004-12-27 devnull stk = a[2];
174 f1e8706d 2004-12-27 devnull free(a);
175 f1e8706d 2004-12-27 devnull p->osprocid = getpid();
177 f1e8706d 2004-12-27 devnull (*fn)(p);
179 f1e8706d 2004-12-27 devnull delayfreestack(stk);
180 f1e8706d 2004-12-27 devnull _exit(0);
181 f1e8706d 2004-12-27 devnull return 0;
185 f1e8706d 2004-12-27 devnull * indirect through here so that parent need not wait for child zombie
187 f1e8706d 2004-12-27 devnull * slight race - if child exits and then another process starts before we
188 f1e8706d 2004-12-27 devnull * manage to exit, we'll be running on a freed stack.
190 f1e8706d 2004-12-27 devnull static int
191 f1e8706d 2004-12-27 devnull trampnowait(void *v)
193 f1e8706d 2004-12-27 devnull void **a;
195 f1e8706d 2004-12-27 devnull a = (void*)v;
196 f1e8706d 2004-12-27 devnull *(int*)a[3] = clone(startprocfn, a[2]+65536-512, CLONE_VM|CLONE_FILES, a);
197 f1e8706d 2004-12-27 devnull _exit(0);
198 f1e8706d 2004-12-27 devnull return 0;
202 f1e8706d 2004-12-27 devnull _procstart(Proc *p, void (*fn)(Proc*))
204 f1e8706d 2004-12-27 devnull void **a;
205 f1e8706d 2004-12-27 devnull uchar *stk;
206 f1e8706d 2004-12-27 devnull int pid, kidpid, status;
208 f1e8706d 2004-12-27 devnull dofreestacks();
209 f1e8706d 2004-12-27 devnull a = malloc(4*sizeof a[0]);
210 f1e8706d 2004-12-27 devnull if(a == nil)
211 f1e8706d 2004-12-27 devnull sysfatal("_procstart malloc: %r");
212 f1e8706d 2004-12-27 devnull stk = malloc(65536);
213 f1e8706d 2004-12-27 devnull if(stk == nil)
214 f1e8706d 2004-12-27 devnull sysfatal("_procstart malloc stack: %r");
216 f1e8706d 2004-12-27 devnull a[0] = fn;
217 f1e8706d 2004-12-27 devnull a[1] = p;
218 f1e8706d 2004-12-27 devnull a[2] = stk;
219 f1e8706d 2004-12-27 devnull a[3] = &kidpid;
220 f1e8706d 2004-12-27 devnull kidpid = -1;
222 f1e8706d 2004-12-27 devnull pid = clone(trampnowait, stk+65536-16, CLONE_VM|CLONE_FILES, a);
223 f1e8706d 2004-12-27 devnull if(pid > 0)
224 f1e8706d 2004-12-27 devnull if(wait4(pid, &status, __WALL, 0) < 0)
225 f1e8706d 2004-12-27 devnull fprint(2, "ffork wait4: %r\n");
226 f1e8706d 2004-12-27 devnull if(pid < 0 || kidpid < 0){
227 f1e8706d 2004-12-27 devnull fprint(2, "_procstart clone: %r\n");
228 f1e8706d 2004-12-27 devnull abort();
232 f1e8706d 2004-12-27 devnull static char *threadexitsmsg;
234 f1e8706d 2004-12-27 devnull sigusr2handler(int s)
236 f1e8706d 2004-12-27 devnull if(threadexitsmsg)
237 f1e8706d 2004-12-27 devnull _exits(threadexitsmsg);
241 f1e8706d 2004-12-27 devnull threadexitsall(char *msg)
243 f1e8706d 2004-12-27 devnull static int pid[1024];
244 f1e8706d 2004-12-27 devnull int i, npid, mypid;
245 f1e8706d 2004-12-27 devnull Proc *p;
247 f1e8706d 2004-12-27 devnull if(msg == nil)
248 f1e8706d 2004-12-27 devnull msg = "";
249 f1e8706d 2004-12-27 devnull mypid = getpid();
250 f1e8706d 2004-12-27 devnull lock(&_threadprocslock);
251 f1e8706d 2004-12-27 devnull threadexitsmsg = msg;
252 f1e8706d 2004-12-27 devnull npid = 0;
253 f1e8706d 2004-12-27 devnull for(p=_threadprocs; p; p=p->next)
254 f1e8706d 2004-12-27 devnull if(p->osprocid != mypid && p->osprocid >= 1)
255 f1e8706d 2004-12-27 devnull pid[npid++] = p->osprocid;
256 f1e8706d 2004-12-27 devnull for(i=0; i<npid; i++)
257 f1e8706d 2004-12-27 devnull kill(pid[i], SIGUSR2);
258 f1e8706d 2004-12-27 devnull unlock(&_threadprocslock);
259 f1e8706d 2004-12-27 devnull exits(msg);
263 f1e8706d 2004-12-27 devnull * per-process data, indexed by pid
265 f1e8706d 2004-12-27 devnull * could use modify_ldt and a segment register
266 f1e8706d 2004-12-27 devnull * to avoid the many calls to getpid(), but i don't
267 f1e8706d 2004-12-27 devnull * care -- this is compatibility code. linux 2.6 with
268 f1e8706d 2004-12-27 devnull * nptl is a good enough pthreads to avoid this whole file.
270 f1e8706d 2004-12-27 devnull typedef struct Perproc Perproc;
271 f1e8706d 2004-12-27 devnull struct Perproc
273 f1e8706d 2004-12-27 devnull int pid;
274 f1e8706d 2004-12-27 devnull Proc *proc;
277 f1e8706d 2004-12-27 devnull static Lock perlock;
278 f1e8706d 2004-12-27 devnull static Perproc perproc[1024];
279 f1e8706d 2004-12-27 devnull #define P ((Proc*)-1)
281 f1e8706d 2004-12-27 devnull static Perproc*
282 f1e8706d 2004-12-27 devnull myperproc(void)
284 f1e8706d 2004-12-27 devnull int i, pid, h;
285 f1e8706d 2004-12-27 devnull Perproc *p;
287 f1e8706d 2004-12-27 devnull pid = getpid();
288 f1e8706d 2004-12-27 devnull h = pid%nelem(perproc);
289 f1e8706d 2004-12-27 devnull for(i=0; i<nelem(perproc); i++){
290 f1e8706d 2004-12-27 devnull p = &perproc[(i+h)%nelem(perproc)];
291 f1e8706d 2004-12-27 devnull if(p->pid == pid)
292 f1e8706d 2004-12-27 devnull return p;
293 f1e8706d 2004-12-27 devnull if(p->pid == 0){
294 f1e8706d 2004-12-27 devnull print("found 0 at %d (h=%d)\n", (i+h)%nelem(perproc), h);
298 f1e8706d 2004-12-27 devnull fprint(2, "myperproc %d: cannot find self\n", pid);
299 f1e8706d 2004-12-27 devnull abort();
300 f1e8706d 2004-12-27 devnull return nil;
303 f1e8706d 2004-12-27 devnull static Perproc*
304 f1e8706d 2004-12-27 devnull newperproc(void)
306 f1e8706d 2004-12-27 devnull int i, pid, h;
307 f1e8706d 2004-12-27 devnull Perproc *p;
309 f1e8706d 2004-12-27 devnull lock(&perlock);
310 f1e8706d 2004-12-27 devnull pid = getpid();
311 f1e8706d 2004-12-27 devnull h = pid%nelem(perproc);
312 f1e8706d 2004-12-27 devnull for(i=0; i<nelem(perproc); i++){
313 f1e8706d 2004-12-27 devnull p = &perproc[(i+h)%nelem(perproc)];
314 f1e8706d 2004-12-27 devnull if(p->pid == pid || p->pid == -1 || p->pid == 0){
315 f1e8706d 2004-12-27 devnull p->pid = pid;
316 f1e8706d 2004-12-27 devnull unlock(&perlock);
317 f1e8706d 2004-12-27 devnull return p;
320 f1e8706d 2004-12-27 devnull fprint(2, "newperproc %d: out of procs\n", pid);
321 f1e8706d 2004-12-27 devnull abort();
322 f1e8706d 2004-12-27 devnull return nil;
326 f1e8706d 2004-12-27 devnull _threadproc(void)
328 f1e8706d 2004-12-27 devnull return myperproc()->proc;
332 f1e8706d 2004-12-27 devnull _threadsetproc(Proc *p)
334 f1e8706d 2004-12-27 devnull Perproc *pp;
336 f1e8706d 2004-12-27 devnull p->osprocid = getpid();
337 f1e8706d 2004-12-27 devnull pp = newperproc();
338 f1e8706d 2004-12-27 devnull pp->proc = p;
339 f1e8706d 2004-12-27 devnull if(p == nil)
340 f1e8706d 2004-12-27 devnull pp->pid = -1;
344 f1e8706d 2004-12-27 devnull _pthreadinit(void)
346 f1e8706d 2004-12-27 devnull signal(SIGUSR2, sigusr2handler);