blob: 27e0dc84bcb52ed2c8cad5c7372df4752d5e150f [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015/* The POSIX spec requires that use of pthread_attr_setstacksize
16 be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
17#ifdef _POSIX_THREAD_ATTR_STACKSIZE
18#ifndef THREAD_STACK_SIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#define THREAD_STACK_SIZE 0 /* use default stack size */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020#endif
Ned Deily9a7c5242011-05-28 00:19:56 -070021
Ned Deily7ca97d52012-03-13 11:18:18 -070022/* The default stack size for new threads on OSX and BSD is small enough that
23 * we'll get hard crashes instead of 'maximum recursion depth exceeded'
24 * exceptions.
25 *
26 * The default stack sizes below are the empirically determined minimal stack
27 * sizes where a simple recursive function doesn't cause a hard crash.
28 */
29#if defined(__APPLE__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
30#undef THREAD_STACK_SIZE
31#define THREAD_STACK_SIZE 0x500000
32#endif
33#if defined(__FreeBSD__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
Ned Deily9a7c5242011-05-28 00:19:56 -070034#undef THREAD_STACK_SIZE
35#define THREAD_STACK_SIZE 0x400000
36#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000037/* for safety, ensure a viable minimum stacksize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038#define THREAD_STACK_MIN 0x8000 /* 32kB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000039#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
40#ifdef THREAD_STACK_SIZE
41#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
42#endif
43#endif
44
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000045/* The POSIX spec says that implementations supporting the sem_*
46 family of functions must indicate this by defining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000048#ifdef _POSIX_SEMAPHORES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000050 we need to add 0 to make it work there as well. */
51#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000052#define HAVE_BROKEN_POSIX_SEMAPHORES
53#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000054#include <semaphore.h>
55#include <errno.h>
56#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000057#endif
Guido van Rossum66020991996-06-11 18:32:18 +000058
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059/* Before FreeBSD 5.4, system scope threads was very limited resource
60 in default setting. So the process scope is preferred to get
61 enough number of threads to work. */
62#ifdef __FreeBSD__
63#include <osreldate.h>
64#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
65#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
66#endif
67#endif
68
Martin v. Löwisb0233812002-12-11 13:12:30 +000069#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000070# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000071#endif
72#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000073# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000074#endif
75#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000076# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000077#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000078
Guido van Rossumd6353e21997-05-13 17:51:13 +000079
Martin v. Löwiscc898662002-03-17 09:53:51 +000080/* Whether or not to use semaphores directly rather than emulating them with
81 * mutexes and condition variables:
82 */
Antoine Pitrou19f8edc2010-10-10 08:37:22 +000083#if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \
84 defined(HAVE_SEM_TIMEDWAIT))
Martin v. Löwiscc898662002-03-17 09:53:51 +000085# define USE_SEMAPHORES
86#else
87# undef USE_SEMAPHORES
88#endif
89
90
Guido van Rossum80230992001-10-12 21:49:17 +000091/* On platforms that don't use standard POSIX threads pthread_sigmask()
92 * isn't present. DEC threads uses sigprocmask() instead as do most
93 * other UNIX International compliant systems that don't have the full
94 * pthread implementation.
95 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000096#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000097# define SET_THREAD_SIGMASK pthread_sigmask
98#else
99# define SET_THREAD_SIGMASK sigprocmask
100#endif
101
102
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000103/* We assume all modern POSIX systems have gettimeofday() */
104#ifdef GETTIMEOFDAY_NO_TZ
105#define GETTIMEOFDAY(ptv) gettimeofday(ptv)
106#else
107#define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
108#endif
109
110#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \
111do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 struct timeval tv; \
113 GETTIMEOFDAY(&tv); \
114 tv.tv_usec += microseconds % 1000000; \
115 tv.tv_sec += microseconds / 1000000; \
116 tv.tv_sec += tv.tv_usec / 1000000; \
117 tv.tv_usec %= 1000000; \
118 ts.tv_sec = tv.tv_sec; \
119 ts.tv_nsec = tv.tv_usec * 1000; \
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000120} while(0)
121
122
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000123/* A pthread mutex isn't sufficient to model the Python lock type
124 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
125 * following are undefined:
126 * -> a thread tries to lock a mutex it already has locked
127 * -> a thread tries to unlock a mutex locked by a different thread
128 * pthread mutexes are designed for serializing threads over short pieces
129 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000130 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000131 *
132 * The pthread_lock struct implements a Python lock as a "locked?" bit
133 * and a <condition, mutex> pair. In general, if the bit can be acquired
134 * instantly, it is, else the pair is used to block the thread until the
135 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000136 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000137
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 char locked; /* 0=unlocked, 1=locked */
140 /* a <cond, mutex> pair to handle an acquire of a locked lock */
141 pthread_cond_t lock_released;
142 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000143} pthread_lock;
144
Guido van Rossum9e46e561998-10-07 16:39:47 +0000145#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000146
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000147/*
148 * Initialization.
149 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000150
Victor Stinner87e78ce2011-07-04 22:53:49 +0200151#if defined(_HAVE_BSDI)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152static
153void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000154{
155}
156
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000157static void
158PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 /* DO AN INIT BY STARTING THE THREAD */
161 static int dummy = 0;
162 pthread_t thread1;
163 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
164 pthread_join(thread1, NULL);
Guido van Rossum9e46e561998-10-07 16:39:47 +0000165}
166
167#else /* !_HAVE_BSDI */
168
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000169static void
170PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000171{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000172#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrou9a00e0a2013-06-18 22:17:48 +0200173 extern void pthread_init(void);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000175#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000176}
177
Guido van Rossum9e46e561998-10-07 16:39:47 +0000178#endif /* !_HAVE_BSDI */
179
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000180/*
181 * Thread support.
182 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000183
184
Guido van Rossum3c288632001-10-16 21:13:49 +0000185long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000186PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 pthread_t th;
189 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000190#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000192#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000193#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 size_t tss;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000195#endif
196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 dprintf(("PyThread_start_new_thread called\n"));
198 if (!initialized)
199 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000200
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000201#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 if (pthread_attr_init(&attrs) != 0)
203 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000204#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000205#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
207 : THREAD_STACK_SIZE;
208 if (tss != 0) {
209 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
210 pthread_attr_destroy(&attrs);
211 return -1;
212 }
213 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000214#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000217#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000220#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000222#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000224#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 (void* (*)(void *))func,
226 (void *)arg
227 );
Guido van Rossum80230992001-10-12 21:49:17 +0000228
Fred Drake03459a52001-11-09 16:00:41 +0000229#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000231#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (status != 0)
233 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000236
Guido van Rossum3c288632001-10-16 21:13:49 +0000237#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000239#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000241#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242}
243
Trent Mick635f6fb2000-08-23 21:33:05 +0000244/* XXX This implementation is considered (to quote Tim Peters) "inherently
245 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000246 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000247 - The cast to long is inherently unsafe.
Jesus Cea736e7fc2011-03-14 17:36:54 +0100248 - It is not clear that the 'volatile' (for AIX?) are any longer necessary.
Trent Mick635f6fb2000-08-23 21:33:05 +0000249*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 volatile pthread_t threadid;
254 if (!initialized)
255 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 threadid = pthread_self();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return (long) threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000258}
259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 dprintf(("PyThread_exit_thread called\n"));
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200264 if (!initialized)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 exit(0);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200266 pthread_exit(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000267}
268
Martin v. Löwiscc898662002-03-17 09:53:51 +0000269#ifdef USE_SEMAPHORES
270
271/*
272 * Lock support.
273 */
274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000276PyThread_allocate_lock(void)
277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 sem_t *lock;
279 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 dprintf(("PyThread_allocate_lock called\n"));
282 if (!initialized)
283 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000284
Victor Stinner80aa5652013-07-07 17:17:59 +0200285 lock = (sem_t *)PyMem_RawMalloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 if (lock) {
288 status = sem_init(lock,0,1);
289 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (error) {
Victor Stinner80aa5652013-07-07 17:17:59 +0200292 PyMem_RawFree((void *)lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 lock = NULL;
294 }
295 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
298 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000299}
300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000302PyThread_free_lock(PyThread_type_lock lock)
303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 sem_t *thelock = (sem_t *)lock;
305 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000306
Christian Heimes56379c02012-12-02 08:37:00 +0100307 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (!thelock)
311 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 status = sem_destroy(thelock);
314 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000315
Victor Stinner80aa5652013-07-07 17:17:59 +0200316 PyMem_RawFree((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000317}
318
319/*
320 * As of February 2002, Cygwin thread implementations mistakenly report error
321 * codes in the return value of the sem_ calls (like the pthread_ functions).
322 * Correct implementations return -1 and put the code in errno. This supports
323 * either.
324 */
325static int
326fix_status(int status)
327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000329}
330
Antoine Pitrou810023d2010-12-15 22:59:16 +0000331PyLockStatus
332PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
333 int intr_flag)
Martin v. Löwiscc898662002-03-17 09:53:51 +0000334{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000335 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 sem_t *thelock = (sem_t *)lock;
337 int status, error = 0;
338 struct timespec ts;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000339
Christian Heimes56379c02012-12-02 08:37:00 +0100340 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000341 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
342 lock, microseconds, intr_flag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 if (microseconds > 0)
345 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
346 do {
347 if (microseconds > 0)
348 status = fix_status(sem_timedwait(thelock, &ts));
349 else if (microseconds == 0)
350 status = fix_status(sem_trywait(thelock));
351 else
352 status = fix_status(sem_wait(thelock));
Antoine Pitrou810023d2010-12-15 22:59:16 +0000353 /* Retry if interrupted by a signal, unless the caller wants to be
354 notified. */
355 } while (!intr_flag && status == EINTR);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000356
Antoine Pitrou810023d2010-12-15 22:59:16 +0000357 /* Don't check the status if we're stopping because of an interrupt. */
358 if (!(intr_flag && status == EINTR)) {
359 if (microseconds > 0) {
360 if (status != ETIMEDOUT)
361 CHECK_STATUS("sem_timedwait");
362 }
363 else if (microseconds == 0) {
364 if (status != EAGAIN)
365 CHECK_STATUS("sem_trywait");
366 }
367 else {
368 CHECK_STATUS("sem_wait");
369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000371
Antoine Pitrou810023d2010-12-15 22:59:16 +0000372 if (status == 0) {
373 success = PY_LOCK_ACQUIRED;
374 } else if (intr_flag && status == EINTR) {
375 success = PY_LOCK_INTR;
376 } else {
377 success = PY_LOCK_FAILURE;
378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379
Antoine Pitrou810023d2010-12-15 22:59:16 +0000380 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
381 lock, microseconds, intr_flag, success));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000383}
384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000386PyThread_release_lock(PyThread_type_lock lock)
387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 sem_t *thelock = (sem_t *)lock;
389 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000390
Christian Heimes56379c02012-12-02 08:37:00 +0100391 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 status = sem_post(thelock);
395 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000396}
397
398#else /* USE_SEMAPHORES */
399
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000400/*
401 * Lock support.
402 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000404PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 pthread_lock *lock;
407 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 dprintf(("PyThread_allocate_lock called\n"));
410 if (!initialized)
411 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000412
Victor Stinner80aa5652013-07-07 17:17:59 +0200413 lock = (pthread_lock *) PyMem_RawMalloc(sizeof(pthread_lock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (lock) {
415 memset((void *)lock, '\0', sizeof(pthread_lock));
416 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 status = pthread_mutex_init(&lock->mut,
419 pthread_mutexattr_default);
420 CHECK_STATUS("pthread_mutex_init");
421 /* Mark the pthread mutex underlying a Python mutex as
422 pure happens-before. We can't simply mark the
423 Python-level mutex as a mutex because it can be
424 acquired and released in different threads, which
425 will cause errors. */
426 _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 status = pthread_cond_init(&lock->lock_released,
429 pthread_condattr_default);
430 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (error) {
Victor Stinner80aa5652013-07-07 17:17:59 +0200433 PyMem_RawFree((void *)lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 lock = 0;
435 }
436 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
439 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000440}
441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 pthread_lock *thelock = (pthread_lock *)lock;
446 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000447
Antoine Pitrou9a00e0a2013-06-18 22:17:48 +0200448 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000450
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000451 /* some pthread-like implementations tie the mutex to the cond
452 * and must have the cond destroyed first.
453 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 status = pthread_cond_destroy( &thelock->lock_released );
455 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000456
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000457 status = pthread_mutex_destroy( &thelock->mut );
458 CHECK_STATUS("pthread_mutex_destroy");
459
Victor Stinner80aa5652013-07-07 17:17:59 +0200460 PyMem_RawFree((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000461}
462
Antoine Pitrou810023d2010-12-15 22:59:16 +0000463PyLockStatus
464PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
465 int intr_flag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000466{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000467 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 pthread_lock *thelock = (pthread_lock *)lock;
469 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000470
Antoine Pitrou810023d2010-12-15 22:59:16 +0000471 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
472 lock, microseconds, intr_flag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 status = pthread_mutex_lock( &thelock->mut );
475 CHECK_STATUS("pthread_mutex_lock[1]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000476
Antoine Pitrou810023d2010-12-15 22:59:16 +0000477 if (thelock->locked == 0) {
478 success = PY_LOCK_ACQUIRED;
479 } else if (microseconds == 0) {
480 success = PY_LOCK_FAILURE;
481 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 struct timespec ts;
483 if (microseconds > 0)
484 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
485 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 /* mut must be locked by me -- part of the condition
488 * protocol */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000489 success = PY_LOCK_FAILURE;
490 while (success == PY_LOCK_FAILURE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (microseconds > 0) {
492 status = pthread_cond_timedwait(
493 &thelock->lock_released,
494 &thelock->mut, &ts);
495 if (status == ETIMEDOUT)
496 break;
497 CHECK_STATUS("pthread_cond_timed_wait");
498 }
499 else {
500 status = pthread_cond_wait(
501 &thelock->lock_released,
502 &thelock->mut);
503 CHECK_STATUS("pthread_cond_wait");
504 }
Antoine Pitrou810023d2010-12-15 22:59:16 +0000505
506 if (intr_flag && status == 0 && thelock->locked) {
507 /* We were woken up, but didn't get the lock. We probably received
508 * a signal. Return PY_LOCK_INTR to allow the caller to handle
509 * it and retry. */
510 success = PY_LOCK_INTR;
511 break;
512 } else if (status == 0 && !thelock->locked) {
513 success = PY_LOCK_ACQUIRED;
514 } else {
515 success = PY_LOCK_FAILURE;
516 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 }
Antoine Pitrou810023d2010-12-15 22:59:16 +0000519 if (success == PY_LOCK_ACQUIRED) thelock->locked = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 status = pthread_mutex_unlock( &thelock->mut );
521 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000522
Antoine Pitrou810023d2010-12-15 22:59:16 +0000523 if (error) success = PY_LOCK_FAILURE;
524 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
525 lock, microseconds, intr_flag, success));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000527}
528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 pthread_lock *thelock = (pthread_lock *)lock;
533 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000534
Antoine Pitrou9a00e0a2013-06-18 22:17:48 +0200535 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 status = pthread_mutex_lock( &thelock->mut );
539 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* wake up someone (anyone, if any) waiting on the lock */
544 status = pthread_cond_signal( &thelock->lock_released );
545 CHECK_STATUS("pthread_cond_signal");
Kristján Valur Jónsson187aa542012-06-05 22:17:42 +0000546
547 status = pthread_mutex_unlock( &thelock->mut );
548 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000549}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000550
551#endif /* USE_SEMAPHORES */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000552
Antoine Pitrou810023d2010-12-15 22:59:16 +0000553int
554PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
555{
556 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0, /*intr_flag=*/0);
557}
558
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559/* set the thread stack size.
560 * Return 0 if size is valid, -1 if size is invalid,
561 * -2 if setting stack size is not supported.
562 */
563static int
564_pythread_pthread_set_stacksize(size_t size)
565{
566#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 pthread_attr_t attrs;
568 size_t tss_min;
569 int rc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000570#endif
571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 /* set to default */
573 if (size == 0) {
574 _pythread_stacksize = 0;
575 return 0;
576 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000577
578#if defined(THREAD_STACK_SIZE)
579#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
581 : THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 tss_min = THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000584#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (size >= tss_min) {
586 /* validate stack size by setting thread attribute */
587 if (pthread_attr_init(&attrs) == 0) {
588 rc = pthread_attr_setstacksize(&attrs, size);
589 pthread_attr_destroy(&attrs);
590 if (rc == 0) {
591 _pythread_stacksize = size;
592 return 0;
593 }
594 }
595 }
596 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000597#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599#endif
600}
601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000603
604#define Py_HAVE_NATIVE_TLS
605
606int
607PyThread_create_key(void)
608{
609 pthread_key_t key;
610 int fail = pthread_key_create(&key, NULL);
Victor Stinnerdaca3d72014-08-17 22:11:06 +0200611 if (fail)
612 return -1;
613 if (key > INT_MAX) {
614 /* Issue #22206: handle integer overflow */
615 pthread_key_delete(key);
616 errno = ENOMEM;
617 return -1;
618 }
619 return (int)key;
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000620}
621
622void
623PyThread_delete_key(int key)
624{
625 pthread_key_delete(key);
626}
627
628void
629PyThread_delete_key_value(int key)
630{
631 pthread_setspecific(key, NULL);
632}
633
634int
635PyThread_set_key_value(int key, void *value)
636{
637 int fail;
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000638 fail = pthread_setspecific(key, value);
639 return fail ? -1 : 0;
640}
641
642void *
643PyThread_get_key_value(int key)
644{
645 return pthread_getspecific(key);
646}
647
648void
649PyThread_ReInitTLS(void)
650{}