1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* $OpenBSD: rthread_libc.c,v 1.4 2007/06/05 18:11:49 kurt Exp $ */
/* $snafu: libc_tag.c,v 1.4 2004/11/30 07:00:06 marc Exp $ */
/* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
#define _POSIX_THREADS
#include <sys/time.h>
#include <machine/spinlock.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "thread_private.h" /* in libc/include */
#include "rthread.h"
/*
* A thread tag is a pointer to a structure of this type. An opaque
* tag is used to decouple libc from the thread library.
*/
struct _thread_tag {
pthread_mutex_t m; /* the tag's mutex */
pthread_key_t k; /* a key for private data */
};
/*
* local mutex to protect against tag creation races.
*/
static pthread_mutex_t _thread_tag_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* Initialize a thread tag structure once. This function is called
* if the tag is null. Allocation and initialization are controlled
* by a mutex. If the tag is not null when the mutex is obtained
* the caller lost a race -- some other thread initialized the tag.
* This function will never return NULL.
*/
static void
_thread_tag_init(void **tag)
{
struct _thread_tag *tt;
int result;
result = pthread_mutex_lock(&_thread_tag_mutex);
if (result == 0) {
if (*tag == NULL) {
tt = malloc(sizeof *tt);
if (tt != NULL) {
result = pthread_mutex_init(&tt->m, NULL);
result |= pthread_key_create(&tt->k, free);
*tag = tt;
}
}
result |= pthread_mutex_unlock(&_thread_tag_mutex);
}
if (result != 0)
_rthread_debug(1, "tag init failure");
}
/*
* lock the mutex associated with the given tag
*/
void
_thread_tag_lock(void **tag)
{
struct _thread_tag *tt;
if (__isthreaded) {
if (*tag == NULL)
_thread_tag_init(tag);
tt = *tag;
if (pthread_mutex_lock(&tt->m) != 0)
_rthread_debug(1, "tag mutex lock failure");
}
}
/*
* unlock the mutex associated with the given tag
*/
void
_thread_tag_unlock(void **tag)
{
struct _thread_tag *tt;
if (__isthreaded) {
if (*tag == NULL)
_thread_tag_init(tag);
tt = *tag;
if (pthread_mutex_unlock(&tt->m) != 0)
_rthread_debug(1, "tag mutex unlock failure");
}
}
/*
* return the thread specific data for the given tag. If there
* is no data for this thread initialize it from 'storage'.
* On any error return 'err'.
*/
void *
_thread_tag_storage(void **tag, void *storage, size_t sz, void *err)
{
struct _thread_tag *tt;
void *ret;
if (*tag == NULL)
_thread_tag_init(tag);
tt = *tag;
ret = pthread_getspecific(tt->k);
if (ret == NULL) {
ret = malloc(sz);
if (ret == NULL)
ret = err;
else {
if (pthread_setspecific(tt->k, ret) == 0)
memcpy(ret, storage, sz);
else {
free(ret);
ret = err;
}
}
}
return ret;
}
void
_thread_mutex_lock(void **mutex)
{
pthread_mutex_t *pmutex = (pthread_mutex_t *)mutex;
if (pthread_mutex_lock(pmutex) != 0)
_rthread_debug(1, "mutex lock failure");
}
void
_thread_mutex_unlock(void **mutex)
{
pthread_mutex_t *pmutex = (pthread_mutex_t *)mutex;
if (pthread_mutex_unlock(pmutex) != 0)
_rthread_debug(1, "mutex unlock failure");
}
void
_thread_mutex_destroy(void **mutex)
{
pthread_mutex_t *pmutex = (pthread_mutex_t *)mutex;
if (pthread_mutex_destroy(pmutex) != 0)
_rthread_debug(1, "mutex destroy failure");
}
/*
* the malloc lock
*/
static _spinlock_lock_t malloc_lock = _SPINLOCK_UNLOCKED;
void
_thread_malloc_lock(void)
{
_spinlock(&malloc_lock);
}
void
_thread_malloc_unlock(void)
{
_spinunlock(&malloc_lock);
}
void
_thread_malloc_init(void)
{
}
#if 0
/*
* miscellaneous libc exported symbols we want to override
*/
int
close(int fd)
{
int rv;
pthread_testcancel();
rv = _thread_sys_close(fd);
pthread_testcancel();
return (rv);
}
#if 0
/* libc calls open */
int
creat(const char *path, mode_t mode)
{
}
#endif
#if 0
int
fcntl(int fd, int cmd, ...)
{
va_list ap;
int rv;
pthread_testcancel();
va_start(ap, cmd);
rv = _thread_sys_fcntl(fd, cmd, va_arg(cmd, void *));
va_end(ap);
pthread_testcancel();
return (rv);
}
#endif
int
fsync(int fd)
{
int rv;
pthread_testcancel();
rv = _thread_sys_fsync(fd);
pthread_testcancel();
return (rv);
}
int
msync(void *addr, size_t len, int flags)
{
int rv;
pthread_testcancel();
rv = _thread_sys_msync(addr, len, flags);
pthread_testcancel();
return (rv);
}
int
nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
int rv;
pthread_testcancel();
rv = _thread_sys_nanosleep(rqtp, rmtp);
pthread_testcancel();
return (rv);
}
#if 0
int
open(const char *path, int flags, ...)
{
va_list ap;
int rv;
pthread_testcancel();
va_start(ap, cmd);
rv = _thread_sys_open(fd, cmd, va_arg(cmd, mode_t));
va_end(ap);
pthread_testcancel();
return (rv);
}
#endif
#if 0
int
pause(void)
{
}
#endif
ssize_t
read(int fd, void *buf, size_t nbytes)
{
ssize_t rv;
pthread_testcancel();
rv = read(fd, buf, nbytes);
pthread_testcancel();
return (rv);
}
#if 0
int
sigwaitinfo()
{
}
#endif
int
sigsuspend(const sigset_t *sigmask)
{
int rv;
pthread_testcancel();
rv = sigsuspend(sigmask);
pthread_testcancel();
return (rv);
}
#if 0
/* libc sleep(3) calls nanosleep(2), so we'll catch it there */
unsigned int
sleep(unsigned int seconds)
{
}
#endif
#if 0
int system(const char *string)
{
}
#endif
#if 0
int
tcdrain(int fd)
{
}
#endif
#if 0
/* wait and waitpid will be handled by libc calling wait4 */
pid_t
wait(int *status)
{
}
pid_t
waitpid(pid_t wpid, int *status, int options)
{
}
#endif
pid_t
wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
{
pid_t rv;
pthread_testcancel();
rv = _thread_sys_wait4(wpid, status, options, rusage);
pthread_testcancel();
return (rv);
}
ssize_t
write(int fd, const void *buf, size_t nbytes)
{
ssize_t rv;
pthread_testcancel();
rv = _thread_sys_write(fd, buf, nbytes);
pthread_testcancel();
return (rv);
}
#endif
|