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)
54 _procsleep(_Procrendez *r)
56 /* r is protected by r->l, which we hold */
57 pthread_cond_init(&r->cond, 0);
59 pthread_cond_wait(&r->cond, &r->l->mutex);
60 pthread_cond_destroy(&r->cond);
65 _procwakeup(_Procrendez *r)
69 pthread_cond_signal(&r->cond);
74 _procwakeupandunlock(_Procrendez *r)
78 pthread_cond_signal(&r->cond);
91 fn = (void(*)(void*))a[0];
94 p->osprocid = pthread_self();
95 pthread_detach(p->osprocid);
103 _procstart(Proc *p, void (*fn)(Proc*))
107 a = malloc(2*sizeof a[0]);
109 sysfatal("_procstart malloc: %r");
113 if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
114 fprint(2, "pthread_create: %r\n");
119 static pthread_key_t prockey;
126 p = pthread_getspecific(prockey);
131 _threadsetproc(Proc *p)
133 pthread_setspecific(prockey, p);
139 static struct utsname un;
143 fprint(2, "warning: uname failed: %r\n");
144 if(strcmp(un.sysname, "Linux") == 0){
146 * Want to distinguish between the old LinuxThreads pthreads
147 * and the new NPTL implementation. NPTL uses much bigger
151 if(*(ulong*)(void*)&id < 1024*1024)
152 sysfatal("cannot use LinuxThreads as pthread library; see %s/src/libthread/README.Linux", get9root());
154 pthread_key_create(&prockey, 0);
158 threadexitsall(char *msg)