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 |
… | |
… | |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | ///////////////////////////////////////////////////////////////////////////// |
16 | ///////////////////////////////////////////////////////////////////////////// |
17 | |
17 | |
18 | #ifdef _WIN32 |
18 | #ifdef _WIN32 |
19 | typedef int ssize_t; |
|
|
20 | |
19 | |
21 | #define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls |
20 | //#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls, fails with mingw |
22 | #define _WIN32_WINNT 0x400 |
21 | #define _WIN32_WINNT 0x400 // maybe working alternative for mingw |
23 | #include <stdio.h>//D |
22 | #include <stdio.h>//D |
24 | #include <fcntl.h> |
23 | #include <fcntl.h> |
25 | #include <io.h> |
24 | #include <io.h> |
26 | #include <time.h> |
25 | #include <time.h> |
27 | #include <winsock2.h> |
26 | #include <winsock2.h> |
28 | #include <process.h> |
27 | #include <process.h> |
29 | #include <windows.h> |
28 | #include <windows.h> |
|
|
29 | |
|
|
30 | /* work around some bugs in ptw32 */ |
|
|
31 | #if defined(__MINGW32__) && defined(_TIMESPEC_DEFINED) |
|
|
32 | #define HAVE_STRUCT_TIMESPEC 1 |
|
|
33 | #endif |
|
|
34 | |
30 | #include <pthread.h> |
35 | #include <pthread.h> |
31 | #define sigset_t int |
36 | #define sigset_t int |
|
|
37 | #define sigfillset(a) |
32 | #define pthread_sigmask(a,b,c) |
38 | #define pthread_sigmask(a,b,c) |
33 | #define sigaddset(a,b) |
39 | #define sigaddset(a,b) |
34 | #define sigemptyset(s) |
40 | #define sigemptyset(s) |
35 | #define sigfillset(s) |
|
|
36 | |
41 | |
37 | typedef pthread_mutex_t xmutex_t; |
42 | typedef pthread_mutex_t xmutex_t; |
38 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
43 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
44 | #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
39 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
45 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
40 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
46 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
41 | |
47 | |
42 | typedef pthread_cond_t xcond_t; |
48 | typedef pthread_cond_t xcond_t; |
43 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
49 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
|
|
50 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
44 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
51 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
45 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
52 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
46 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
53 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
47 | |
54 | |
48 | typedef pthread_t xthread_t; |
55 | typedef pthread_t xthread_t; |
49 | #define X_THREAD_PROC(name) void *name (void *thr_arg) |
56 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
50 | #define X_THREAD_ATFORK(a,b,c) |
57 | #define X_THREAD_ATFORK(a,b,c) |
51 | |
58 | |
52 | static int |
59 | static int |
53 | thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
60 | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
54 | { |
61 | { |
55 | int retval; |
62 | int retval; |
56 | pthread_attr_t attr; |
63 | pthread_attr_t attr; |
57 | |
64 | |
58 | pthread_attr_init (&attr); |
65 | pthread_attr_init (&attr); |
… | |
… | |
93 | #include <limits.h> |
100 | #include <limits.h> |
94 | #include <pthread.h> |
101 | #include <pthread.h> |
95 | |
102 | |
96 | typedef pthread_mutex_t xmutex_t; |
103 | typedef pthread_mutex_t xmutex_t; |
97 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
104 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
98 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
105 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
106 | # define X_MUTEX_CREATE(mutex) \ |
|
|
107 | do { \ |
|
|
108 | pthread_mutexattr_t attr; \ |
|
|
109 | pthread_mutexattr_init (&attr); \ |
|
|
110 | pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \ |
|
|
111 | pthread_mutex_init (&(mutex), &attr); \ |
|
|
112 | } while (0) |
99 | #else |
113 | #else |
100 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
114 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
115 | # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
101 | #endif |
116 | #endif |
102 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
117 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
103 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
118 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
104 | |
119 | |
105 | typedef pthread_cond_t xcond_t; |
120 | typedef pthread_cond_t xcond_t; |
106 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
121 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
|
|
122 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
107 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
123 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
108 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
124 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
109 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
125 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
110 | |
126 | |
111 | typedef pthread_t xthread_t; |
127 | typedef pthread_t xthread_t; |
112 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
128 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
113 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
129 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
114 | |
130 | |
… | |
… | |
116 | #ifndef PTHREAD_STACK_MIN |
132 | #ifndef PTHREAD_STACK_MIN |
117 | # define PTHREAD_STACK_MIN 0 |
133 | # define PTHREAD_STACK_MIN 0 |
118 | #endif |
134 | #endif |
119 | |
135 | |
120 | #ifndef X_STACKSIZE |
136 | #ifndef X_STACKSIZE |
121 | # define X_STACKSIZE sizeof (long) * 4096 |
137 | # define X_STACKSIZE sizeof (void *) * 4096 |
122 | #endif |
138 | #endif |
123 | |
139 | |
124 | static int |
140 | static int |
125 | thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
141 | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
126 | { |
142 | { |
127 | int retval; |
143 | int retval; |
128 | sigset_t fullsigset, oldsigset; |
144 | sigset_t fullsigset, oldsigset; |
129 | pthread_attr_t attr; |
145 | pthread_attr_t attr; |
130 | |
146 | |