1 #include "threadimpl.h"
3 extern int __isthreaded;
11 _threadunlock(Lock *l, ulong pc)
19 _threadlock(Lock *l, int block, ulong pc)
31 /* a thousand times pretty fast */
32 for(i=0; i<1000; i++){
37 /* now nice and slow */
38 for(i=0; i<1000; i++){
54 volatile long access_lock;
55 volatile long lock_owner;
61 _spinlock(spinlock_t *lk)
63 lock((Lock*)&lk->access_lock);
75 static void /*__attribute__((constructor))*/
80 memset(&sa, 0, sizeof sa);
82 sigemptyset(&sa.sa_mask);
83 sigaddset(&sa.sa_mask, SIGUSR1);
85 sa.sa_flags = SA_RESTART;
86 sigaction(SIGUSR1, &sa, nil);
90 _procsleep(_Procrendez *r)
97 * Block USR1, set the handler to interrupt system calls,
98 * unlock the vouslock so our waker can wake us,
105 sigprocmask(SIG_SETMASK, nil, &mask);
106 sigaddset(&mask, SIGUSR1);
107 sigprocmask(SIG_SETMASK, &mask, nil);
110 sigdelset(&mask, SIGUSR1);
114 * We're awake. Make USR1 not interrupt system calls.
118 if(r->asleep && r->pid == getpid()){
119 /* Didn't really wake up - signal from something else */
125 _procwakeup(_Procrendez *r)
130 kill(r->pid, SIGUSR1);
135 * process creation and exit
137 typedef struct Stackfree Stackfree;
143 static Lock stacklock;
144 static Stackfree *stackfree;
147 delayfreestack(uchar *stk)
151 sf = (Stackfree*)stk;
154 sf->next = stackfree;
162 Stackfree *sf, *last, *next;
164 if(stackfree==nil || !canlock(&stacklock))
167 for(last=nil,sf=stackfree; sf; last=sf,sf=next){
169 if(sf->pid >= 1 && kill(sf->pid, 0) < 0 && errno == ESRCH){
194 p->osprocid = getpid();
204 _procstart(Proc *p, void (*fn)(Proc*))
211 a = malloc(3*sizeof a[0]);
213 sysfatal("_procstart malloc: %r");
216 sysfatal("_procstart malloc stack: %r");
222 pid = rfork_thread(RFPROC|RFMEM|RFNOWAIT, stk+65536-64, startprocfn, a);
224 fprint(2, "_procstart rfork_thread: %r\n");
229 static char *threadexitsmsg;
231 sigusr2handler(int s)
233 /* fprint(2, "%d usr2 %d\n", time(0), getpid()); */
235 _exits(threadexitsmsg);
239 threadexitsall(char *msg)
241 static int pid[1024];
248 lock(&_threadprocslock);
249 threadexitsmsg = msg;
251 for(p=_threadprocs; p; p=p->next)
252 if(p->osprocid != mypid && p->osprocid >= 1)
253 pid[npid++] = p->osprocid;
254 for(i=0; i<npid; i++)
255 kill(pid[i], SIGUSR2);
256 unlock(&_threadprocslock);
261 * per-process data, indexed by pid
263 * could use modify_ldt and a segment register
264 * to avoid the many calls to getpid(), but i don't
265 * care -- this is compatibility code. linux 2.6 with
266 * nptl is a good enough pthreads to avoid this whole file.
268 typedef struct Perproc Perproc;
276 static Perproc perproc[1024];
277 #define P ((Proc*)-1)
286 h = pid%nelem(perproc);
287 for(i=0; i<nelem(perproc); i++){
288 p = &perproc[(i+h)%nelem(perproc)];
292 print("found 0 at %d (h=%d)\n", (i+h)%nelem(perproc), h);
296 fprint(2, "myperproc %d: cannot find self\n", pid);
309 h = pid%nelem(perproc);
310 for(i=0; i<nelem(perproc); i++){
311 p = &perproc[(i+h)%nelem(perproc)];
312 if(p->pid == pid || p->pid == -1 || p->pid == 0){
318 fprint(2, "newperproc %d: out of procs\n", pid);
326 return myperproc()->proc;
330 _threadsetproc(Proc *p)
335 p->osprocid = getpid();
346 signal(SIGUSR2, sigusr2handler);
357 * FreeBSD 4 and earlier needs the context functions.
360 makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
364 sp = (int*)ucp->uc_stack.ss_sp+ucp->uc_stack.ss_size/4;
366 memmove(sp, &argc+1, argc*sizeof(int));
367 *--sp = 0; /* return address */
368 ucp->uc_mcontext.mc_eip = (long)func;
369 ucp->uc_mcontext.mc_esp = (int)sp;
372 extern int getmcontext(mcontext_t*);
373 extern int setmcontext(mcontext_t*);
376 getcontext(ucontext_t *uc)
378 return getmcontext(&uc->uc_mcontext);
382 setcontext(ucontext_t *uc)
384 setmcontext(&uc->uc_mcontext);
388 swapcontext(ucontext_t *oucp, ucontext_t *ucp)
390 if(getcontext(oucp) == 0)