1 #include "threadimpl.h"
9 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
10 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
14 tm = *localtime(time(0));
15 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
16 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
17 (int)(ns%1000000000)/1000000);
23 extern int _tas(int*);
26 _threadunlock(Lock *l, ulong pc)
34 _threadlock(Lock *l, int block, ulong pc)
38 if(first) {first=0; fmtinstall('\001', timefmt);}
48 /* a thousand times pretty fast */
49 for(i=0; i<1000; i++){
54 /* now increasingly slow */
60 fprint(2, "%\001 %s: lock loop1 %p from %lux\n", argv0, l, pc);
66 fprint(2, "%\001 %s: lock loop2 %p from %lux\n", argv0, l, pc);
72 fprint(2, "%\001 %s: lock loop3 %p from %lux\n", argv0, l, pc);
78 fprint(2, "%\001 %s: lock loop4 %p from %lux\n", argv0, l, pc);
84 fprint(2, "%\001 %s: lock loop5 %p from %lux\n", argv0, l, pc);
85 for(i=0; i<1000; i++){
90 fprint(2, "%\001 %s: lock loop6 %p from %lux\n", argv0, l, pc);
106 static void /*__attribute__((constructor))*/
111 memset(&sa, 0, sizeof sa);
113 sigemptyset(&sa.sa_mask);
114 sigaddset(&sa.sa_mask, SIGUSR1);
116 sa.sa_flags = SA_RESTART;
117 sigaction(SIGUSR1, &sa, nil);
121 _procsleep(_Procrendez *r)
128 * Block USR1, set the handler to interrupt system calls,
129 * unlock the vouslock so our waker can wake us,
136 sigprocmask(SIG_SETMASK, nil, &mask);
137 sigaddset(&mask, SIGUSR1);
138 sigprocmask(SIG_SETMASK, &mask, nil);
141 sigdelset(&mask, SIGUSR1);
145 * We're awake. Make USR1 not interrupt system calls.
149 if(r->asleep && r->pid == getpid()){
150 /* Didn't really wake up - signal from something else */
156 _procwakeupandunlock(_Procrendez *r)
173 * process creation and exit
175 typedef struct Stackfree Stackfree;
182 static Lock stacklock;
183 static Stackfree *stackfree;
186 delayfreestack(uchar *stk, int pid, int pid1)
190 sf = (Stackfree*)stk;
194 sf->next = stackfree;
202 Stackfree *sf, *last, *next;
204 if(stackfree==nil || !canlock(&stacklock))
207 for(last=nil,sf=stackfree; sf; last=sf,sf=next){
209 if(sf->pid >= 1 && kill(sf->pid, 0) < 0 && errno == ESRCH)
210 if(sf->pid1 >= 1 && kill(sf->pid1, 0) < 0 && errno == ESRCH){
242 delayfreestack(stk, pid0, pid1);
248 * indirect through here so that parent need not wait for child zombie
250 * slight race - if child exits and then another process starts before we
251 * manage to exit, we'll be running on a freed stack.
261 a[4] = (void*)getpid();
262 *kidpid = clone(startprocfn, a[2]+65536-512, CLONE_VM|CLONE_FILES, a);
268 _procstart(Proc *p, void (*fn)(Proc*))
272 int pid, kidpid, status;
275 a = malloc(5*sizeof a[0]);
277 sysfatal("_procstart malloc: %r");
280 sysfatal("_procstart malloc stack: %r");
288 pid = clone(trampnowait, stk+65536-16, CLONE_VM|CLONE_FILES, a);
290 if(wait4(pid, &status, __WALL, 0) < 0)
291 fprint(2, "ffork wait4: %r\n");
292 if(pid < 0 || kidpid < 0){
293 fprint(2, "_procstart clone: %r\n");
298 static char *threadexitsmsg;
300 sigusr2handler(int s)
302 /* fprint(2, "%d usr2 %d\n", time(0), getpid()); */
304 _exits(threadexitsmsg);
308 threadexitsall(char *msg)
310 static int pid[1024];
318 * Only one guy, ever, gets to run this.
319 * If two guys do it, inevitably they end up
320 * tripping over each other in the underlying
321 * C library exit() implementation, which is
322 * trying to run the atexit handlers and apparently
323 * not thread safe. This has been observed on
324 * both Linux and OpenBSD. Sigh.
328 if(!canlock(&onelock))
329 _exits(threadexitsmsg);
330 threadexitsmsg = msg;
334 lock(&_threadprocslock);
336 for(p=_threadprocs; p; p=p->next)
337 if(p->osprocid != mypid && p->osprocid >= 1)
338 pid[npid++] = p->osprocid;
339 for(i=0; i<npid; i++)
340 kill(pid[i], SIGUSR2);
341 unlock(&_threadprocslock);
346 * per-process data, indexed by pid
348 * could use modify_ldt and a segment register
349 * to avoid the many calls to getpid(), but i don't
350 * care -- this is compatibility code. linux 2.6 with
351 * nptl is a good enough pthreads to avoid this whole file.
353 typedef struct Perproc Perproc;
361 static Perproc perproc[1024];
362 #define P ((Proc*)-1)
371 h = pid%nelem(perproc);
372 for(i=0; i<nelem(perproc); i++){
373 p = &perproc[(i+h)%nelem(perproc)];
377 print("found 0 at %d (h=%d)\n", (i+h)%nelem(perproc), h);
381 fprint(2, "myperproc %d (%s): cannot find self\n", pid, argv0);
394 h = pid%nelem(perproc);
395 for(i=0; i<nelem(perproc); i++){
396 p = &perproc[(i+h)%nelem(perproc)];
397 if(p->pid == pid || p->pid == -1 || p->pid == 0){
403 fprint(2, "newperproc %d: out of procs\n", pid);
411 return myperproc()->proc;
415 _threadsetproc(Proc *p)
420 p->osprocid = getpid();
430 signal(SIGUSR2, sigusr2handler);
441 makecontext(ucontext_t *uc, void (*fn)(void), int argc, ...)
446 sp = (int*)uc->uc_stack.ss_sp+uc->uc_stack.ss_size/4;
448 for(i=0; i<4 && i<argc; i++)
449 uc->uc_mcontext.gregs[i] = va_arg(arg, uint);
451 uc->uc_mcontext.gregs[13] = (uint)sp;
452 uc->uc_mcontext.gregs[14] = (uint)fn;
456 swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
458 if(getcontext(oucp) == 0)