1 #include "threadimpl.h"
6 static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
11 pthread_mutexattr_t attr;
13 pthread_mutex_lock(&initmutex);
15 pthread_mutexattr_init(&attr);
16 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
17 pthread_mutex_init(&lk->mutex, &attr);
18 pthread_mutexattr_destroy(&attr);
21 pthread_mutex_unlock(&initmutex);
25 _threadlock(Lock *lk, int block, ulong pc)
32 if(pthread_mutex_lock(&lk->mutex) != 0)
36 r = pthread_mutex_trylock(&lk->mutex);
47 _threadunlock(Lock *lk, ulong pc)
49 if(pthread_mutex_unlock(&lk->mutex) != 0)
53 /* note: _procsleep can have spurious wakeups, like pthread_cond_wait */
55 _procsleep(_Procrendez *r)
57 /* r is protected by r->l, which we hold */
58 pthread_cond_init(&r->cond, 0);
60 if(pthread_cond_wait(&r->cond, &r->l->mutex) != 0)
61 sysfatal("pthread_cond_wait: %r");
62 pthread_cond_destroy(&r->cond);
67 _procwakeup(_Procrendez *r)
71 pthread_cond_signal(&r->cond);
76 _procwakeupandunlock(_Procrendez *r)
80 pthread_cond_signal(&r->cond);
93 fn = (void(*)(void*))a[0];
96 p->osprocid = pthread_self();
97 pthread_detach(p->osprocid);
105 startpthreadfn(void *v)
115 t->osprocid = pthread_self();
116 pthread_detach(t->osprocid);
117 _threadpthreadmain(p, t);
122 _procstart(Proc *p, void (*fn)(Proc*))
126 a = malloc(2*sizeof a[0]);
128 sysfatal("_procstart malloc: %r");
132 if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
133 fprint(2, "pthread_create: %r\n");
139 _threadpthreadstart(Proc *p, _Thread *t)
143 a = malloc(3*sizeof a[0]);
145 sysfatal("_pthreadstart malloc: %r");
148 if(pthread_create(&t->osprocid, nil, (void*(*)(void*))startpthreadfn, (void*)a) < 0){
149 fprint(2, "pthread_create: %r\n");
154 static pthread_key_t prockey;
161 p = pthread_getspecific(prockey);
166 _threadsetproc(Proc *p)
168 pthread_setspecific(prockey, p);
174 static struct utsname un;
178 fprint(2, "warning: uname failed: %r\n");
179 if(strcmp(un.sysname, "Linux") == 0){
181 * Want to distinguish between the old LinuxThreads pthreads
182 * and the new NPTL implementation. NPTL uses much bigger
186 if(*(ulong*)(void*)&id < 1024*1024)
187 sysfatal("cannot use LinuxThreads as pthread library; see %s/src/libthread/README.Linux", get9root());
189 pthread_key_create(&prockey, 0);
193 threadexitsall(char *msg)