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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/* $OpenBSD: rthread.c,v 1.48 2011/12/27 17:33:21 guenther Exp $ */
/*
* Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
* All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* The heart of rthreads. Basic functions like creating and joining
* threads.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>
#include <pthread.h>
#include "thread_private.h" /* in libc/include */
#include "rthread.h"
#include "tcb.h"
static int concurrency_level; /* not used */
int _threads_ready;
struct listhead _thread_list = LIST_HEAD_INITIALIZER(_thread_list);
_spinlock_lock_t _thread_lock = _SPINLOCK_UNLOCKED;
static struct pthread_queue _thread_gc_list
= TAILQ_HEAD_INITIALIZER(_thread_gc_list);
static _spinlock_lock_t _thread_gc_lock = _SPINLOCK_UNLOCKED;
struct pthread _initial_thread;
struct thread_control_block _initial_thread_tcb;
int __tfork_thread(const struct __tfork *, void *, void (*)(void *), void *);
/*
* internal support functions
*/
void
_spinlock(_spinlock_lock_t *lock)
{
while (_atomic_lock(lock))
pthread_yield();
}
void
_spinunlock(_spinlock_lock_t *lock)
{
*lock = _SPINLOCK_UNLOCKED;
}
/*
* This sets up the thread base for the initial thread so that it
* references the errno location provided by libc. For other threads
* this is handled by the block in _rthread_start().
*/
void _rthread_initlib(void) __attribute__((constructor));
void _rthread_initlib(void)
{
struct thread_control_block *tcb = &_initial_thread_tcb;
/* use libc's errno for the main thread */
TCB_INIT(tcb, &_initial_thread, ___errno());
TCB_SET(tcb);
}
int *
__errno(void)
{
return (TCB_ERRNOPTR());
}
static void
_rthread_start(void *v)
{
pthread_t thread = v;
void *retval;
retval = thread->fn(thread->arg);
pthread_exit(retval);
}
static void
sigthr_handler(__unused int sig)
{
pthread_t self = pthread_self();
if ((self->flags & (THREAD_CANCELED | THREAD_CANCEL_COND)) ==
THREAD_CANCELED && (self->cancel_point ||
(self->flags & THREAD_CANCEL_DEFERRED) == 0))
pthread_exit(PTHREAD_CANCELED);
}
static int
_rthread_init(void)
{
pthread_t thread = &_initial_thread;
extern int __isthreaded;
struct sigaction sa;
thread->tid = getthrid();
thread->donesem.lock = _SPINLOCK_UNLOCKED;
thread->flags |= THREAD_CANCEL_ENABLE|THREAD_CANCEL_DEFERRED;
thread->flags_lock = _SPINLOCK_UNLOCKED;
strlcpy(thread->name, "Main process", sizeof(thread->name));
LIST_INSERT_HEAD(&_thread_list, thread, threads);
_rthread_debug_init();
_rthread_debug(1, "rthread init\n");
_threads_ready = 1;
__isthreaded = 1;
#if defined(__ELF__) && defined(PIC)
/*
* To avoid recursion problems in ld.so, we need to trigger the
* functions once to fully bind them before registering them
* for use.
*/
_rthread_dl_lock(0);
_rthread_dl_lock(1);
_rthread_bind_lock(0);
_rthread_bind_lock(1);
dlctl(NULL, DL_SETTHREADLCK, _rthread_dl_lock);
dlctl(NULL, DL_SETBINDLCK, _rthread_bind_lock);
#endif
/*
* Set the handler on the signal used for cancelation and
* suspension, and make sure it's unblocked
*/
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = sigthr_handler;
_thread_sys_sigaction(SIGTHR, &sa, NULL);
sigaddset(&sa.sa_mask, SIGTHR);
sigprocmask(SIG_UNBLOCK, &sa.sa_mask, NULL);
return (0);
}
static void
_rthread_free(pthread_t thread)
{
/* catch wrongdoers for the moment */
/* initial_thread.tid must remain valid */
if (thread != &_initial_thread) {
struct stack *stack = thread->stack;
pid_t tid = thread->tid;
void *arg = thread->arg;
/* catch wrongdoers for the moment */
memset(thread, 0xd0, sizeof(*thread));
thread->stack = stack;
thread->tid = tid;
thread->arg = arg;
_spinlock(&_thread_gc_lock);
TAILQ_INSERT_TAIL(&_thread_gc_list, thread, waiting);
_spinunlock(&_thread_gc_lock);
}
}
static void
_rthread_setflag(pthread_t thread, int flag)
{
_spinlock(&thread->flags_lock);
thread->flags |= flag;
_spinunlock(&thread->flags_lock);
}
static void
_rthread_clearflag(pthread_t thread, int flag)
{
_spinlock(&thread->flags_lock);
thread->flags &= ~flag;
_spinunlock(&thread->flags_lock);
}
/*
* real pthread functions
*/
pthread_t
pthread_self(void)
{
if (!_threads_ready)
if (_rthread_init())
return (NULL);
return (TCB_THREAD());
}
static void
_rthread_reaper(void)
{
pthread_t thread;
restart:_spinlock(&_thread_gc_lock);
TAILQ_FOREACH(thread, &_thread_gc_list, waiting) {
if (thread->tid != 0)
continue;
TAILQ_REMOVE(&_thread_gc_list, thread, waiting);
_spinunlock(&_thread_gc_lock);
_rthread_debug(3, "rthread reaping %p stack %p\n",
(void *)thread, (void *)thread->stack);
_rthread_free_stack(thread->stack);
_rtld_free_tls(thread->arg,
sizeof(struct thread_control_block), sizeof(void *));
free(thread);
goto restart;
}
_spinunlock(&_thread_gc_lock);
}
void
pthread_exit(void *retval)
{
struct rthread_cleanup_fn *clfn;
pthread_t thread = pthread_self();
if (thread->flags & THREAD_DYING) {
/*
* Called pthread_exit() from destructor or cancelation
* handler: blow up. XXX write something to stderr?
*/
_exit(42);
}
_rthread_setflag(thread, THREAD_DYING);
thread->retval = retval;
for (clfn = thread->cleanup_fns; clfn; ) {
struct rthread_cleanup_fn *oclfn = clfn;
clfn = clfn->next;
oclfn->fn(oclfn->arg);
free(oclfn);
}
_rthread_tls_destructors(thread);
_spinlock(&_thread_lock);
LIST_REMOVE(thread, threads);
_spinunlock(&_thread_lock);
#ifdef TCB_GET
thread->arg = TCB_GET();
#else
thread->arg = __get_tcb();
#endif
if (thread->flags & THREAD_DETACHED)
_rthread_free(thread);
else {
_rthread_setflag(thread, THREAD_DONE);
_sem_post(&thread->donesem);
}
threxit(&thread->tid);
for(;;);
}
int
pthread_join(pthread_t thread, void **retval)
{
int e;
if (thread == NULL)
e = EINVAL;
else if (thread->tid == getthrid())
e = EDEADLK;
else if (thread->flags & THREAD_DETACHED)
e = EINVAL;
else {
_sem_wait(&thread->donesem, 0);
if (retval)
*retval = thread->retval;
e = 0;
/* We should be the last having a ref to this thread, but
* someone stupid or evil might haved detached it;
* in that case the thread will cleanup itself */
if ((thread->flags & THREAD_DETACHED) == 0)
_rthread_free(thread);
}
_rthread_reaper();
return (e);
}
int
pthread_detach(pthread_t thread)
{
int rc = 0;
_spinlock(&thread->flags_lock);
if (thread->flags & THREAD_DETACHED) {
rc = EINVAL;
_spinunlock(&thread->flags_lock);
} else if (thread->flags & THREAD_DONE) {
_spinunlock(&thread->flags_lock);
_rthread_free(thread);
} else {
thread->flags |= THREAD_DETACHED;
_spinunlock(&thread->flags_lock);
}
_rthread_reaper();
return (rc);
}
int
pthread_create(pthread_t *threadp, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
struct thread_control_block *tcb;
pthread_t thread;
struct __tfork param;
int rc = 0;
if (!_threads_ready)
if ((rc = _rthread_init()))
return (rc);
_rthread_reaper();
thread = calloc(1, sizeof(*thread));
if (!thread)
return (errno);
thread->donesem.lock = _SPINLOCK_UNLOCKED;
thread->flags_lock = _SPINLOCK_UNLOCKED;
thread->fn = start_routine;
thread->arg = arg;
thread->tid = -1;
if (attr)
thread->attr = *(*attr);
else {
thread->attr.stack_size = RTHREAD_STACK_SIZE_DEF;
thread->attr.guard_size = sysconf(_SC_PAGESIZE);
thread->attr.stack_size -= thread->attr.guard_size;
}
if (thread->attr.detach_state == PTHREAD_CREATE_DETACHED)
thread->flags |= THREAD_DETACHED;
thread->flags |= THREAD_CANCEL_ENABLE|THREAD_CANCEL_DEFERRED;
thread->stack = _rthread_alloc_stack(thread);
if (!thread->stack) {
rc = errno;
goto fail1;
}
tcb = _rtld_allocate_tls(NULL, sizeof(*tcb), sizeof(void *));
if (tcb == NULL) {
rc = errno;
goto fail2;
}
TCB_INIT(tcb, thread, &thread->myerrno);
param.tf_tcb = tcb;
param.tf_tid = &thread->tid;
param.tf_flags = 0;
_spinlock(&_thread_lock);
LIST_INSERT_HEAD(&_thread_list, thread, threads);
_spinunlock(&_thread_lock);
rc = __tfork_thread(¶m, thread->stack->sp, _rthread_start, thread);
if (rc != -1) {
/* success */
*threadp = thread;
return (0);
}
rc = errno;
_spinlock(&_thread_lock);
LIST_REMOVE(thread, threads);
_spinunlock(&_thread_lock);
_rtld_free_tls(tcb, sizeof(*tcb), sizeof(void *));
fail2:
_rthread_free_stack(thread->stack);
fail1:
_rthread_free(thread);
return (rc);
}
int
pthread_kill(pthread_t thread, int sig)
{
return (kill(thread->tid, sig) == 0 ? 0 : errno);
}
int
pthread_equal(pthread_t t1, pthread_t t2)
{
return (t1 == t2);
}
int
pthread_cancel(pthread_t thread)
{
_rthread_setflag(thread, THREAD_CANCELED);
if (thread->flags & THREAD_CANCEL_ENABLE)
kill(thread->tid, SIGTHR);
return (0);
}
void
pthread_testcancel(void)
{
if ((pthread_self()->flags & (THREAD_CANCELED|THREAD_CANCEL_ENABLE)) ==
(THREAD_CANCELED|THREAD_CANCEL_ENABLE))
pthread_exit(PTHREAD_CANCELED);
}
int
pthread_setcancelstate(int state, int *oldstatep)
{
pthread_t self = pthread_self();
int oldstate;
oldstate = self->flags & THREAD_CANCEL_ENABLE ?
PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE;
if (state == PTHREAD_CANCEL_ENABLE) {
_rthread_setflag(self, THREAD_CANCEL_ENABLE);
pthread_testcancel();
} else if (state == PTHREAD_CANCEL_DISABLE) {
_rthread_clearflag(self, THREAD_CANCEL_ENABLE);
} else {
return (EINVAL);
}
if (oldstatep)
*oldstatep = oldstate;
return (0);
}
int
pthread_setcanceltype(int type, int *oldtypep)
{
pthread_t self = pthread_self();
int oldtype;
oldtype = self->flags & THREAD_CANCEL_DEFERRED ?
PTHREAD_CANCEL_DEFERRED : PTHREAD_CANCEL_ASYNCHRONOUS;
if (type == PTHREAD_CANCEL_DEFERRED) {
_rthread_setflag(self, THREAD_CANCEL_DEFERRED);
pthread_testcancel();
} else if (type == PTHREAD_CANCEL_ASYNCHRONOUS) {
_rthread_clearflag(self, THREAD_CANCEL_DEFERRED);
} else {
return (EINVAL);
}
if (oldtypep)
*oldtypep = oldtype;
return (0);
}
void
pthread_cleanup_push(void (*fn)(void *), void *arg)
{
struct rthread_cleanup_fn *clfn;
pthread_t self = pthread_self();
clfn = calloc(1, sizeof(*clfn));
if (!clfn)
return;
clfn->fn = fn;
clfn->arg = arg;
clfn->next = self->cleanup_fns;
self->cleanup_fns = clfn;
}
void
pthread_cleanup_pop(int execute)
{
struct rthread_cleanup_fn *clfn;
pthread_t self = pthread_self();
clfn = self->cleanup_fns;
if (clfn) {
self->cleanup_fns = clfn->next;
if (execute)
clfn->fn(clfn->arg);
free(clfn);
}
}
int
pthread_getconcurrency(void)
{
return (concurrency_level);
}
int
pthread_setconcurrency(int new_level)
{
if (new_level < 0)
return (EINVAL);
concurrency_level = new_level;
return (0);
}
/*
* compat debug stuff
*/
void
_thread_dump_info(void)
{
pthread_t thread;
_spinlock(&_thread_lock);
LIST_FOREACH(thread, &_thread_list, threads)
printf("thread %d flags %d name %s\n",
thread->tid, thread->flags, thread->name);
_spinunlock(&_thread_lock);
}
#if defined(__ELF__) && defined(PIC)
void
_rthread_dl_lock(int what)
{
static _spinlock_lock_t lock = _SPINLOCK_UNLOCKED;
if (what == 0)
_spinlock(&lock);
else
_spinunlock(&lock);
}
void
_rthread_bind_lock(int what)
{
static _spinlock_lock_t lock = _SPINLOCK_UNLOCKED;
if (what == 0)
_spinlock(&lock);
else
_spinunlock(&lock);
}
#endif
|