1 619085f0 2004-12-25 devnull #include "u.h"
2 619085f0 2004-12-25 devnull #include <errno.h>
3 619085f0 2004-12-25 devnull #include "libc.h"
4 619085f0 2004-12-25 devnull #include "thread.h"
5 619085f0 2004-12-25 devnull #include "threadimpl.h"
7 619085f0 2004-12-25 devnull static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
9 619085f0 2004-12-25 devnull static void
10 619085f0 2004-12-25 devnull lockinit(Lock *lk)
12 619085f0 2004-12-25 devnull pthread_mutexattr_t attr;
14 619085f0 2004-12-25 devnull pthread_mutex_lock(&initmutex);
15 619085f0 2004-12-25 devnull if(lk->init == 0){
16 619085f0 2004-12-25 devnull pthread_mutexattr_init(&attr);
17 619085f0 2004-12-25 devnull pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
18 619085f0 2004-12-25 devnull pthread_mutex_init(&lk->mutex, &attr);
19 619085f0 2004-12-25 devnull pthread_mutexattr_destroy(&attr);
20 619085f0 2004-12-25 devnull lk->init = 1;
22 619085f0 2004-12-25 devnull pthread_mutex_unlock(&initmutex);
26 619085f0 2004-12-25 devnull _threadlock(Lock *lk, int block, ulong pc)
30 619085f0 2004-12-25 devnull if(!lk->init)
31 619085f0 2004-12-25 devnull lockinit(lk);
32 619085f0 2004-12-25 devnull if(block){
33 619085f0 2004-12-25 devnull if(pthread_mutex_lock(&lk->mutex) != 0)
35 619085f0 2004-12-25 devnull return 1;
37 619085f0 2004-12-25 devnull r = pthread_mutex_trylock(&lk->mutex);
38 619085f0 2004-12-25 devnull if(r == 0)
39 619085f0 2004-12-25 devnull return 1;
40 619085f0 2004-12-25 devnull if(r == EBUSY)
41 619085f0 2004-12-25 devnull return 0;
43 619085f0 2004-12-25 devnull return 0;
48 619085f0 2004-12-25 devnull _threadunlock(Lock *lk, ulong pc)
50 619085f0 2004-12-25 devnull if(pthread_mutex_unlock(&lk->mutex) != 0)
55 619085f0 2004-12-25 devnull _procsleep(_Procrendez *r)
57 619085f0 2004-12-25 devnull /* r is protected by r->l, which we hold */
58 619085f0 2004-12-25 devnull pthread_cond_init(&r->cond, 0);
59 619085f0 2004-12-25 devnull r->asleep = 1;
60 619085f0 2004-12-25 devnull pthread_cond_wait(&r->cond, &r->l->mutex);
61 619085f0 2004-12-25 devnull pthread_cond_destroy(&r->cond);
62 619085f0 2004-12-25 devnull r->asleep = 0;
66 619085f0 2004-12-25 devnull _procwakeup(_Procrendez *r)
68 619085f0 2004-12-25 devnull if(r->asleep){
69 619085f0 2004-12-25 devnull r->asleep = 0;
70 619085f0 2004-12-25 devnull pthread_cond_signal(&r->cond);
74 73722a8b 2004-12-27 devnull static void
75 73722a8b 2004-12-27 devnull startprocfn(void *v)
77 73722a8b 2004-12-27 devnull void **a;
78 73722a8b 2004-12-27 devnull void (*fn)(void*);
81 73722a8b 2004-12-27 devnull a = (void**)v;
82 73722a8b 2004-12-27 devnull fn = a[0];
83 73722a8b 2004-12-27 devnull p = a[1];
85 4dbefdd4 2004-12-27 devnull p->osprocid = pthread_self();
86 4dbefdd4 2004-12-27 devnull pthread_detach(p->osprocid);
88 73722a8b 2004-12-27 devnull (*fn)(p);
90 73722a8b 2004-12-27 devnull pthread_exit(0);
94 73722a8b 2004-12-27 devnull _procstart(Proc *p, void (*fn)(Proc*))
96 73722a8b 2004-12-27 devnull void **a;
98 73722a8b 2004-12-27 devnull a = malloc(2*sizeof a[0]);
99 73722a8b 2004-12-27 devnull if(a == nil)
100 73722a8b 2004-12-27 devnull sysfatal("_procstart malloc: %r");
101 73722a8b 2004-12-27 devnull a[0] = fn;
102 73722a8b 2004-12-27 devnull a[1] = p;
104 4dbefdd4 2004-12-27 devnull if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
105 619085f0 2004-12-25 devnull fprint(2, "pthread_create: %r\n");
106 619085f0 2004-12-25 devnull abort();
110 619085f0 2004-12-25 devnull static pthread_key_t prockey;
113 619085f0 2004-12-25 devnull _threadproc(void)
115 619085f0 2004-12-25 devnull Proc *p;
117 619085f0 2004-12-25 devnull p = pthread_getspecific(prockey);
118 619085f0 2004-12-25 devnull return p;
122 619085f0 2004-12-25 devnull _threadsetproc(Proc *p)
124 619085f0 2004-12-25 devnull pthread_setspecific(prockey, p);
128 e1dc7e45 2004-12-27 devnull _pthreadinit(void)
130 619085f0 2004-12-25 devnull pthread_key_create(&prockey, 0);