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