Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 2 | /* Posix threads interface */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 4 | #include <stdlib.h> |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 5 | #include <string.h> |
Martin v. Löwis | a7a76d3 | 2002-10-04 07:21:24 +0000 | [diff] [blame] | 6 | #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR) |
Jack Jansen | 7668957 | 2002-01-15 20:36:14 +0000 | [diff] [blame] | 7 | #define destructor xxdestructor |
| 8 | #endif |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 9 | #include <pthread.h> |
Martin v. Löwis | a7a76d3 | 2002-10-04 07:21:24 +0000 | [diff] [blame] | 10 | #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR) |
Jack Jansen | 7668957 | 2002-01-15 20:36:14 +0000 | [diff] [blame] | 11 | #undef destructor |
| 12 | #endif |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 13 | #include <signal.h> |
Martin v. Löwis | 42ab61e | 2002-03-17 17:19:00 +0000 | [diff] [blame] | 14 | |
Jake Tesler | 4959c33 | 2019-05-12 10:08:24 -0700 | [diff] [blame] | 15 | #if defined(__linux__) |
| 16 | #include <sys/syscall.h> |
| 17 | #elif defined(__FreeBSD__) |
| 18 | #include <pthread_np.h> |
| 19 | #endif |
| 20 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | /* The POSIX spec requires that use of pthread_attr_setstacksize |
| 22 | be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */ |
| 23 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 24 | #ifndef THREAD_STACK_SIZE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | #define THREAD_STACK_SIZE 0 /* use default stack size */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 26 | #endif |
Ned Deily | 9a7c524 | 2011-05-28 00:19:56 -0700 | [diff] [blame] | 27 | |
Ned Deily | 7ca97d5 | 2012-03-13 11:18:18 -0700 | [diff] [blame] | 28 | /* The default stack size for new threads on OSX and BSD is small enough that |
| 29 | * we'll get hard crashes instead of 'maximum recursion depth exceeded' |
| 30 | * exceptions. |
| 31 | * |
| 32 | * The default stack sizes below are the empirically determined minimal stack |
| 33 | * sizes where a simple recursive function doesn't cause a hard crash. |
| 34 | */ |
| 35 | #if defined(__APPLE__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 |
| 36 | #undef THREAD_STACK_SIZE |
| 37 | #define THREAD_STACK_SIZE 0x500000 |
| 38 | #endif |
| 39 | #if defined(__FreeBSD__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 |
Ned Deily | 9a7c524 | 2011-05-28 00:19:56 -0700 | [diff] [blame] | 40 | #undef THREAD_STACK_SIZE |
| 41 | #define THREAD_STACK_SIZE 0x400000 |
| 42 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 43 | /* for safety, ensure a viable minimum stacksize */ |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 44 | #define THREAD_STACK_MIN 0x8000 /* 32 KiB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 45 | #else /* !_POSIX_THREAD_ATTR_STACKSIZE */ |
| 46 | #ifdef THREAD_STACK_SIZE |
| 47 | #error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined" |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Martin v. Löwis | 42ab61e | 2002-03-17 17:19:00 +0000 | [diff] [blame] | 51 | /* The POSIX spec says that implementations supporting the sem_* |
| 52 | family of functions must indicate this by defining |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | _POSIX_SEMAPHORES. */ |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 54 | #ifdef _POSIX_SEMAPHORES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so |
Martin v. Löwis | 8b8fb3d | 2005-03-28 12:34:20 +0000 | [diff] [blame] | 56 | we need to add 0 to make it work there as well. */ |
| 57 | #if (_POSIX_SEMAPHORES+0) == -1 |
Anthony Baxter | 19b2369 | 2005-03-16 04:15:07 +0000 | [diff] [blame] | 58 | #define HAVE_BROKEN_POSIX_SEMAPHORES |
| 59 | #else |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 60 | #include <semaphore.h> |
| 61 | #include <errno.h> |
| 62 | #endif |
Anthony Baxter | 19b2369 | 2005-03-16 04:15:07 +0000 | [diff] [blame] | 63 | #endif |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 65 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 66 | /* Whether or not to use semaphores directly rather than emulating them with |
| 67 | * mutexes and condition variables: |
| 68 | */ |
Antoine Pitrou | 19f8edc | 2010-10-10 08:37:22 +0000 | [diff] [blame] | 69 | #if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \ |
| 70 | defined(HAVE_SEM_TIMEDWAIT)) |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 71 | # define USE_SEMAPHORES |
| 72 | #else |
| 73 | # undef USE_SEMAPHORES |
| 74 | #endif |
| 75 | |
| 76 | |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 77 | /* On platforms that don't use standard POSIX threads pthread_sigmask() |
| 78 | * isn't present. DEC threads uses sigprocmask() instead as do most |
| 79 | * other UNIX International compliant systems that don't have the full |
| 80 | * pthread implementation. |
| 81 | */ |
Jason Tishler | fac083d | 2003-07-22 15:20:49 +0000 | [diff] [blame] | 82 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 83 | # define SET_THREAD_SIGMASK pthread_sigmask |
| 84 | #else |
| 85 | # define SET_THREAD_SIGMASK sigprocmask |
| 86 | #endif |
| 87 | |
| 88 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 89 | /* We assume all modern POSIX systems have gettimeofday() */ |
| 90 | #ifdef GETTIMEOFDAY_NO_TZ |
| 91 | #define GETTIMEOFDAY(ptv) gettimeofday(ptv) |
| 92 | #else |
| 93 | #define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL) |
| 94 | #endif |
| 95 | |
| 96 | #define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \ |
| 97 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | struct timeval tv; \ |
| 99 | GETTIMEOFDAY(&tv); \ |
| 100 | tv.tv_usec += microseconds % 1000000; \ |
| 101 | tv.tv_sec += microseconds / 1000000; \ |
| 102 | tv.tv_sec += tv.tv_usec / 1000000; \ |
| 103 | tv.tv_usec %= 1000000; \ |
| 104 | ts.tv_sec = tv.tv_sec; \ |
| 105 | ts.tv_nsec = tv.tv_usec * 1000; \ |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 106 | } while(0) |
| 107 | |
| 108 | |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 109 | /* |
| 110 | * pthread_cond support |
| 111 | */ |
| 112 | |
| 113 | #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) |
| 114 | // monotonic is supported statically. It doesn't mean it works on runtime. |
| 115 | #define CONDATTR_MONOTONIC |
| 116 | #endif |
| 117 | |
| 118 | // NULL when pthread_condattr_setclock(CLOCK_MONOTONIC) is not supported. |
| 119 | static pthread_condattr_t *condattr_monotonic = NULL; |
| 120 | |
| 121 | static void |
| 122 | init_condattr() |
| 123 | { |
| 124 | #ifdef CONDATTR_MONOTONIC |
| 125 | static pthread_condattr_t ca; |
| 126 | pthread_condattr_init(&ca); |
| 127 | if (pthread_condattr_setclock(&ca, CLOCK_MONOTONIC) == 0) { |
| 128 | condattr_monotonic = &ca; // Use monotonic clock |
| 129 | } |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | int |
| 134 | _PyThread_cond_init(PyCOND_T *cond) |
| 135 | { |
| 136 | return pthread_cond_init(cond, condattr_monotonic); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | _PyThread_cond_after(long long us, struct timespec *abs) |
| 141 | { |
| 142 | #ifdef CONDATTR_MONOTONIC |
| 143 | if (condattr_monotonic) { |
| 144 | clock_gettime(CLOCK_MONOTONIC, abs); |
| 145 | abs->tv_sec += us / 1000000; |
| 146 | abs->tv_nsec += (us % 1000000) * 1000; |
| 147 | abs->tv_sec += abs->tv_nsec / 1000000000; |
| 148 | abs->tv_nsec %= 1000000000; |
| 149 | return; |
| 150 | } |
| 151 | #endif |
| 152 | |
| 153 | struct timespec ts; |
| 154 | MICROSECONDS_TO_TIMESPEC(us, ts); |
| 155 | *abs = ts; |
| 156 | } |
| 157 | |
| 158 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 159 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 160 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 161 | * following are undefined: |
| 162 | * -> a thread tries to lock a mutex it already has locked |
| 163 | * -> a thread tries to unlock a mutex locked by a different thread |
| 164 | * pthread mutexes are designed for serializing threads over short pieces |
| 165 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 166 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 167 | * |
| 168 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 169 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 170 | * instantly, it is, else the pair is used to block the thread until the |
| 171 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 172 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 174 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | char locked; /* 0=unlocked, 1=locked */ |
| 176 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 177 | pthread_cond_t lock_released; |
| 178 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 179 | } pthread_lock; |
| 180 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 181 | #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; } |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 182 | #define CHECK_STATUS_PTHREAD(name) if (status != 0) { fprintf(stderr, \ |
| 183 | "%s: %s\n", name, strerror(status)); error = 1; } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 185 | /* |
| 186 | * Initialization. |
| 187 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 188 | static void |
| 189 | PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 190 | { |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 191 | #if defined(_AIX) && defined(__GNUC__) |
Antoine Pitrou | 9a00e0a | 2013-06-18 22:17:48 +0200 | [diff] [blame] | 192 | extern void pthread_init(void); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | pthread_init(); |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 194 | #endif |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 195 | init_condattr(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Thread support. |
| 200 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 201 | |
Siddhesh Poyarekar | 9eea6ea | 2018-11-30 20:44:25 +0530 | [diff] [blame] | 202 | /* bpo-33015: pythread_callback struct and pythread_wrapper() cast |
| 203 | "void func(void *)" to "void* func(void *)": always return NULL. |
| 204 | |
| 205 | PyThread_start_new_thread() uses "void func(void *)" type, whereas |
| 206 | pthread_create() requires a void* return value. */ |
| 207 | typedef struct { |
| 208 | void (*func) (void *); |
| 209 | void *arg; |
| 210 | } pythread_callback; |
| 211 | |
| 212 | static void * |
| 213 | pythread_wrapper(void *arg) |
| 214 | { |
| 215 | /* copy func and func_arg and free the temporary structure */ |
| 216 | pythread_callback *callback = arg; |
| 217 | void (*func)(void *) = callback->func; |
| 218 | void *func_arg = callback->arg; |
| 219 | PyMem_RawFree(arg); |
| 220 | |
| 221 | func(func_arg); |
| 222 | return NULL; |
| 223 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 224 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 225 | unsigned long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 226 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | pthread_t th; |
| 229 | int status; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 230 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | pthread_attr_t attrs; |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 232 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 233 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | size_t tss; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 235 | #endif |
| 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | dprintf(("PyThread_start_new_thread called\n")); |
| 238 | if (!initialized) |
| 239 | PyThread_init_thread(); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 241 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | if (pthread_attr_init(&attrs) != 0) |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 243 | return PYTHREAD_INVALID_THREAD_ID; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 244 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 245 | #if defined(THREAD_STACK_SIZE) |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 246 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 247 | size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0; |
| 248 | tss = (stacksize != 0) ? stacksize : THREAD_STACK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | if (tss != 0) { |
| 250 | if (pthread_attr_setstacksize(&attrs, tss) != 0) { |
| 251 | pthread_attr_destroy(&attrs); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 252 | return PYTHREAD_INVALID_THREAD_ID; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 255 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 256 | #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 258 | #endif |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 259 | |
Siddhesh Poyarekar | 9eea6ea | 2018-11-30 20:44:25 +0530 | [diff] [blame] | 260 | pythread_callback *callback = PyMem_RawMalloc(sizeof(pythread_callback)); |
| 261 | |
| 262 | if (callback == NULL) { |
| 263 | return PYTHREAD_INVALID_THREAD_ID; |
| 264 | } |
| 265 | |
| 266 | callback->func = func; |
| 267 | callback->arg = arg; |
| 268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | status = pthread_create(&th, |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 270 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | &attrs, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 272 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | (pthread_attr_t*)NULL, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 274 | #endif |
Siddhesh Poyarekar | 9eea6ea | 2018-11-30 20:44:25 +0530 | [diff] [blame] | 275 | pythread_wrapper, callback); |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 276 | |
Fred Drake | 03459a5 | 2001-11-09 16:00:41 +0000 | [diff] [blame] | 277 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | pthread_attr_destroy(&attrs); |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 279 | #endif |
Siddhesh Poyarekar | 9eea6ea | 2018-11-30 20:44:25 +0530 | [diff] [blame] | 280 | |
| 281 | if (status != 0) { |
| 282 | PyMem_RawFree(callback); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 283 | return PYTHREAD_INVALID_THREAD_ID; |
Siddhesh Poyarekar | 9eea6ea | 2018-11-30 20:44:25 +0530 | [diff] [blame] | 284 | } |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | pthread_detach(th); |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 287 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 288 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 289 | return (unsigned long) th; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 290 | #else |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 291 | return (unsigned long) *(unsigned long *) &th; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 292 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 295 | /* XXX This implementation is considered (to quote Tim Peters) "inherently |
| 296 | hosed" because: |
Skip Montanaro | 6babcc2 | 2004-03-03 08:42:23 +0000 | [diff] [blame] | 297 | - It does not guarantee the promise that a non-zero integer is returned. |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 298 | - The cast to unsigned long is inherently unsafe. |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 299 | - It is not clear that the 'volatile' (for AIX?) are any longer necessary. |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 300 | */ |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 301 | unsigned long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 302 | PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | volatile pthread_t threadid; |
| 305 | if (!initialized) |
| 306 | PyThread_init_thread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | threadid = pthread_self(); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 308 | return (unsigned long) threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Jake Tesler | 4959c33 | 2019-05-12 10:08:24 -0700 | [diff] [blame] | 311 | unsigned long |
| 312 | PyThread_get_thread_native_id(void) |
| 313 | { |
| 314 | if (!initialized) |
| 315 | PyThread_init_thread(); |
| 316 | #ifdef __APPLE__ |
| 317 | uint64_t native_id; |
| 318 | pthread_threadid_np(NULL, &native_id); |
| 319 | #elif defined(__linux__) |
| 320 | pid_t native_id; |
| 321 | native_id = syscall(__NR_gettid); |
| 322 | #elif defined(__FreeBSD__) |
| 323 | pid_t native_id; |
| 324 | native_id = pthread_getthreadid_np(); |
| 325 | #else |
| 326 | unsigned long native_id; |
| 327 | native_id = 0; |
| 328 | #endif |
| 329 | return (unsigned long) native_id; |
| 330 | } |
| 331 | |
Victor Stinner | c664b34 | 2019-05-04 11:48:05 -0400 | [diff] [blame] | 332 | void _Py_NO_RETURN |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 333 | PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | dprintf(("PyThread_exit_thread called\n")); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 336 | if (!initialized) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | exit(0); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 338 | pthread_exit(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 341 | #ifdef USE_SEMAPHORES |
| 342 | |
| 343 | /* |
| 344 | * Lock support. |
| 345 | */ |
| 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | PyThread_type_lock |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 348 | PyThread_allocate_lock(void) |
| 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | sem_t *lock; |
| 351 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | dprintf(("PyThread_allocate_lock called\n")); |
| 354 | if (!initialized) |
| 355 | PyThread_init_thread(); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 356 | |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 357 | lock = (sem_t *)PyMem_RawMalloc(sizeof(sem_t)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | if (lock) { |
| 360 | status = sem_init(lock,0,1); |
| 361 | CHECK_STATUS("sem_init"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | if (error) { |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 364 | PyMem_RawFree((void *)lock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | lock = NULL; |
| 366 | } |
| 367 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 368 | |
Zackery Spytz | 1a2252e | 2019-05-06 10:56:51 -0600 | [diff] [blame] | 369 | dprintf(("PyThread_allocate_lock() -> %p\n", (void *)lock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | return (PyThread_type_lock)lock; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | void |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 374 | PyThread_free_lock(PyThread_type_lock lock) |
| 375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | sem_t *thelock = (sem_t *)lock; |
| 377 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 378 | |
Christian Heimes | 56379c0 | 2012-12-02 08:37:00 +0100 | [diff] [blame] | 379 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | if (!thelock) |
| 383 | return; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | status = sem_destroy(thelock); |
| 386 | CHECK_STATUS("sem_destroy"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 387 | |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 388 | PyMem_RawFree((void *)thelock); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
| 392 | * As of February 2002, Cygwin thread implementations mistakenly report error |
| 393 | * codes in the return value of the sem_ calls (like the pthread_ functions). |
| 394 | * Correct implementations return -1 and put the code in errno. This supports |
| 395 | * either. |
| 396 | */ |
| 397 | static int |
| 398 | fix_status(int status) |
| 399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | return (status == -1) ? errno : status; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 403 | PyLockStatus |
| 404 | PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds, |
| 405 | int intr_flag) |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 406 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 407 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | sem_t *thelock = (sem_t *)lock; |
| 409 | int status, error = 0; |
| 410 | struct timespec ts; |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 411 | _PyTime_t deadline = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 412 | |
Christian Heimes | 56379c0 | 2012-12-02 08:37:00 +0100 | [diff] [blame] | 413 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 414 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n", |
| 415 | lock, microseconds, intr_flag)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 416 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 417 | if (microseconds > PY_TIMEOUT_MAX) { |
| 418 | Py_FatalError("Timeout larger than PY_TIMEOUT_MAX"); |
| 419 | } |
| 420 | |
| 421 | if (microseconds > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | MICROSECONDS_TO_TIMESPEC(microseconds, ts); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 423 | |
| 424 | if (!intr_flag) { |
| 425 | /* cannot overflow thanks to (microseconds > PY_TIMEOUT_MAX) |
| 426 | check done above */ |
| 427 | _PyTime_t timeout = _PyTime_FromNanoseconds(microseconds * 1000); |
| 428 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | while (1) { |
| 433 | if (microseconds > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | status = fix_status(sem_timedwait(thelock, &ts)); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 435 | } |
| 436 | else if (microseconds == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | status = fix_status(sem_trywait(thelock)); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 438 | } |
| 439 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | status = fix_status(sem_wait(thelock)); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 443 | /* Retry if interrupted by a signal, unless the caller wants to be |
| 444 | notified. */ |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 445 | if (intr_flag || status != EINTR) { |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | if (microseconds > 0) { |
| 450 | /* wait interrupted by a signal (EINTR): recompute the timeout */ |
| 451 | _PyTime_t dt = deadline - _PyTime_GetMonotonicClock(); |
| 452 | if (dt < 0) { |
| 453 | status = ETIMEDOUT; |
| 454 | break; |
| 455 | } |
| 456 | else if (dt > 0) { |
| 457 | _PyTime_t realtime_deadline = _PyTime_GetSystemClock() + dt; |
| 458 | if (_PyTime_AsTimespec(realtime_deadline, &ts) < 0) { |
| 459 | /* Cannot occur thanks to (microseconds > PY_TIMEOUT_MAX) |
| 460 | check done above */ |
| 461 | Py_UNREACHABLE(); |
| 462 | } |
| 463 | /* no need to update microseconds value, the code only care |
| 464 | if (microseconds > 0 or (microseconds == 0). */ |
| 465 | } |
| 466 | else { |
| 467 | microseconds = 0; |
| 468 | } |
| 469 | } |
| 470 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 472 | /* Don't check the status if we're stopping because of an interrupt. */ |
| 473 | if (!(intr_flag && status == EINTR)) { |
| 474 | if (microseconds > 0) { |
| 475 | if (status != ETIMEDOUT) |
| 476 | CHECK_STATUS("sem_timedwait"); |
| 477 | } |
| 478 | else if (microseconds == 0) { |
| 479 | if (status != EAGAIN) |
| 480 | CHECK_STATUS("sem_trywait"); |
| 481 | } |
| 482 | else { |
| 483 | CHECK_STATUS("sem_wait"); |
| 484 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 487 | if (status == 0) { |
| 488 | success = PY_LOCK_ACQUIRED; |
| 489 | } else if (intr_flag && status == EINTR) { |
| 490 | success = PY_LOCK_INTR; |
| 491 | } else { |
| 492 | success = PY_LOCK_FAILURE; |
| 493 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 495 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n", |
| 496 | lock, microseconds, intr_flag, success)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | return success; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | void |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 501 | PyThread_release_lock(PyThread_type_lock lock) |
| 502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | sem_t *thelock = (sem_t *)lock; |
| 504 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 505 | |
Christian Heimes | 56379c0 | 2012-12-02 08:37:00 +0100 | [diff] [blame] | 506 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | status = sem_post(thelock); |
| 510 | CHECK_STATUS("sem_post"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | #else /* USE_SEMAPHORES */ |
| 514 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 515 | /* |
| 516 | * Lock support. |
| 517 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | PyThread_type_lock |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 519 | PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | pthread_lock *lock; |
| 522 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | dprintf(("PyThread_allocate_lock called\n")); |
| 525 | if (!initialized) |
| 526 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 527 | |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 528 | lock = (pthread_lock *) PyMem_RawMalloc(sizeof(pthread_lock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | if (lock) { |
| 530 | memset((void *)lock, '\0', sizeof(pthread_lock)); |
| 531 | lock->locked = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 532 | |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 533 | status = pthread_mutex_init(&lock->mut, NULL); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 534 | CHECK_STATUS_PTHREAD("pthread_mutex_init"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | /* Mark the pthread mutex underlying a Python mutex as |
| 536 | pure happens-before. We can't simply mark the |
| 537 | Python-level mutex as a mutex because it can be |
| 538 | acquired and released in different threads, which |
| 539 | will cause errors. */ |
| 540 | _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 541 | |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 542 | status = _PyThread_cond_init(&lock->lock_released); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 543 | CHECK_STATUS_PTHREAD("pthread_cond_init"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | if (error) { |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 546 | PyMem_RawFree((void *)lock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | lock = 0; |
| 548 | } |
| 549 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 550 | |
Zackery Spytz | 1a2252e | 2019-05-06 10:56:51 -0600 | [diff] [blame] | 551 | dprintf(("PyThread_allocate_lock() -> %p\n", (void *)lock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 556 | PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | pthread_lock *thelock = (pthread_lock *)lock; |
| 559 | int status, error = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | 9a00e0a | 2013-06-18 22:17:48 +0200 | [diff] [blame] | 561 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 563 | |
Kristján Valur Jónsson | 187aa54 | 2012-06-05 22:17:42 +0000 | [diff] [blame] | 564 | /* some pthread-like implementations tie the mutex to the cond |
| 565 | * and must have the cond destroyed first. |
| 566 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | status = pthread_cond_destroy( &thelock->lock_released ); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 568 | CHECK_STATUS_PTHREAD("pthread_cond_destroy"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 569 | |
Kristján Valur Jónsson | 187aa54 | 2012-06-05 22:17:42 +0000 | [diff] [blame] | 570 | status = pthread_mutex_destroy( &thelock->mut ); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 571 | CHECK_STATUS_PTHREAD("pthread_mutex_destroy"); |
Kristján Valur Jónsson | 187aa54 | 2012-06-05 22:17:42 +0000 | [diff] [blame] | 572 | |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 573 | PyMem_RawFree((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 576 | PyLockStatus |
| 577 | PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds, |
| 578 | int intr_flag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 579 | { |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 580 | PyLockStatus success = PY_LOCK_FAILURE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | pthread_lock *thelock = (pthread_lock *)lock; |
| 582 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 584 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n", |
| 585 | lock, microseconds, intr_flag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 587 | if (microseconds == 0) { |
| 588 | status = pthread_mutex_trylock( &thelock->mut ); |
| 589 | if (status != EBUSY) |
| 590 | CHECK_STATUS_PTHREAD("pthread_mutex_trylock[1]"); |
| 591 | } |
| 592 | else { |
| 593 | status = pthread_mutex_lock( &thelock->mut ); |
| 594 | CHECK_STATUS_PTHREAD("pthread_mutex_lock[1]"); |
| 595 | } |
| 596 | if (status == 0) { |
| 597 | if (thelock->locked == 0) { |
| 598 | success = PY_LOCK_ACQUIRED; |
| 599 | } |
| 600 | else if (microseconds != 0) { |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 601 | struct timespec abs; |
| 602 | if (microseconds > 0) { |
| 603 | _PyThread_cond_after(microseconds, &abs); |
| 604 | } |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 605 | /* continue trying until we get the lock */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 607 | /* mut must be locked by me -- part of the condition |
| 608 | * protocol */ |
| 609 | while (success == PY_LOCK_FAILURE) { |
| 610 | if (microseconds > 0) { |
| 611 | status = pthread_cond_timedwait( |
| 612 | &thelock->lock_released, |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 613 | &thelock->mut, &abs); |
| 614 | if (status == 1) { |
| 615 | break; |
| 616 | } |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 617 | if (status == ETIMEDOUT) |
| 618 | break; |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 619 | CHECK_STATUS_PTHREAD("pthread_cond_timedwait"); |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 620 | } |
| 621 | else { |
| 622 | status = pthread_cond_wait( |
| 623 | &thelock->lock_released, |
| 624 | &thelock->mut); |
| 625 | CHECK_STATUS_PTHREAD("pthread_cond_wait"); |
| 626 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 628 | if (intr_flag && status == 0 && thelock->locked) { |
| 629 | /* We were woken up, but didn't get the lock. We probably received |
| 630 | * a signal. Return PY_LOCK_INTR to allow the caller to handle |
| 631 | * it and retry. */ |
| 632 | success = PY_LOCK_INTR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | break; |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 634 | } |
| 635 | else if (status == 0 && !thelock->locked) { |
| 636 | success = PY_LOCK_ACQUIRED; |
| 637 | } |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 638 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | } |
Antoine Pitrou | f84ac42 | 2017-06-26 20:41:07 +0200 | [diff] [blame] | 640 | if (success == PY_LOCK_ACQUIRED) thelock->locked = 1; |
| 641 | status = pthread_mutex_unlock( &thelock->mut ); |
| 642 | CHECK_STATUS_PTHREAD("pthread_mutex_unlock[1]"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | } |
Martin v. Löwis | 1509a15 | 2003-04-18 11:11:09 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 645 | if (error) success = PY_LOCK_FAILURE; |
| 646 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n", |
| 647 | lock, microseconds, intr_flag, success)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 652 | PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | pthread_lock *thelock = (pthread_lock *)lock; |
| 655 | int status, error = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | 9a00e0a | 2013-06-18 22:17:48 +0200 | [diff] [blame] | 657 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | status = pthread_mutex_lock( &thelock->mut ); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 661 | CHECK_STATUS_PTHREAD("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | thelock->locked = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 666 | status = pthread_cond_signal( &thelock->lock_released ); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 667 | CHECK_STATUS_PTHREAD("pthread_cond_signal"); |
Kristján Valur Jónsson | 187aa54 | 2012-06-05 22:17:42 +0000 | [diff] [blame] | 668 | |
| 669 | status = pthread_mutex_unlock( &thelock->mut ); |
Daniel Birnstiel | d7fa6b2 | 2017-03-21 14:06:06 +0100 | [diff] [blame] | 670 | CHECK_STATUS_PTHREAD("pthread_mutex_unlock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 671 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 672 | |
| 673 | #endif /* USE_SEMAPHORES */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 675 | int |
| 676 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
| 677 | { |
| 678 | return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0, /*intr_flag=*/0); |
| 679 | } |
| 680 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 681 | /* set the thread stack size. |
| 682 | * Return 0 if size is valid, -1 if size is invalid, |
| 683 | * -2 if setting stack size is not supported. |
| 684 | */ |
| 685 | static int |
| 686 | _pythread_pthread_set_stacksize(size_t size) |
| 687 | { |
| 688 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | pthread_attr_t attrs; |
| 690 | size_t tss_min; |
| 691 | int rc = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 692 | #endif |
| 693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | /* set to default */ |
| 695 | if (size == 0) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 696 | _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | return 0; |
| 698 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 699 | |
| 700 | #if defined(THREAD_STACK_SIZE) |
| 701 | #if defined(PTHREAD_STACK_MIN) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN |
| 703 | : THREAD_STACK_MIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 704 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | tss_min = THREAD_STACK_MIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 706 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (size >= tss_min) { |
| 708 | /* validate stack size by setting thread attribute */ |
| 709 | if (pthread_attr_init(&attrs) == 0) { |
| 710 | rc = pthread_attr_setstacksize(&attrs, size); |
| 711 | pthread_attr_destroy(&attrs); |
| 712 | if (rc == 0) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 713 | _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 719 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 721 | #endif |
| 722 | } |
| 723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x) |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 725 | |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 726 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 727 | /* Thread Local Storage (TLS) API |
| 728 | |
| 729 | This API is DEPRECATED since Python 3.7. See PEP 539 for details. |
| 730 | */ |
| 731 | |
| 732 | /* Issue #25658: On platforms where native TLS key is defined in a way that |
| 733 | cannot be safely cast to int, PyThread_create_key returns immediately a |
| 734 | failure status and other TLS functions all are no-ops. This indicates |
| 735 | clearly that the old API is not supported on platforms where it cannot be |
| 736 | used reliably, and that no effort will be made to add such support. |
| 737 | |
| 738 | Note: PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT will be unnecessary after |
| 739 | removing this API. |
| 740 | */ |
| 741 | |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 742 | int |
| 743 | PyThread_create_key(void) |
| 744 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 745 | #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 746 | pthread_key_t key; |
| 747 | int fail = pthread_key_create(&key, NULL); |
Victor Stinner | daca3d7 | 2014-08-17 22:11:06 +0200 | [diff] [blame] | 748 | if (fail) |
| 749 | return -1; |
| 750 | if (key > INT_MAX) { |
| 751 | /* Issue #22206: handle integer overflow */ |
| 752 | pthread_key_delete(key); |
| 753 | errno = ENOMEM; |
| 754 | return -1; |
| 755 | } |
| 756 | return (int)key; |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 757 | #else |
| 758 | return -1; /* never return valid key value. */ |
| 759 | #endif |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | void |
| 763 | PyThread_delete_key(int key) |
| 764 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 765 | #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 766 | pthread_key_delete(key); |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 767 | #endif |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void |
| 771 | PyThread_delete_key_value(int key) |
| 772 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 773 | #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 774 | pthread_setspecific(key, NULL); |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 775 | #endif |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | int |
| 779 | PyThread_set_key_value(int key, void *value) |
| 780 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 781 | #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT |
| 782 | int fail = pthread_setspecific(key, value); |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 783 | return fail ? -1 : 0; |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 784 | #else |
| 785 | return -1; |
| 786 | #endif |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void * |
| 790 | PyThread_get_key_value(int key) |
| 791 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 792 | #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 793 | return pthread_getspecific(key); |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 794 | #else |
| 795 | return NULL; |
| 796 | #endif |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 799 | |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 800 | void |
| 801 | PyThread_ReInitTLS(void) |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 802 | { |
| 803 | } |
| 804 | |
| 805 | |
| 806 | /* Thread Specific Storage (TSS) API |
| 807 | |
| 808 | Platform-specific components of TSS API implementation. |
| 809 | */ |
| 810 | |
| 811 | int |
| 812 | PyThread_tss_create(Py_tss_t *key) |
| 813 | { |
| 814 | assert(key != NULL); |
| 815 | /* If the key has been created, function is silently skipped. */ |
| 816 | if (key->_is_initialized) { |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | int fail = pthread_key_create(&(key->_key), NULL); |
| 821 | if (fail) { |
| 822 | return -1; |
| 823 | } |
| 824 | key->_is_initialized = 1; |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | void |
| 829 | PyThread_tss_delete(Py_tss_t *key) |
| 830 | { |
| 831 | assert(key != NULL); |
| 832 | /* If the key has not been created, function is silently skipped. */ |
| 833 | if (!key->_is_initialized) { |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | pthread_key_delete(key->_key); |
| 838 | /* pthread has not provided the defined invalid value for the key. */ |
| 839 | key->_is_initialized = 0; |
| 840 | } |
| 841 | |
| 842 | int |
| 843 | PyThread_tss_set(Py_tss_t *key, void *value) |
| 844 | { |
| 845 | assert(key != NULL); |
| 846 | int fail = pthread_setspecific(key->_key, value); |
| 847 | return fail ? -1 : 0; |
| 848 | } |
| 849 | |
| 850 | void * |
| 851 | PyThread_tss_get(Py_tss_t *key) |
| 852 | { |
| 853 | assert(key != NULL); |
| 854 | return pthread_getspecific(key->_key); |
| 855 | } |