| 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 |
#ifndef _GLTHREAD_THREAD_H |
| 71 |
#define _GLTHREAD_THREAD_H |
| 72 |
|
| 73 |
#include <errno.h> |
| 74 |
#include <stdlib.h> |
| 75 |
|
| 76 |
#if !defined c11_threads_in_use |
| 77 |
# if HAVE_THREADS_H && USE_POSIX_THREADS_WEAK |
| 78 |
# include <threads.h> |
| 79 |
# pragma weak thrd_exit |
| 80 |
# define c11_threads_in_use() (thrd_exit != NULL) |
| 81 |
# else |
| 82 |
# define c11_threads_in_use() 0 |
| 83 |
# endif |
| 84 |
#endif |
| 85 |
|
| 86 |
#ifndef _GL_INLINE_HEADER_BEGIN |
| 87 |
#error "Please include config.h first." |
| 88 |
#endif |
| 89 |
_GL_INLINE_HEADER_BEGIN |
| 90 |
#ifndef _GLTHREAD_THREAD_INLINE |
| 91 |
# define _GLTHREAD_THREAD_INLINE _GL_INLINE |
| 92 |
#endif |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#if USE_ISOC_THREADS |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
# include <threads.h> |
| 101 |
|
| 102 |
# ifdef __cplusplus |
| 103 |
extern "C" { |
| 104 |
# endif |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
typedef struct thrd_with_exitvalue *gl_thread_t; |
| 109 |
extern int glthread_create (gl_thread_t *threadp, |
| 110 |
void *(*func) (void *), void *arg); |
| 111 |
# define glthread_sigmask(HOW, SET, OSET) \ |
| 112 |
pthread_sigmask (HOW, SET, OSET) |
| 113 |
extern int glthread_join (gl_thread_t thread, void **return_value_ptr); |
| 114 |
extern gl_thread_t gl_thread_self (void); |
| 115 |
# define gl_thread_self_pointer() \ |
| 116 |
(void *) gl_thread_self () |
| 117 |
extern _Noreturn void gl_thread_exit (void *return_value); |
| 118 |
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 |
| 119 |
|
| 120 |
# ifdef __cplusplus |
| 121 |
} |
| 122 |
# endif |
| 123 |
|
| 124 |
#endif |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
# include <pthread.h> |
| 133 |
|
| 134 |
|
| 135 |
# if defined __sgi |
| 136 |
# include <unistd.h> |
| 137 |
# endif |
| 138 |
|
| 139 |
# if USE_POSIX_THREADS_WEAK |
| 140 |
|
| 141 |
|
| 142 |
# include <signal.h> |
| 143 |
# endif |
| 144 |
|
| 145 |
# ifdef __cplusplus |
| 146 |
extern "C" { |
| 147 |
# endif |
| 148 |
|
| 149 |
# if PTHREAD_IN_USE_DETECTION_HARD |
| 150 |
|
| 151 |
|
| 152 |
# define pthread_in_use() \ |
| 153 |
glthread_in_use () |
| 154 |
extern int glthread_in_use (void); |
| 155 |
|
| 156 |
# endif |
| 157 |
|
| 158 |
# if USE_POSIX_THREADS_WEAK |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
# ifndef pthread_sigmask |
| 178 |
# pragma weak pthread_sigmask |
| 179 |
# endif |
| 180 |
|
| 181 |
# pragma weak pthread_join |
| 182 |
# ifndef pthread_self |
| 183 |
# pragma weak pthread_self |
| 184 |
# endif |
| 185 |
# pragma weak pthread_exit |
| 186 |
# if HAVE_PTHREAD_ATFORK |
| 187 |
# pragma weak pthread_atfork |
| 188 |
# endif |
| 189 |
|
| 190 |
# if !PTHREAD_IN_USE_DETECTION_HARD |
| 191 |
# pragma weak pthread_mutexattr_gettype |
| 192 |
# define pthread_in_use() \ |
| 193 |
(pthread_mutexattr_gettype != NULL || c11_threads_in_use ()) |
| 194 |
# endif |
| 195 |
|
| 196 |
# else |
| 197 |
|
| 198 |
# if !PTHREAD_IN_USE_DETECTION_HARD |
| 199 |
# define pthread_in_use() 1 |
| 200 |
# endif |
| 201 |
|
| 202 |
# endif |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
typedef pthread_t gl_thread_t; |
| 210 |
# define glthread_create(THREADP, FUNC, ARG) \ |
| 211 |
(pthread_in_use () ? pthread_create (THREADP, NULL, FUNC, ARG) : ENOSYS) |
| 212 |
# define glthread_sigmask(HOW, SET, OSET) \ |
| 213 |
(pthread_in_use () ? pthread_sigmask (HOW, SET, OSET) : 0) |
| 214 |
# define glthread_join(THREAD, RETVALP) \ |
| 215 |
(pthread_in_use () ? pthread_join (THREAD, RETVALP) : 0) |
| 216 |
# ifdef PTW32_VERSION |
| 217 |
|
| 218 |
|
| 219 |
# define gl_thread_self() \ |
| 220 |
(pthread_in_use () ? pthread_self () : gl_null_thread) |
| 221 |
# define gl_thread_self_pointer() \ |
| 222 |
(pthread_in_use () ? pthread_self ().p : NULL) |
| 223 |
extern const gl_thread_t gl_null_thread; |
| 224 |
# elif defined __MVS__ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
# define gl_thread_self() \ |
| 229 |
(pthread_in_use () ? pthread_self () : gl_null_thread) |
| 230 |
# define gl_thread_self_pointer() \ |
| 231 |
(pthread_in_use () ? *((void **) pthread_self ().__) : NULL) |
| 232 |
extern const gl_thread_t gl_null_thread; |
| 233 |
# else |
| 234 |
# define gl_thread_self() \ |
| 235 |
(pthread_in_use () ? pthread_self () : (pthread_t) NULL) |
| 236 |
# define gl_thread_self_pointer() \ |
| 237 |
(pthread_in_use () ? (void *) pthread_self () : NULL) |
| 238 |
# endif |
| 239 |
# define gl_thread_exit(RETVAL) \ |
| 240 |
(pthread_in_use () ? pthread_exit (RETVAL) : 0) |
| 241 |
|
| 242 |
# if HAVE_PTHREAD_ATFORK |
| 243 |
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \ |
| 244 |
(pthread_in_use () ? pthread_atfork (PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) : 0) |
| 245 |
# else |
| 246 |
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 |
| 247 |
# endif |
| 248 |
|
| 249 |
# ifdef __cplusplus |
| 250 |
} |
| 251 |
# endif |
| 252 |
|
| 253 |
#endif |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
#if USE_WINDOWS_THREADS |
| 258 |
|
| 259 |
# define WIN32_LEAN_AND_MEAN |
| 260 |
# include <windows.h> |
| 261 |
|
| 262 |
# include "windows-thread.h" |
| 263 |
|
| 264 |
# ifdef __cplusplus |
| 265 |
extern "C" { |
| 266 |
# endif |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
typedef glwthread_thread_t gl_thread_t; |
| 271 |
# define glthread_create(THREADP, FUNC, ARG) \ |
| 272 |
glwthread_thread_create (THREADP, 0, FUNC, ARG) |
| 273 |
# define glthread_sigmask(HOW, SET, OSET) \ |
| 274 |
0 |
| 275 |
# define glthread_join(THREAD, RETVALP) \ |
| 276 |
glwthread_thread_join (THREAD, RETVALP) |
| 277 |
# define gl_thread_self() \ |
| 278 |
glwthread_thread_self () |
| 279 |
# define gl_thread_self_pointer() \ |
| 280 |
gl_thread_self () |
| 281 |
# define gl_thread_exit(RETVAL) \ |
| 282 |
glwthread_thread_exit (RETVAL) |
| 283 |
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 |
| 284 |
|
| 285 |
# ifdef __cplusplus |
| 286 |
} |
| 287 |
# endif |
| 288 |
|
| 289 |
#endif |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS) |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
typedef int gl_thread_t; |
| 298 |
# define glthread_create(THREADP, FUNC, ARG) ENOSYS |
| 299 |
# define glthread_sigmask(HOW, SET, OSET) 0 |
| 300 |
# define glthread_join(THREAD, RETVALP) 0 |
| 301 |
# define gl_thread_self() 0 |
| 302 |
# define gl_thread_self_pointer() \ |
| 303 |
((void *) gl_thread_self ()) |
| 304 |
# define gl_thread_exit(RETVAL) 0 |
| 305 |
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 |
| 306 |
|
| 307 |
#endif |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
#ifdef __cplusplus |
| 314 |
extern "C" { |
| 315 |
#endif |
| 316 |
|
| 317 |
_GLTHREAD_THREAD_INLINE gl_thread_t |
| 318 |
gl_thread_create (void *(*func) (void *arg), void *arg) |
| 319 |
{ |
| 320 |
gl_thread_t thread; |
| 321 |
int ret; |
| 322 |
|
| 323 |
ret = glthread_create (&thread, func, arg); |
| 324 |
if (ret != 0) |
| 325 |
abort (); |
| 326 |
return thread; |
| 327 |
} |
| 328 |
#define gl_thread_sigmask(HOW, SET, OSET) \ |
| 329 |
do \ |
| 330 |
{ \ |
| 331 |
if (glthread_sigmask (HOW, SET, OSET)) \ |
| 332 |
abort (); \ |
| 333 |
} \ |
| 334 |
while (0) |
| 335 |
#define gl_thread_join(THREAD, RETVAL) \ |
| 336 |
do \ |
| 337 |
{ \ |
| 338 |
if (glthread_join (THREAD, RETVAL)) \ |
| 339 |
abort (); \ |
| 340 |
} \ |
| 341 |
while (0) |
| 342 |
#define gl_thread_atfork(PREPARE, PARENT, CHILD) \ |
| 343 |
do \ |
| 344 |
{ \ |
| 345 |
if (glthread_atfork (PREPARE, PARENT, CHILD)) \ |
| 346 |
abort (); \ |
| 347 |
} \ |
| 348 |
while (0) |
| 349 |
|
| 350 |
#ifdef __cplusplus |
| 351 |
} |
| 352 |
#endif |
| 353 |
|
| 354 |
_GL_INLINE_HEADER_END |
| 355 |
|
| 356 |
#endif |