Blame


1 619085f0 2004-12-25 devnull #include "threadimpl.h"
2 619085f0 2004-12-25 devnull
3 3d867865 2005-01-18 devnull #undef exits
4 3d867865 2005-01-18 devnull #undef _exits
5 3d867865 2005-01-18 devnull
6 619085f0 2004-12-25 devnull static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
7 619085f0 2004-12-25 devnull
8 619085f0 2004-12-25 devnull static void
9 619085f0 2004-12-25 devnull lockinit(Lock *lk)
10 619085f0 2004-12-25 devnull {
11 619085f0 2004-12-25 devnull pthread_mutexattr_t attr;
12 619085f0 2004-12-25 devnull
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;
20 619085f0 2004-12-25 devnull }
21 619085f0 2004-12-25 devnull pthread_mutex_unlock(&initmutex);
22 619085f0 2004-12-25 devnull }
23 619085f0 2004-12-25 devnull
24 619085f0 2004-12-25 devnull int
25 619085f0 2004-12-25 devnull _threadlock(Lock *lk, int block, ulong pc)
26 619085f0 2004-12-25 devnull {
27 619085f0 2004-12-25 devnull int r;
28 619085f0 2004-12-25 devnull
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)
33 619085f0 2004-12-25 devnull abort();
34 619085f0 2004-12-25 devnull return 1;
35 619085f0 2004-12-25 devnull }else{
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;
41 619085f0 2004-12-25 devnull abort();
42 619085f0 2004-12-25 devnull return 0;
43 619085f0 2004-12-25 devnull }
44 619085f0 2004-12-25 devnull }
45 619085f0 2004-12-25 devnull
46 619085f0 2004-12-25 devnull void
47 619085f0 2004-12-25 devnull _threadunlock(Lock *lk, ulong pc)
48 619085f0 2004-12-25 devnull {
49 619085f0 2004-12-25 devnull if(pthread_mutex_unlock(&lk->mutex) != 0)
50 619085f0 2004-12-25 devnull abort();
51 619085f0 2004-12-25 devnull }
52 619085f0 2004-12-25 devnull
53 162d0d5c 2020-05-18 rsc /* note: _procsleep can have spurious wakeups, like pthread_cond_wait */
54 619085f0 2004-12-25 devnull void
55 619085f0 2004-12-25 devnull _procsleep(_Procrendez *r)
56 619085f0 2004-12-25 devnull {
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;
64 619085f0 2004-12-25 devnull }
65 619085f0 2004-12-25 devnull
66 619085f0 2004-12-25 devnull void
67 619085f0 2004-12-25 devnull _procwakeup(_Procrendez *r)
68 619085f0 2004-12-25 devnull {
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);
72 619085f0 2004-12-25 devnull }
73 619085f0 2004-12-25 devnull }
74 619085f0 2004-12-25 devnull
75 a0a331aa 2005-01-06 devnull void
76 a0a331aa 2005-01-06 devnull _procwakeupandunlock(_Procrendez *r)
77 a0a331aa 2005-01-06 devnull {
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);
81 a0a331aa 2005-01-06 devnull }
82 c345061e 2005-01-07 devnull unlock(r->l);
83 a0a331aa 2005-01-06 devnull }
84 a0a331aa 2005-01-06 devnull
85 73722a8b 2004-12-27 devnull static void
86 73722a8b 2004-12-27 devnull startprocfn(void *v)
87 73722a8b 2004-12-27 devnull {
88 73722a8b 2004-12-27 devnull void **a;
89 73722a8b 2004-12-27 devnull void (*fn)(void*);
90 73722a8b 2004-12-27 devnull Proc *p;
91 73722a8b 2004-12-27 devnull
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];
95 73722a8b 2004-12-27 devnull free(a);
96 4dbefdd4 2004-12-27 devnull p->osprocid = pthread_self();
97 4dbefdd4 2004-12-27 devnull pthread_detach(p->osprocid);
98 73722a8b 2004-12-27 devnull
99 73722a8b 2004-12-27 devnull (*fn)(p);
100 73722a8b 2004-12-27 devnull
101 73722a8b 2004-12-27 devnull pthread_exit(0);
102 73722a8b 2004-12-27 devnull }
103 73722a8b 2004-12-27 devnull
104 baef953d 2020-05-18 rsc static void
105 baef953d 2020-05-18 rsc startpthreadfn(void *v)
106 baef953d 2020-05-18 rsc {
107 baef953d 2020-05-18 rsc void **a;
108 baef953d 2020-05-18 rsc Proc *p;
109 baef953d 2020-05-18 rsc _Thread *t;
110 baef953d 2020-05-18 rsc
111 baef953d 2020-05-18 rsc a = (void**)v;
112 baef953d 2020-05-18 rsc p = a[0];
113 baef953d 2020-05-18 rsc t = a[1];
114 baef953d 2020-05-18 rsc free(a);
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);
119 baef953d 2020-05-18 rsc }
120 baef953d 2020-05-18 rsc
121 619085f0 2004-12-25 devnull void
122 73722a8b 2004-12-27 devnull _procstart(Proc *p, void (*fn)(Proc*))
123 619085f0 2004-12-25 devnull {
124 73722a8b 2004-12-27 devnull void **a;
125 73722a8b 2004-12-27 devnull
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;
131 73722a8b 2004-12-27 devnull
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();
135 619085f0 2004-12-25 devnull }
136 619085f0 2004-12-25 devnull }
137 619085f0 2004-12-25 devnull
138 baef953d 2020-05-18 rsc void
139 baef953d 2020-05-18 rsc _threadpthreadstart(Proc *p, _Thread *t)
140 baef953d 2020-05-18 rsc {
141 baef953d 2020-05-18 rsc void **a;
142 baef953d 2020-05-18 rsc
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");
146 baef953d 2020-05-18 rsc a[0] = p;
147 baef953d 2020-05-18 rsc a[1] = t;
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");
150 baef953d 2020-05-18 rsc abort();
151 baef953d 2020-05-18 rsc }
152 baef953d 2020-05-18 rsc }
153 baef953d 2020-05-18 rsc
154 619085f0 2004-12-25 devnull static pthread_key_t prockey;
155 619085f0 2004-12-25 devnull
156 619085f0 2004-12-25 devnull Proc*
157 619085f0 2004-12-25 devnull _threadproc(void)
158 619085f0 2004-12-25 devnull {
159 619085f0 2004-12-25 devnull Proc *p;
160 619085f0 2004-12-25 devnull
161 619085f0 2004-12-25 devnull p = pthread_getspecific(prockey);
162 619085f0 2004-12-25 devnull return p;
163 619085f0 2004-12-25 devnull }
164 619085f0 2004-12-25 devnull
165 619085f0 2004-12-25 devnull void
166 619085f0 2004-12-25 devnull _threadsetproc(Proc *p)
167 619085f0 2004-12-25 devnull {
168 619085f0 2004-12-25 devnull pthread_setspecific(prockey, p);
169 619085f0 2004-12-25 devnull }
170 619085f0 2004-12-25 devnull
171 619085f0 2004-12-25 devnull void
172 e1dc7e45 2004-12-27 devnull _pthreadinit(void)
173 619085f0 2004-12-25 devnull {
174 7a2c8850 2005-01-17 devnull static struct utsname un;
175 7a2c8850 2005-01-17 devnull pthread_t id;
176 7a2c8850 2005-01-17 devnull
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){
180 7a2c8850 2005-01-17 devnull /*
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.
184 7a2c8850 2005-01-17 devnull */
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());
188 7a2c8850 2005-01-17 devnull }
189 619085f0 2004-12-25 devnull pthread_key_create(&prockey, 0);
190 619085f0 2004-12-25 devnull }
191 619085f0 2004-12-25 devnull
192 93eb807a 2004-12-28 devnull void
193 93eb807a 2004-12-28 devnull threadexitsall(char *msg)
194 93eb807a 2004-12-28 devnull {
195 93eb807a 2004-12-28 devnull exits(msg);
196 93eb807a 2004-12-28 devnull }
197 93eb807a 2004-12-28 devnull
198 1d2533d0 2004-12-28 devnull void
199 1d2533d0 2004-12-28 devnull _threadpexit(void)
200 1d2533d0 2004-12-28 devnull {
201 1d2533d0 2004-12-28 devnull pthread_exit(0);
202 1d2533d0 2004-12-28 devnull }