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