blob: 9b4b23bdc7d7bf2a6e6c2d90d2edb09028d3557d [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001
Guido van Rossum66020991996-06-11 18:32:18 +00002/* Posix threads interface */
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00003
Guido van Rossum66020991996-06-11 18:32:18 +00004#include <stdlib.h>
Guido van Rossum9e46e561998-10-07 16:39:47 +00005#include <string.h>
Martin v. Löwisa7a76d32002-10-04 07:21:24 +00006#if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
Jack Jansen76689572002-01-15 20:36:14 +00007#define destructor xxdestructor
8#endif
Guido van Rossum66020991996-06-11 18:32:18 +00009#include <pthread.h>
Martin v. Löwisa7a76d32002-10-04 07:21:24 +000010#if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
Jack Jansen76689572002-01-15 20:36:14 +000011#undef destructor
12#endif
Guido van Rossum80230992001-10-12 21:49:17 +000013#include <signal.h>
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000014
Jake Teslerb121f632019-05-22 08:43:17 -070015#if defined(__linux__)
16# include <sys/syscall.h> /* syscall(SYS_gettid) */
17#elif defined(__FreeBSD__)
18# include <pthread_np.h> /* pthread_getthreadid_np() */
David Carlier0b9956e2019-06-03 16:43:33 +010019#elif defined(__OpenBSD__)
20# include <unistd.h> /* getthrid() */
David Carlier52870222019-06-12 15:37:56 +000021#elif defined(__NetBSD__) /* _lwp_self */
22# include <lwp.h>
Jake Teslerb121f632019-05-22 08:43:17 -070023#endif
24
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025/* The POSIX spec requires that use of pthread_attr_setstacksize
26 be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
27#ifdef _POSIX_THREAD_ATTR_STACKSIZE
28#ifndef THREAD_STACK_SIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define THREAD_STACK_SIZE 0 /* use default stack size */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030#endif
Ned Deily9a7c5242011-05-28 00:19:56 -070031
Ned Deily7ca97d52012-03-13 11:18:18 -070032/* The default stack size for new threads on OSX and BSD is small enough that
33 * we'll get hard crashes instead of 'maximum recursion depth exceeded'
34 * exceptions.
35 *
36 * The default stack sizes below are the empirically determined minimal stack
37 * sizes where a simple recursive function doesn't cause a hard crash.
38 */
39#if defined(__APPLE__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
40#undef THREAD_STACK_SIZE
41#define THREAD_STACK_SIZE 0x500000
42#endif
43#if defined(__FreeBSD__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
Ned Deily9a7c5242011-05-28 00:19:56 -070044#undef THREAD_STACK_SIZE
45#define THREAD_STACK_SIZE 0x400000
46#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047/* for safety, ensure a viable minimum stacksize */
Victor Stinner8c663fd2017-11-08 14:44:44 -080048#define THREAD_STACK_MIN 0x8000 /* 32 KiB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
50#ifdef THREAD_STACK_SIZE
51#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
52#endif
53#endif
54
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000055/* The POSIX spec says that implementations supporting the sem_*
56 family of functions must indicate this by defining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000058#ifdef _POSIX_SEMAPHORES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000060 we need to add 0 to make it work there as well. */
61#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000062#define HAVE_BROKEN_POSIX_SEMAPHORES
63#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000064#include <semaphore.h>
65#include <errno.h>
66#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000067#endif
Guido van Rossum66020991996-06-11 18:32:18 +000068
Guido van Rossumd6353e21997-05-13 17:51:13 +000069
Martin v. Löwiscc898662002-03-17 09:53:51 +000070/* Whether or not to use semaphores directly rather than emulating them with
71 * mutexes and condition variables:
72 */
Antoine Pitrou19f8edc2010-10-10 08:37:22 +000073#if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \
74 defined(HAVE_SEM_TIMEDWAIT))
Martin v. Löwiscc898662002-03-17 09:53:51 +000075# define USE_SEMAPHORES
76#else
77# undef USE_SEMAPHORES
78#endif
79
80
Guido van Rossum80230992001-10-12 21:49:17 +000081/* On platforms that don't use standard POSIX threads pthread_sigmask()
82 * isn't present. DEC threads uses sigprocmask() instead as do most
83 * other UNIX International compliant systems that don't have the full
84 * pthread implementation.
85 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000086#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000087# define SET_THREAD_SIGMASK pthread_sigmask
88#else
89# define SET_THREAD_SIGMASK sigprocmask
90#endif
91
92
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000093/* We assume all modern POSIX systems have gettimeofday() */
94#ifdef GETTIMEOFDAY_NO_TZ
95#define GETTIMEOFDAY(ptv) gettimeofday(ptv)
96#else
97#define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
98#endif
99
100#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \
101do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 struct timeval tv; \
103 GETTIMEOFDAY(&tv); \
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 Pitrou7c3e5772010-04-14 15:44:10 +0000110} while(0)
111
112
Inada Naoki001fee12019-02-20 10:00:09 +0900113/*
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.
123static pthread_condattr_t *condattr_monotonic = NULL;
124
125static void
126init_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
137int
138_PyThread_cond_init(PyCOND_T *cond)
139{
140 return pthread_cond_init(cond, condattr_monotonic);
141}
142
143void
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 Rossumb98b1b31994-05-11 08:42:04 +0000163/* 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 Rossum2c8cb9f1994-05-09 15:12:46 +0000170 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000171 *
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 Rossum2c8cb9f1994-05-09 15:12:46 +0000176 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000177
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000178typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 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 Rossum2c8cb9f1994-05-09 15:12:46 +0000183} pthread_lock;
184
Guido van Rossum9e46e561998-10-07 16:39:47 +0000185#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100186#define CHECK_STATUS_PTHREAD(name) if (status != 0) { fprintf(stderr, \
187 "%s: %s\n", name, strerror(status)); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000188
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000189/*
190 * Initialization.
191 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192static void
193PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000194{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000195#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrou9a00e0a2013-06-18 22:17:48 +0200196 extern void pthread_init(void);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000198#endif
Inada Naoki001fee12019-02-20 10:00:09 +0900199 init_condattr();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000200}
201
202/*
203 * Thread support.
204 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000205
Siddhesh Poyarekar9eea6ea2018-11-30 20:44:25 +0530206/* 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. */
211typedef struct {
212 void (*func) (void *);
213 void *arg;
214} pythread_callback;
215
216static void *
217pythread_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 Rossumb98b1b31994-05-11 08:42:04 +0000228
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200229unsigned long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 pthread_t th;
233 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000234#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000236#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000237#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 size_t tss;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000239#endif
240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 dprintf(("PyThread_start_new_thread called\n"));
242 if (!initialized)
243 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000244
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000245#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (pthread_attr_init(&attrs) != 0)
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200247 return PYTHREAD_INVALID_THREAD_ID;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000248#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249#if defined(THREAD_STACK_SIZE)
Victor Stinner50b48572018-11-01 01:51:40 +0100250 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600251 size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0;
252 tss = (stacksize != 0) ? stacksize : THREAD_STACK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 if (tss != 0) {
254 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
255 pthread_attr_destroy(&attrs);
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200256 return PYTHREAD_INVALID_THREAD_ID;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
258 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000259#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000262#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000263
Siddhesh Poyarekar9eea6ea2018-11-30 20:44:25 +0530264 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 Pitrouf95a1b32010-05-09 15:52:27 +0000273 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000274#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000276#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000278#endif
Siddhesh Poyarekar9eea6ea2018-11-30 20:44:25 +0530279 pythread_wrapper, callback);
Guido van Rossum80230992001-10-12 21:49:17 +0000280
Fred Drake03459a52001-11-09 16:00:41 +0000281#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000283#endif
Siddhesh Poyarekar9eea6ea2018-11-30 20:44:25 +0530284
285 if (status != 0) {
286 PyMem_RawFree(callback);
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200287 return PYTHREAD_INVALID_THREAD_ID;
Siddhesh Poyarekar9eea6ea2018-11-30 20:44:25 +0530288 }
Martin v. Löwis910ae622003-04-19 07:44:52 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000291
Guido van Rossum3c288632001-10-16 21:13:49 +0000292#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200293 return (unsigned long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000294#else
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200295 return (unsigned long) *(unsigned long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000296#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000297}
298
Trent Mick635f6fb2000-08-23 21:33:05 +0000299/* XXX This implementation is considered (to quote Tim Peters) "inherently
300 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000301 - It does not guarantee the promise that a non-zero integer is returned.
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200302 - The cast to unsigned long is inherently unsafe.
Jesus Cea736e7fc2011-03-14 17:36:54 +0100303 - It is not clear that the 'volatile' (for AIX?) are any longer necessary.
Trent Mick635f6fb2000-08-23 21:33:05 +0000304*/
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200305unsigned long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 volatile pthread_t threadid;
309 if (!initialized)
310 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 threadid = pthread_self();
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200312 return (unsigned long) threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000313}
314
Jake Teslerb121f632019-05-22 08:43:17 -0700315#ifdef PY_HAVE_THREAD_NATIVE_ID
316unsigned long
317PyThread_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 Carlier0b9956e2019-06-03 16:43:33 +0100330#elif defined(__OpenBSD__)
331 pid_t native_id;
332 native_id = getthrid();
David Carlier52870222019-06-12 15:37:56 +0000333#elif defined(__NetBSD__)
334 lwpid_t native_id;
335 native_id = _lwp_self();
Jake Teslerb121f632019-05-22 08:43:17 -0700336#endif
337 return (unsigned long) native_id;
338}
339#endif
340
Victor Stinnerc664b342019-05-04 11:48:05 -0400341void _Py_NO_RETURN
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000342PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 dprintf(("PyThread_exit_thread called\n"));
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200345 if (!initialized)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 exit(0);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200347 pthread_exit(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000348}
349
Martin v. Löwiscc898662002-03-17 09:53:51 +0000350#ifdef USE_SEMAPHORES
351
352/*
353 * Lock support.
354 */
355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000357PyThread_allocate_lock(void)
358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 sem_t *lock;
360 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 dprintf(("PyThread_allocate_lock called\n"));
363 if (!initialized)
364 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000365
Victor Stinner80aa5652013-07-07 17:17:59 +0200366 lock = (sem_t *)PyMem_RawMalloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (lock) {
369 status = sem_init(lock,0,1);
370 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (error) {
Victor Stinner80aa5652013-07-07 17:17:59 +0200373 PyMem_RawFree((void *)lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 lock = NULL;
375 }
376 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000377
Zackery Spytz1a2252e2019-05-06 10:56:51 -0600378 dprintf(("PyThread_allocate_lock() -> %p\n", (void *)lock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000380}
381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000383PyThread_free_lock(PyThread_type_lock lock)
384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 sem_t *thelock = (sem_t *)lock;
386 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000387
Christian Heimes56379c02012-12-02 08:37:00 +0100388 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (!thelock)
392 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 status = sem_destroy(thelock);
395 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000396
Victor Stinner80aa5652013-07-07 17:17:59 +0200397 PyMem_RawFree((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000398}
399
400/*
401 * As of February 2002, Cygwin thread implementations mistakenly report error
402 * codes in the return value of the sem_ calls (like the pthread_ functions).
403 * Correct implementations return -1 and put the code in errno. This supports
404 * either.
405 */
406static int
407fix_status(int status)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000410}
411
Antoine Pitrou810023d2010-12-15 22:59:16 +0000412PyLockStatus
413PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
414 int intr_flag)
Martin v. Löwiscc898662002-03-17 09:53:51 +0000415{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000416 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 sem_t *thelock = (sem_t *)lock;
418 int status, error = 0;
419 struct timespec ts;
Victor Stinner850a18e2017-10-24 16:53:32 -0700420 _PyTime_t deadline = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000421
Christian Heimes56379c02012-12-02 08:37:00 +0100422 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000423 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
424 lock, microseconds, intr_flag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000425
Victor Stinner850a18e2017-10-24 16:53:32 -0700426 if (microseconds > PY_TIMEOUT_MAX) {
427 Py_FatalError("Timeout larger than PY_TIMEOUT_MAX");
428 }
429
430 if (microseconds > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
Victor Stinner850a18e2017-10-24 16:53:32 -0700432
433 if (!intr_flag) {
434 /* cannot overflow thanks to (microseconds > PY_TIMEOUT_MAX)
435 check done above */
436 _PyTime_t timeout = _PyTime_FromNanoseconds(microseconds * 1000);
437 deadline = _PyTime_GetMonotonicClock() + timeout;
438 }
439 }
440
441 while (1) {
442 if (microseconds > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 status = fix_status(sem_timedwait(thelock, &ts));
Victor Stinner850a18e2017-10-24 16:53:32 -0700444 }
445 else if (microseconds == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 status = fix_status(sem_trywait(thelock));
Victor Stinner850a18e2017-10-24 16:53:32 -0700447 }
448 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 status = fix_status(sem_wait(thelock));
Victor Stinner850a18e2017-10-24 16:53:32 -0700450 }
451
Antoine Pitrou810023d2010-12-15 22:59:16 +0000452 /* Retry if interrupted by a signal, unless the caller wants to be
453 notified. */
Victor Stinner850a18e2017-10-24 16:53:32 -0700454 if (intr_flag || status != EINTR) {
455 break;
456 }
457
458 if (microseconds > 0) {
459 /* wait interrupted by a signal (EINTR): recompute the timeout */
460 _PyTime_t dt = deadline - _PyTime_GetMonotonicClock();
461 if (dt < 0) {
462 status = ETIMEDOUT;
463 break;
464 }
465 else if (dt > 0) {
466 _PyTime_t realtime_deadline = _PyTime_GetSystemClock() + dt;
467 if (_PyTime_AsTimespec(realtime_deadline, &ts) < 0) {
468 /* Cannot occur thanks to (microseconds > PY_TIMEOUT_MAX)
469 check done above */
470 Py_UNREACHABLE();
471 }
472 /* no need to update microseconds value, the code only care
473 if (microseconds > 0 or (microseconds == 0). */
474 }
475 else {
476 microseconds = 0;
477 }
478 }
479 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000480
Antoine Pitrou810023d2010-12-15 22:59:16 +0000481 /* Don't check the status if we're stopping because of an interrupt. */
482 if (!(intr_flag && status == EINTR)) {
483 if (microseconds > 0) {
484 if (status != ETIMEDOUT)
485 CHECK_STATUS("sem_timedwait");
486 }
487 else if (microseconds == 0) {
488 if (status != EAGAIN)
489 CHECK_STATUS("sem_trywait");
490 }
491 else {
492 CHECK_STATUS("sem_wait");
493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000495
Antoine Pitrou810023d2010-12-15 22:59:16 +0000496 if (status == 0) {
497 success = PY_LOCK_ACQUIRED;
498 } else if (intr_flag && status == EINTR) {
499 success = PY_LOCK_INTR;
500 } else {
501 success = PY_LOCK_FAILURE;
502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503
Antoine Pitrou810023d2010-12-15 22:59:16 +0000504 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
505 lock, microseconds, intr_flag, success));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000507}
508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000510PyThread_release_lock(PyThread_type_lock lock)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 sem_t *thelock = (sem_t *)lock;
513 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000514
Christian Heimes56379c02012-12-02 08:37:00 +0100515 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 status = sem_post(thelock);
519 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000520}
521
522#else /* USE_SEMAPHORES */
523
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000524/*
525 * Lock support.
526 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000528PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 pthread_lock *lock;
531 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 dprintf(("PyThread_allocate_lock called\n"));
534 if (!initialized)
535 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000536
Victor Stinner80aa5652013-07-07 17:17:59 +0200537 lock = (pthread_lock *) PyMem_RawMalloc(sizeof(pthread_lock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (lock) {
539 memset((void *)lock, '\0', sizeof(pthread_lock));
540 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000541
Inada Naoki001fee12019-02-20 10:00:09 +0900542 status = pthread_mutex_init(&lock->mut, NULL);
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100543 CHECK_STATUS_PTHREAD("pthread_mutex_init");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 /* Mark the pthread mutex underlying a Python mutex as
545 pure happens-before. We can't simply mark the
546 Python-level mutex as a mutex because it can be
547 acquired and released in different threads, which
548 will cause errors. */
549 _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000550
Inada Naoki001fee12019-02-20 10:00:09 +0900551 status = _PyThread_cond_init(&lock->lock_released);
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100552 CHECK_STATUS_PTHREAD("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (error) {
Victor Stinner80aa5652013-07-07 17:17:59 +0200555 PyMem_RawFree((void *)lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 lock = 0;
557 }
558 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000559
Zackery Spytz1a2252e2019-05-06 10:56:51 -0600560 dprintf(("PyThread_allocate_lock() -> %p\n", (void *)lock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000562}
563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 pthread_lock *thelock = (pthread_lock *)lock;
568 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000569
Antoine Pitrou9a00e0a2013-06-18 22:17:48 +0200570 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000572
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000573 /* some pthread-like implementations tie the mutex to the cond
574 * and must have the cond destroyed first.
575 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 status = pthread_cond_destroy( &thelock->lock_released );
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100577 CHECK_STATUS_PTHREAD("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000578
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000579 status = pthread_mutex_destroy( &thelock->mut );
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100580 CHECK_STATUS_PTHREAD("pthread_mutex_destroy");
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000581
Victor Stinner80aa5652013-07-07 17:17:59 +0200582 PyMem_RawFree((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000583}
584
Antoine Pitrou810023d2010-12-15 22:59:16 +0000585PyLockStatus
586PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
587 int intr_flag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000588{
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200589 PyLockStatus success = PY_LOCK_FAILURE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 pthread_lock *thelock = (pthread_lock *)lock;
591 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000592
Antoine Pitrou810023d2010-12-15 22:59:16 +0000593 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
594 lock, microseconds, intr_flag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000595
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200596 if (microseconds == 0) {
597 status = pthread_mutex_trylock( &thelock->mut );
598 if (status != EBUSY)
599 CHECK_STATUS_PTHREAD("pthread_mutex_trylock[1]");
600 }
601 else {
602 status = pthread_mutex_lock( &thelock->mut );
603 CHECK_STATUS_PTHREAD("pthread_mutex_lock[1]");
604 }
605 if (status == 0) {
606 if (thelock->locked == 0) {
607 success = PY_LOCK_ACQUIRED;
608 }
609 else if (microseconds != 0) {
Inada Naoki001fee12019-02-20 10:00:09 +0900610 struct timespec abs;
611 if (microseconds > 0) {
612 _PyThread_cond_after(microseconds, &abs);
613 }
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200614 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000615
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200616 /* mut must be locked by me -- part of the condition
617 * protocol */
618 while (success == PY_LOCK_FAILURE) {
619 if (microseconds > 0) {
620 status = pthread_cond_timedwait(
621 &thelock->lock_released,
Inada Naoki001fee12019-02-20 10:00:09 +0900622 &thelock->mut, &abs);
623 if (status == 1) {
624 break;
625 }
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200626 if (status == ETIMEDOUT)
627 break;
Inada Naoki001fee12019-02-20 10:00:09 +0900628 CHECK_STATUS_PTHREAD("pthread_cond_timedwait");
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200629 }
630 else {
631 status = pthread_cond_wait(
632 &thelock->lock_released,
633 &thelock->mut);
634 CHECK_STATUS_PTHREAD("pthread_cond_wait");
635 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000636
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200637 if (intr_flag && status == 0 && thelock->locked) {
638 /* We were woken up, but didn't get the lock. We probably received
639 * a signal. Return PY_LOCK_INTR to allow the caller to handle
640 * it and retry. */
641 success = PY_LOCK_INTR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 break;
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200643 }
644 else if (status == 0 && !thelock->locked) {
645 success = PY_LOCK_ACQUIRED;
646 }
Antoine Pitrou810023d2010-12-15 22:59:16 +0000647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 }
Antoine Pitrouf84ac422017-06-26 20:41:07 +0200649 if (success == PY_LOCK_ACQUIRED) thelock->locked = 1;
650 status = pthread_mutex_unlock( &thelock->mut );
651 CHECK_STATUS_PTHREAD("pthread_mutex_unlock[1]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000653
Antoine Pitrou810023d2010-12-15 22:59:16 +0000654 if (error) success = PY_LOCK_FAILURE;
655 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
656 lock, microseconds, intr_flag, success));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000658}
659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000661PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 pthread_lock *thelock = (pthread_lock *)lock;
664 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000665
Antoine Pitrou9a00e0a2013-06-18 22:17:48 +0200666 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 status = pthread_mutex_lock( &thelock->mut );
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100670 CHECK_STATUS_PTHREAD("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 /* wake up someone (anyone, if any) waiting on the lock */
675 status = pthread_cond_signal( &thelock->lock_released );
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100676 CHECK_STATUS_PTHREAD("pthread_cond_signal");
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000677
678 status = pthread_mutex_unlock( &thelock->mut );
Daniel Birnstield7fa6b22017-03-21 14:06:06 +0100679 CHECK_STATUS_PTHREAD("pthread_mutex_unlock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000680}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000681
682#endif /* USE_SEMAPHORES */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000683
Antoine Pitrou810023d2010-12-15 22:59:16 +0000684int
685PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
686{
687 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0, /*intr_flag=*/0);
688}
689
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000690/* set the thread stack size.
691 * Return 0 if size is valid, -1 if size is invalid,
692 * -2 if setting stack size is not supported.
693 */
694static int
695_pythread_pthread_set_stacksize(size_t size)
696{
697#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 pthread_attr_t attrs;
699 size_t tss_min;
700 int rc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000701#endif
702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 /* set to default */
704 if (size == 0) {
Victor Stinner50b48572018-11-01 01:51:40 +0100705 _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return 0;
707 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000708
709#if defined(THREAD_STACK_SIZE)
710#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
712 : THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000713#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 tss_min = THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000715#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (size >= tss_min) {
717 /* validate stack size by setting thread attribute */
718 if (pthread_attr_init(&attrs) == 0) {
719 rc = pthread_attr_setstacksize(&attrs, size);
720 pthread_attr_destroy(&attrs);
721 if (rc == 0) {
Victor Stinner50b48572018-11-01 01:51:40 +0100722 _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 return 0;
724 }
725 }
726 }
727 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000728#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000730#endif
731}
732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000734
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000735
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900736/* Thread Local Storage (TLS) API
737
738 This API is DEPRECATED since Python 3.7. See PEP 539 for details.
739*/
740
741/* Issue #25658: On platforms where native TLS key is defined in a way that
742 cannot be safely cast to int, PyThread_create_key returns immediately a
743 failure status and other TLS functions all are no-ops. This indicates
744 clearly that the old API is not supported on platforms where it cannot be
745 used reliably, and that no effort will be made to add such support.
746
747 Note: PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT will be unnecessary after
748 removing this API.
749*/
750
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000751int
752PyThread_create_key(void)
753{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900754#ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000755 pthread_key_t key;
756 int fail = pthread_key_create(&key, NULL);
Victor Stinnerdaca3d72014-08-17 22:11:06 +0200757 if (fail)
758 return -1;
759 if (key > INT_MAX) {
760 /* Issue #22206: handle integer overflow */
761 pthread_key_delete(key);
762 errno = ENOMEM;
763 return -1;
764 }
765 return (int)key;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900766#else
767 return -1; /* never return valid key value. */
768#endif
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000769}
770
771void
772PyThread_delete_key(int key)
773{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900774#ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000775 pthread_key_delete(key);
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900776#endif
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000777}
778
779void
780PyThread_delete_key_value(int key)
781{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900782#ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000783 pthread_setspecific(key, NULL);
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900784#endif
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000785}
786
787int
788PyThread_set_key_value(int key, void *value)
789{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900790#ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
791 int fail = pthread_setspecific(key, value);
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000792 return fail ? -1 : 0;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900793#else
794 return -1;
795#endif
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000796}
797
798void *
799PyThread_get_key_value(int key)
800{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900801#ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000802 return pthread_getspecific(key);
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900803#else
804 return NULL;
805#endif
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000806}
807
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900808
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000809void
810PyThread_ReInitTLS(void)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900811{
812}
813
814
815/* Thread Specific Storage (TSS) API
816
817 Platform-specific components of TSS API implementation.
818*/
819
820int
821PyThread_tss_create(Py_tss_t *key)
822{
823 assert(key != NULL);
824 /* If the key has been created, function is silently skipped. */
825 if (key->_is_initialized) {
826 return 0;
827 }
828
829 int fail = pthread_key_create(&(key->_key), NULL);
830 if (fail) {
831 return -1;
832 }
833 key->_is_initialized = 1;
834 return 0;
835}
836
837void
838PyThread_tss_delete(Py_tss_t *key)
839{
840 assert(key != NULL);
841 /* If the key has not been created, function is silently skipped. */
842 if (!key->_is_initialized) {
843 return;
844 }
845
846 pthread_key_delete(key->_key);
847 /* pthread has not provided the defined invalid value for the key. */
848 key->_is_initialized = 0;
849}
850
851int
852PyThread_tss_set(Py_tss_t *key, void *value)
853{
854 assert(key != NULL);
855 int fail = pthread_setspecific(key->_key, value);
856 return fail ? -1 : 0;
857}
858
859void *
860PyThread_tss_get(Py_tss_t *key)
861{
862 assert(key != NULL);
863 return pthread_getspecific(key->_key);
864}