1 619085f0 2004-12-25 devnull #include "threadimpl.h"
3 3d867865 2005-01-18 devnull #undef exits
4 3d867865 2005-01-18 devnull #undef _exits
6 619085f0 2004-12-25 devnull static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
8 619085f0 2004-12-25 devnull static void
9 619085f0 2004-12-25 devnull lockinit(Lock *lk)
11 619085f0 2004-12-25 devnull pthread_mutexattr_t attr;
13 619085f0 2004-12-25 devnull pthread_mutex_lock(&initmutex);
14 619085f0 2004-12-25 devnull if(lk->init == 0){
15 619085f0 2004-12-25 devnull pthread_mutexattr_init(&attr);
16 619085f0 2004-12-25 devnull pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
17 619085f0 2004-12-25 devnull pthread_mutex_init(&lk->mutex, &attr);
18 619085f0 2004-12-25 devnull pthread_mutexattr_destroy(&attr);
19 619085f0 2004-12-25 devnull lk->init = 1;
21 619085f0 2004-12-25 devnull pthread_mutex_unlock(&initmutex);
25 619085f0 2004-12-25 devnull _threadlock(Lock *lk, int block, ulong pc)
29 619085f0 2004-12-25 devnull if(!lk->init)
30 619085f0 2004-12-25 devnull lockinit(lk);
31 619085f0 2004-12-25 devnull if(block){
32 619085f0 2004-12-25 devnull if(pthread_mutex_lock(&lk->mutex) != 0)
34 619085f0 2004-12-25 devnull return 1;
36 619085f0 2004-12-25 devnull r = pthread_mutex_trylock(&lk->mutex);
37 619085f0 2004-12-25 devnull if(r == 0)
38 619085f0 2004-12-25 devnull return 1;
39 619085f0 2004-12-25 devnull if(r == EBUSY)
40 619085f0 2004-12-25 devnull return 0;
42 619085f0 2004-12-25 devnull return 0;
47 619085f0 2004-12-25 devnull _threadunlock(Lock *lk, ulong pc)
49 619085f0 2004-12-25 devnull if(pthread_mutex_unlock(&lk->mutex) != 0)
53 162d0d5c 2020-05-18 rsc /* note: _procsleep can have spurious wakeups, like pthread_cond_wait */
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 162d0d5c 2020-05-18 rsc if(pthread_cond_wait(&r->cond, &r->l->mutex) != 0)
61 162d0d5c 2020-05-18 rsc sysfatal("pthread_cond_wait: %r");
62 619085f0 2004-12-25 devnull pthread_cond_destroy(&r->cond);
63 619085f0 2004-12-25 devnull r->asleep = 0;
67 619085f0 2004-12-25 devnull _procwakeup(_Procrendez *r)
69 619085f0 2004-12-25 devnull if(r->asleep){
70 619085f0 2004-12-25 devnull r->asleep = 0;
71 619085f0 2004-12-25 devnull pthread_cond_signal(&r->cond);
76 a0a331aa 2005-01-06 devnull _procwakeupandunlock(_Procrendez *r)
78 a0a331aa 2005-01-06 devnull if(r->asleep){
79 a0a331aa 2005-01-06 devnull r->asleep = 0;
80 a0a331aa 2005-01-06 devnull pthread_cond_signal(&r->cond);
82 c345061e 2005-01-07 devnull unlock(r->l);
85 73722a8b 2004-12-27 devnull static void
86 73722a8b 2004-12-27 devnull startprocfn(void *v)
88 73722a8b 2004-12-27 devnull void **a;
89 73722a8b 2004-12-27 devnull void (*fn)(void*);
92 73722a8b 2004-12-27 devnull a = (void**)v;
93 c345061e 2005-01-07 devnull fn = (void(*)(void*))a[0];
94 73722a8b 2004-12-27 devnull p = a[1];
96 4dbefdd4 2004-12-27 devnull p->osprocid = pthread_self();
97 4dbefdd4 2004-12-27 devnull pthread_detach(p->osprocid);
99 73722a8b 2004-12-27 devnull (*fn)(p);
101 73722a8b 2004-12-27 devnull pthread_exit(0);
105 baef953d 2020-05-18 rsc startpthreadfn(void *v)
111 baef953d 2020-05-18 rsc a = (void**)v;
115 baef953d 2020-05-18 rsc t->osprocid = pthread_self();
116 baef953d 2020-05-18 rsc pthread_detach(t->osprocid);
117 baef953d 2020-05-18 rsc _threadpthreadmain(p, t);
118 baef953d 2020-05-18 rsc pthread_exit(0);
122 73722a8b 2004-12-27 devnull _procstart(Proc *p, void (*fn)(Proc*))
124 73722a8b 2004-12-27 devnull void **a;
126 73722a8b 2004-12-27 devnull a = malloc(2*sizeof a[0]);
127 73722a8b 2004-12-27 devnull if(a == nil)
128 73722a8b 2004-12-27 devnull sysfatal("_procstart malloc: %r");
129 c345061e 2005-01-07 devnull a[0] = (void*)fn;
130 73722a8b 2004-12-27 devnull a[1] = p;
132 4dbefdd4 2004-12-27 devnull if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
133 619085f0 2004-12-25 devnull fprint(2, "pthread_create: %r\n");
134 619085f0 2004-12-25 devnull abort();
139 baef953d 2020-05-18 rsc _threadpthreadstart(Proc *p, _Thread *t)
143 baef953d 2020-05-18 rsc a = malloc(3*sizeof a[0]);
144 baef953d 2020-05-18 rsc if(a == nil)
145 baef953d 2020-05-18 rsc sysfatal("_pthreadstart malloc: %r");
148 baef953d 2020-05-18 rsc if(pthread_create(&t->osprocid, nil, (void*(*)(void*))startpthreadfn, (void*)a) < 0){
149 baef953d 2020-05-18 rsc fprint(2, "pthread_create: %r\n");
154 619085f0 2004-12-25 devnull static pthread_key_t prockey;
157 619085f0 2004-12-25 devnull _threadproc(void)
159 619085f0 2004-12-25 devnull Proc *p;
161 619085f0 2004-12-25 devnull p = pthread_getspecific(prockey);
162 619085f0 2004-12-25 devnull return p;
166 619085f0 2004-12-25 devnull _threadsetproc(Proc *p)
168 619085f0 2004-12-25 devnull pthread_setspecific(prockey, p);
172 e1dc7e45 2004-12-27 devnull _pthreadinit(void)
174 7a2c8850 2005-01-17 devnull static struct utsname un;
175 7a2c8850 2005-01-17 devnull pthread_t id;
177 3d867865 2005-01-18 devnull if(uname(&un) < 0)
178 7a2c8850 2005-01-17 devnull fprint(2, "warning: uname failed: %r\n");
179 7a2c8850 2005-01-17 devnull if(strcmp(un.sysname, "Linux") == 0){
181 7a2c8850 2005-01-17 devnull * Want to distinguish between the old LinuxThreads pthreads
182 7a2c8850 2005-01-17 devnull * and the new NPTL implementation. NPTL uses much bigger
183 7a2c8850 2005-01-17 devnull * thread IDs.
185 7a2c8850 2005-01-17 devnull id = pthread_self();
186 46cfcf55 2005-02-03 devnull if(*(ulong*)(void*)&id < 1024*1024)
187 7a2c8850 2005-01-17 devnull sysfatal("cannot use LinuxThreads as pthread library; see %s/src/libthread/README.Linux", get9root());
189 619085f0 2004-12-25 devnull pthread_key_create(&prockey, 0);
193 93eb807a 2004-12-28 devnull threadexitsall(char *msg)
195 93eb807a 2004-12-28 devnull exits(msg);
199 1d2533d0 2004-12-28 devnull _threadpexit(void)
201 1d2533d0 2004-12-28 devnull pthread_exit(0);