1 | #ifndef XTHREAD_H_ |
1 | #ifndef XTHREAD_H_ |
2 | #define XTHREAD_H_ |
2 | #define XTHREAD_H_ |
3 | |
3 | |
4 | /* whether word reads are potentially non-atomic. |
4 | /* whether word reads are potentially non-atomic. |
5 | * this is conservatice, likely most arches this runs |
5 | * this is conservative, likely most arches this runs |
6 | * on have atomic word read/writes. |
6 | * on have atomic word read/writes. |
7 | */ |
7 | */ |
8 | #ifndef WORDACCESS_UNSAFE |
8 | #ifndef WORDACCESS_UNSAFE |
9 | # if __i386 || __x86_64 |
9 | # if __i386 || __x86_64 |
10 | # define WORDACCESS_UNSAFE 0 |
10 | # define WORDACCESS_UNSAFE 0 |
… | |
… | |
33 | #define pthread_sigmask(a,b,c) |
33 | #define pthread_sigmask(a,b,c) |
34 | #define sigaddset(a,b) |
34 | #define sigaddset(a,b) |
35 | #define sigemptyset(s) |
35 | #define sigemptyset(s) |
36 | #define sigfillset(s) |
36 | #define sigfillset(s) |
37 | |
37 | |
38 | typedef pthread_mutex_t mutex_t; |
38 | typedef pthread_mutex_t xmutex_t; |
39 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
39 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
40 | #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
40 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
41 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
41 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
42 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
42 | |
43 | |
43 | typedef pthread_cond_t cond_t; |
44 | typedef pthread_cond_t xcond_t; |
44 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
45 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
|
|
46 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
45 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
47 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
46 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
48 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
47 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
49 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
48 | |
50 | |
49 | typedef pthread_t thread_t; |
51 | typedef pthread_t xthread_t; |
50 | #define X_THREAD_PROC(name) void *name (void *thr_arg) |
52 | #define X_THREAD_PROC(name) void *name (void *thr_arg) |
51 | #define X_THREAD_ATFORK(a,b,c) |
53 | #define X_THREAD_ATFORK(a,b,c) |
52 | |
54 | |
53 | static int |
55 | static int |
54 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
56 | thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
55 | { |
57 | { |
56 | int retval; |
58 | int retval; |
57 | pthread_attr_t attr; |
59 | pthread_attr_t attr; |
58 | |
60 | |
59 | pthread_attr_init (&attr); |
61 | pthread_attr_init (&attr); |
… | |
… | |
92 | #include <fcntl.h> |
94 | #include <fcntl.h> |
93 | #include <signal.h> |
95 | #include <signal.h> |
94 | #include <limits.h> |
96 | #include <limits.h> |
95 | #include <pthread.h> |
97 | #include <pthread.h> |
96 | |
98 | |
97 | typedef pthread_mutex_t mutex_t; |
99 | typedef pthread_mutex_t xmutex_t; |
98 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
100 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
99 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
101 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
102 | # define X_MUTEX_CREATE(mutex) \ |
|
|
103 | do { \ |
|
|
104 | pthread_mutexattr_t attr; \ |
|
|
105 | pthread_mutexattr_init (&attr); \ |
|
|
106 | pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \ |
|
|
107 | pthread_mutex_init (&(mutex), &attr); \ |
|
|
108 | } while (0) |
100 | #else |
109 | #else |
101 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
110 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
111 | # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
102 | #endif |
112 | #endif |
103 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
113 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
104 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
114 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
105 | |
115 | |
106 | typedef pthread_cond_t cond_t; |
116 | typedef pthread_cond_t xcond_t; |
107 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
117 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
|
|
118 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
108 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
119 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
109 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
120 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
110 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
121 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
111 | |
122 | |
112 | typedef pthread_t thread_t; |
123 | typedef pthread_t xthread_t; |
113 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
124 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
114 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
125 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
115 | |
126 | |
116 | // the broken bsd's once more |
127 | // the broken bsd's once more |
117 | #ifndef PTHREAD_STACK_MIN |
128 | #ifndef PTHREAD_STACK_MIN |
… | |
… | |
121 | #ifndef X_STACKSIZE |
132 | #ifndef X_STACKSIZE |
122 | # define X_STACKSIZE sizeof (long) * 4096 |
133 | # define X_STACKSIZE sizeof (long) * 4096 |
123 | #endif |
134 | #endif |
124 | |
135 | |
125 | static int |
136 | static int |
126 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
137 | thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
127 | { |
138 | { |
128 | int retval; |
139 | int retval; |
129 | sigset_t fullsigset, oldsigset; |
140 | sigset_t fullsigset, oldsigset; |
130 | pthread_attr_t attr; |
141 | pthread_attr_t attr; |
131 | |
142 | |