| line | stmt | bran | cond | sub | pod | time | code | 
| 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 |  |  |  |  |  |  | #  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 |  |  |  |  |  |  | #if _WIN32_WINNT < 0x400 | 
| 22 |  |  |  |  |  |  | # define _WIN32_WINNT 0x400 // maybe working alternative for mingw | 
| 23 |  |  |  |  |  |  | #endif | 
| 24 |  |  |  |  |  |  | #include //D | 
| 25 |  |  |  |  |  |  | #include | 
| 26 |  |  |  |  |  |  | #include | 
| 27 |  |  |  |  |  |  | #include | 
| 28 |  |  |  |  |  |  | #include | 
| 29 |  |  |  |  |  |  | #include | 
| 30 |  |  |  |  |  |  | #include | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | /* work around some bugs in ptw32 */ | 
| 33 |  |  |  |  |  |  | #if defined(__MINGW32__) && defined(_TIMESPEC_DEFINED) | 
| 34 |  |  |  |  |  |  | #define HAVE_STRUCT_TIMESPEC 1 | 
| 35 |  |  |  |  |  |  | #endif | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | #include | 
| 38 |  |  |  |  |  |  | #ifndef pthread_sigmask | 
| 39 |  |  |  |  |  |  | # define sigset_t int | 
| 40 |  |  |  |  |  |  | # define sigfillset(a) | 
| 41 |  |  |  |  |  |  | # define pthread_sigmask(a,b,c) | 
| 42 |  |  |  |  |  |  | # define sigaddset(a,b) | 
| 43 |  |  |  |  |  |  | # define sigemptyset(s) | 
| 44 |  |  |  |  |  |  | #endif | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | typedef pthread_mutex_t xmutex_t; | 
| 47 |  |  |  |  |  |  | #define X_MUTEX_INIT           PTHREAD_MUTEX_INITIALIZER | 
| 48 |  |  |  |  |  |  | #define X_MUTEX_CREATE(mutex)  pthread_mutex_init (&(mutex), 0) | 
| 49 |  |  |  |  |  |  | #define X_LOCK(mutex)          pthread_mutex_lock (&(mutex)) | 
| 50 |  |  |  |  |  |  | #define X_UNLOCK(mutex)        pthread_mutex_unlock (&(mutex)) | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | typedef pthread_cond_t xcond_t; | 
| 53 |  |  |  |  |  |  | #define X_COND_INIT                     PTHREAD_COND_INITIALIZER | 
| 54 |  |  |  |  |  |  | #define X_COND_CREATE(cond)		pthread_cond_init (&(cond), 0) | 
| 55 |  |  |  |  |  |  | #define X_COND_SIGNAL(cond)             pthread_cond_signal (&(cond)) | 
| 56 |  |  |  |  |  |  | #define X_COND_WAIT(cond,mutex)         pthread_cond_wait (&(cond), &(mutex)) | 
| 57 |  |  |  |  |  |  | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | typedef pthread_t xthread_t; | 
| 60 |  |  |  |  |  |  | #define X_THREAD_PROC(name) static void *name (void *thr_arg) | 
| 61 |  |  |  |  |  |  | #define X_THREAD_ATFORK(a,b,c) | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | static int | 
| 64 |  |  |  |  |  |  | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) | 
| 65 |  |  |  |  |  |  | { | 
| 66 |  |  |  |  |  |  | int retval; | 
| 67 |  |  |  |  |  |  | #if 0 | 
| 68 |  |  |  |  |  |  | pthread_attr_t attr; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | pthread_attr_init (&attr); | 
| 71 |  |  |  |  |  |  | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | retval = pthread_create (tid, &attr, proc, arg) == 0; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | pthread_attr_destroy (&attr); | 
| 76 |  |  |  |  |  |  | #else | 
| 77 |  |  |  |  |  |  | retval = pthread_create (tid, 0, proc, arg) == 0; | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | if (retval) | 
| 80 |  |  |  |  |  |  | pthread_detach (*tid); | 
| 81 |  |  |  |  |  |  | #endif | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | return retval; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | #define respipe_read(a,b,c)  PerlSock_recv ((a), (b), (c), 0) | 
| 87 |  |  |  |  |  |  | #define respipe_write(a,b,c) send ((a), (b), (c), 0) | 
| 88 |  |  |  |  |  |  | #define respipe_close(a)     PerlSock_closesocket ((a)) | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | #else | 
| 91 |  |  |  |  |  |  | ///////////////////////////////////////////////////////////////////////////// | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | #if __linux && !defined(_GNU_SOURCE) | 
| 94 |  |  |  |  |  |  | # define _GNU_SOURCE | 
| 95 |  |  |  |  |  |  | #endif | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | /* just in case */ | 
| 98 |  |  |  |  |  |  | #define _REENTRANT 1 | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | #if __solaris | 
| 101 |  |  |  |  |  |  | # define _POSIX_PTHREAD_SEMANTICS 1 | 
| 102 |  |  |  |  |  |  | /* try to bribe solaris headers into providing a current pthread API | 
| 103 |  |  |  |  |  |  | * despite environment being configured for an older version. | 
| 104 |  |  |  |  |  |  | */ | 
| 105 |  |  |  |  |  |  | # define __EXTENSIONS__ 1 | 
| 106 |  |  |  |  |  |  | #endif | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | #include | 
| 109 |  |  |  |  |  |  | #include | 
| 110 |  |  |  |  |  |  | #include | 
| 111 |  |  |  |  |  |  | #include | 
| 112 |  |  |  |  |  |  | #include | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | typedef pthread_mutex_t xmutex_t; | 
| 115 |  |  |  |  |  |  | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) | 
| 116 |  |  |  |  |  |  | # define X_MUTEX_INIT		PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP | 
| 117 |  |  |  |  |  |  | # define X_MUTEX_CREATE(mutex)						\ | 
| 118 |  |  |  |  |  |  | do {									\ | 
| 119 |  |  |  |  |  |  | pthread_mutexattr_t attr;						\ | 
| 120 |  |  |  |  |  |  | pthread_mutexattr_init (&attr);					\ | 
| 121 |  |  |  |  |  |  | pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);	\ | 
| 122 |  |  |  |  |  |  | pthread_mutex_init (&(mutex), &attr);				\ | 
| 123 |  |  |  |  |  |  | } while (0) | 
| 124 |  |  |  |  |  |  | #else | 
| 125 |  |  |  |  |  |  | # define X_MUTEX_INIT		PTHREAD_MUTEX_INITIALIZER | 
| 126 |  |  |  |  |  |  | # define X_MUTEX_CREATE(mutex)	pthread_mutex_init (&(mutex), 0) | 
| 127 |  |  |  |  |  |  | #endif | 
| 128 |  |  |  |  |  |  | #define X_LOCK(mutex)		pthread_mutex_lock   (&(mutex)) | 
| 129 |  |  |  |  |  |  | #define X_UNLOCK(mutex)		pthread_mutex_unlock (&(mutex)) | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | typedef pthread_cond_t xcond_t; | 
| 132 |  |  |  |  |  |  | #define X_COND_INIT			PTHREAD_COND_INITIALIZER | 
| 133 |  |  |  |  |  |  | #define X_COND_CREATE(cond)		pthread_cond_init (&(cond), 0) | 
| 134 |  |  |  |  |  |  | #define X_COND_SIGNAL(cond)		pthread_cond_signal (&(cond)) | 
| 135 |  |  |  |  |  |  | #define X_COND_WAIT(cond,mutex)		pthread_cond_wait (&(cond), &(mutex)) | 
| 136 |  |  |  |  |  |  | #define X_COND_TIMEDWAIT(cond,mutex,to)	pthread_cond_timedwait (&(cond), &(mutex), &(to)) | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | typedef pthread_t xthread_t; | 
| 139 |  |  |  |  |  |  | #define X_THREAD_PROC(name) static void *name (void *thr_arg) | 
| 140 |  |  |  |  |  |  | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | // the broken bsd's once more | 
| 143 |  |  |  |  |  |  | #ifndef PTHREAD_STACK_MIN | 
| 144 |  |  |  |  |  |  | # define PTHREAD_STACK_MIN 0 | 
| 145 |  |  |  |  |  |  | #endif | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | #ifndef X_STACKSIZE | 
| 148 |  |  |  |  |  |  | # define X_STACKSIZE sizeof (void *) * 4096 | 
| 149 |  |  |  |  |  |  | #endif | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | static int | 
| 152 | 22 |  |  |  |  |  | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) | 
| 153 |  |  |  |  |  |  | { | 
| 154 |  |  |  |  |  |  | int retval; | 
| 155 |  |  |  |  |  |  | sigset_t fullsigset, oldsigset; | 
| 156 |  |  |  |  |  |  | pthread_attr_t attr; | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 22 |  |  |  |  |  | pthread_attr_init (&attr); | 
| 159 | 22 |  |  |  |  |  | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); | 
| 160 | 22 |  |  |  |  |  | pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); | 
| 161 |  |  |  |  |  |  | #ifdef PTHREAD_SCOPE_PROCESS | 
| 162 | 22 |  |  |  |  |  | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); | 
| 163 |  |  |  |  |  |  | #endif | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 22 |  |  |  |  |  | sigfillset (&fullsigset); | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 22 |  |  |  |  |  | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); | 
| 168 | 22 |  |  |  |  |  | retval = pthread_create (tid, &attr, proc, arg) == 0; | 
| 169 | 22 |  |  |  |  |  | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 22 |  |  |  |  |  | pthread_attr_destroy (&attr); | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 22 |  |  |  |  |  | return retval; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | #define respipe_read(a,b,c)  read  ((a), (b), (c)) | 
| 177 |  |  |  |  |  |  | #define respipe_write(a,b,c) write ((a), (b), (c)) | 
| 178 |  |  |  |  |  |  | #define respipe_close(a)     close ((a)) | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | #endif | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | #if __linux && __GNUC__ >= 4 && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 3 && 0 /* also check arch */ | 
| 183 |  |  |  |  |  |  | /* __thread has little to no advantage over pthread_* in most configurations, so this is not used */ | 
| 184 |  |  |  |  |  |  | # define X_TLS_DECLARE(varname)   __thread void *varname | 
| 185 |  |  |  |  |  |  | # define X_TLS_INIT(varname) | 
| 186 |  |  |  |  |  |  | # define X_TLS_SET(varname,value) varname = (value) | 
| 187 |  |  |  |  |  |  | # define X_TLS_GET(varname)       varname | 
| 188 |  |  |  |  |  |  | #else | 
| 189 |  |  |  |  |  |  | # define X_TLS_DECLARE(varname)   pthread_key_t varname | 
| 190 |  |  |  |  |  |  | # define X_TLS_INIT(varname)      do { if (pthread_key_create (&(varname), 0)) abort (); } while (0) | 
| 191 |  |  |  |  |  |  | # define X_TLS_SET(varname,value) pthread_setspecific (varname, (value)) | 
| 192 |  |  |  |  |  |  | # define X_TLS_GET(varname)       pthread_getspecific (varname) | 
| 193 |  |  |  |  |  |  | #endif | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | #endif | 
| 196 |  |  |  |  |  |  |  |