/src/tarantool/third_party/libeio/xthread.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef XTHREAD_H_ |
2 | | #define XTHREAD_H_ |
3 | | |
4 | | /* whether word reads are potentially non-atomic. |
5 | | * this is conservative, likely most arches this runs |
6 | | * on have atomic word read/writes. |
7 | | */ |
8 | | #ifndef WORDACCESS_UNSAFE |
9 | | # if __i386 || __x86_64 |
10 | 0 | # define WORDACCESS_UNSAFE 0 |
11 | | # else |
12 | | # define WORDACCESS_UNSAFE 1 |
13 | | # endif |
14 | | #endif |
15 | | |
16 | | ///////////////////////////////////////////////////////////////////////////// |
17 | | |
18 | | #ifdef _WIN32 |
19 | | |
20 | | //#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls, fails with mingw |
21 | | #define _WIN32_WINNT 0x400 // maybe working alternative for mingw |
22 | | #include <stdio.h>//D |
23 | | #include <fcntl.h> |
24 | | #include <io.h> |
25 | | #include <time.h> |
26 | | #include <winsock2.h> |
27 | | #include <process.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 | | |
35 | | #include <pthread.h> |
36 | | #define sigset_t int |
37 | | #define sigfillset(a) |
38 | | #define pthread_sigmask(a,b,c) |
39 | | #define sigaddset(a,b) |
40 | | #define sigemptyset(s) |
41 | | |
42 | | typedef pthread_mutex_t xmutex_t; |
43 | | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
44 | | #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
45 | | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
46 | | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
47 | | |
48 | | typedef pthread_cond_t xcond_t; |
49 | | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
50 | | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
51 | | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
52 | | #define X_COND_BROADCAST(cond) pthread_cond_broadcast (&(cond)) |
53 | | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
54 | | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
55 | | |
56 | | typedef pthread_t xthread_t; |
57 | | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
58 | | #define X_THREAD_ATFORK(a,b,c) |
59 | | |
60 | | static int |
61 | | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
62 | | { |
63 | | int retval; |
64 | | pthread_attr_t attr; |
65 | | |
66 | | pthread_attr_init (&attr); |
67 | | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
68 | | |
69 | | retval = pthread_create (tid, &attr, proc, arg) == 0; |
70 | | |
71 | | pthread_attr_destroy (&attr); |
72 | | |
73 | | return retval; |
74 | | } |
75 | | |
76 | | #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) |
77 | | #define respipe_write(a,b,c) send ((a), (b), (c), 0) |
78 | | #define respipe_close(a) PerlSock_closesocket ((a)) |
79 | | |
80 | | #else |
81 | | ///////////////////////////////////////////////////////////////////////////// |
82 | | |
83 | | #if __linux && !defined(_GNU_SOURCE) |
84 | | # define _GNU_SOURCE |
85 | | #endif |
86 | | |
87 | | /* just in case */ |
88 | | #define _REENTRANT 1 |
89 | | |
90 | | #if __solaris |
91 | | # define _POSIX_PTHREAD_SEMANTICS 1 |
92 | | /* try to bribe solaris headers into providing a current pthread API |
93 | | * despite environment being configured for an older version. |
94 | | */ |
95 | | # define __EXTENSIONS__ 1 |
96 | | #endif |
97 | | |
98 | | #include <unistd.h> |
99 | | #include <fcntl.h> |
100 | | #include <signal.h> |
101 | | #include <limits.h> |
102 | | #include <pthread.h> |
103 | | |
104 | | typedef pthread_mutex_t xmutex_t; |
105 | | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
106 | | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
107 | | # define X_MUTEX_CREATE(mutex) \ |
108 | 0 | do { \ |
109 | 0 | pthread_mutexattr_t attr; \ |
110 | 0 | pthread_mutexattr_init (&attr); \ |
111 | 0 | pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \ |
112 | 0 | pthread_mutex_init (&(mutex), &attr); \ |
113 | 0 | } while (0) |
114 | | #else |
115 | | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
116 | | # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
117 | | #endif |
118 | 0 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
119 | 0 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
120 | | |
121 | | typedef pthread_cond_t xcond_t; |
122 | | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
123 | 0 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
124 | 0 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
125 | 0 | #define X_COND_BROADCAST(cond) pthread_cond_broadcast (&(cond)) |
126 | 0 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
127 | 0 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
128 | | |
129 | | typedef pthread_t xthread_t; |
130 | | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
131 | 0 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
132 | | |
133 | | // the broken bsd's once more |
134 | | #ifndef PTHREAD_STACK_MIN |
135 | | # define PTHREAD_STACK_MIN 0 |
136 | | #endif |
137 | | |
138 | | #ifndef X_STACKSIZE |
139 | | # define X_STACKSIZE sizeof (void *) * 4096 |
140 | | #endif |
141 | | |
142 | | static int |
143 | | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
144 | 0 | { |
145 | 0 | int retval; |
146 | 0 | sigset_t fullsigset, oldsigset; |
147 | 0 | pthread_attr_t attr; |
148 | |
|
149 | 0 | pthread_attr_init (&attr); |
150 | 0 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
151 | 0 | if (X_STACKSIZE != 0) |
152 | 0 | pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); |
153 | 0 | #ifdef PTHREAD_SCOPE_PROCESS |
154 | 0 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
155 | 0 | #endif |
156 | |
|
157 | 0 | sigfillset (&fullsigset); |
158 | |
|
159 | 0 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
160 | 0 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
161 | 0 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
162 | |
|
163 | 0 | pthread_attr_destroy (&attr); |
164 | |
|
165 | 0 | return retval; |
166 | 0 | } |
167 | | |
168 | | #define respipe_read(a,b,c) read ((a), (b), (c)) |
169 | | #define respipe_write(a,b,c) write ((a), (b), (c)) |
170 | | #define respipe_close(a) close ((a)) |
171 | | |
172 | | #endif |
173 | | |
174 | | #if __linux && __GNUC__ >= 4 && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 3 && 0 /* also check arch */ |
175 | | /* __thread has little to no advantage over pthread_* in most configurations, so this is not used */ |
176 | | # define X_TLS_DECLARE(varname) __thread void *varname |
177 | | # define X_TLS_INIT(varname) |
178 | | # define X_TLS_SET(varname,value) varname = (value) |
179 | | # define X_TLS_GET(varname) varname |
180 | | #else |
181 | | # define X_TLS_DECLARE(varname) pthread_key_t varname |
182 | | # define X_TLS_INIT(varname) do { if (pthread_key_create (&(varname), 0)) abort (); } while (0) |
183 | | # define X_TLS_SET(varname,value) pthread_setspecific (varname, (value)) |
184 | | # define X_TLS_GET(varname) pthread_getspecific (varname) |
185 | | #endif |
186 | | |
187 | | #endif |
188 | | |