blob: 3efccf660a50b0e4af0c9ae22fb80c5fc816a8b7 [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
22#if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
23 /* The default stack size for new threads on OSX is small enough that
24 * we'll get hard crashes instead of 'maximum recursion depth exceeded'
25 * exceptions.
26 *
27 * The default stack size below is the minimal stack size where a
28 * simple recursive function doesn't cause a hard crash.
29 */
30#undef THREAD_STACK_SIZE
31#define THREAD_STACK_SIZE 0x400000
32#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033/* for safety, ensure a viable minimum stacksize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034#define THREAD_STACK_MIN 0x8000 /* 32kB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000035#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
36#ifdef THREAD_STACK_SIZE
37#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
38#endif
39#endif
40
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000041/* The POSIX spec says that implementations supporting the sem_*
42 family of functions must indicate this by defining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000044#ifdef _POSIX_SEMAPHORES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000046 we need to add 0 to make it work there as well. */
47#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000048#define HAVE_BROKEN_POSIX_SEMAPHORES
49#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000050#include <semaphore.h>
51#include <errno.h>
52#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000053#endif
Guido van Rossum66020991996-06-11 18:32:18 +000054
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055/* Before FreeBSD 5.4, system scope threads was very limited resource
56 in default setting. So the process scope is preferred to get
57 enough number of threads to work. */
58#ifdef __FreeBSD__
59#include <osreldate.h>
60#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
61#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
62#endif
63#endif
64
Martin v. Löwisb0233812002-12-11 13:12:30 +000065#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000066# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000067#endif
68#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000069# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000070#endif
71#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000072# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000073#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000074
Guido van Rossumd6353e21997-05-13 17:51:13 +000075
Martin v. Löwiscc898662002-03-17 09:53:51 +000076/* Whether or not to use semaphores directly rather than emulating them with
77 * mutexes and condition variables:
78 */
Antoine Pitrou19f8edc2010-10-10 08:37:22 +000079#if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \
80 defined(HAVE_SEM_TIMEDWAIT))
Martin v. Löwiscc898662002-03-17 09:53:51 +000081# define USE_SEMAPHORES
82#else
83# undef USE_SEMAPHORES
84#endif
85
86
Guido van Rossum80230992001-10-12 21:49:17 +000087/* On platforms that don't use standard POSIX threads pthread_sigmask()
88 * isn't present. DEC threads uses sigprocmask() instead as do most
89 * other UNIX International compliant systems that don't have the full
90 * pthread implementation.
91 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000092#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000093# define SET_THREAD_SIGMASK pthread_sigmask
94#else
95# define SET_THREAD_SIGMASK sigprocmask
96#endif
97
98
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000099/* We assume all modern POSIX systems have gettimeofday() */
100#ifdef GETTIMEOFDAY_NO_TZ
101#define GETTIMEOFDAY(ptv) gettimeofday(ptv)
102#else
103#define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
104#endif
105
106#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \
107do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 struct timeval tv; \
109 GETTIMEOFDAY(&tv); \
110 tv.tv_usec += microseconds % 1000000; \
111 tv.tv_sec += microseconds / 1000000; \
112 tv.tv_sec += tv.tv_usec / 1000000; \
113 tv.tv_usec %= 1000000; \
114 ts.tv_sec = tv.tv_sec; \
115 ts.tv_nsec = tv.tv_usec * 1000; \
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000116} while(0)
117
118
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000119/* A pthread mutex isn't sufficient to model the Python lock type
120 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
121 * following are undefined:
122 * -> a thread tries to lock a mutex it already has locked
123 * -> a thread tries to unlock a mutex locked by a different thread
124 * pthread mutexes are designed for serializing threads over short pieces
125 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000127 *
128 * The pthread_lock struct implements a Python lock as a "locked?" bit
129 * and a <condition, mutex> pair. In general, if the bit can be acquired
130 * instantly, it is, else the pair is used to block the thread until the
131 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000132 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000133
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000134typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 char locked; /* 0=unlocked, 1=locked */
136 /* a <cond, mutex> pair to handle an acquire of a locked lock */
137 pthread_cond_t lock_released;
138 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000139} pthread_lock;
140
Guido van Rossum9e46e561998-10-07 16:39:47 +0000141#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000142
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000143/*
144 * Initialization.
145 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000146
147#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000148static
149void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000150{
151}
152
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153static void
154PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* DO AN INIT BY STARTING THE THREAD */
157 static int dummy = 0;
158 pthread_t thread1;
159 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
160 pthread_join(thread1, NULL);
Guido van Rossum9e46e561998-10-07 16:39:47 +0000161}
162
163#else /* !_HAVE_BSDI */
164
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000165static void
166PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000167{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000168#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000170#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000171}
172
Guido van Rossum9e46e561998-10-07 16:39:47 +0000173#endif /* !_HAVE_BSDI */
174
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000175/*
176 * Thread support.
177 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000178
179
Guido van Rossum3c288632001-10-16 21:13:49 +0000180long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000181PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 pthread_t th;
184 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000185#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000187#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 size_t tss;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000190#endif
191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 dprintf(("PyThread_start_new_thread called\n"));
193 if (!initialized)
194 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000195
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000196#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (pthread_attr_init(&attrs) != 0)
198 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000199#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
202 : THREAD_STACK_SIZE;
203 if (tss != 0) {
204 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
205 pthread_attr_destroy(&attrs);
206 return -1;
207 }
208 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000209#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000212#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000215#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000217#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000219#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 (void* (*)(void *))func,
221 (void *)arg
222 );
Guido van Rossum80230992001-10-12 21:49:17 +0000223
Fred Drake03459a52001-11-09 16:00:41 +0000224#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000226#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (status != 0)
228 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000231
Guido van Rossum3c288632001-10-16 21:13:49 +0000232#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000234#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000236#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000237}
238
Trent Mick635f6fb2000-08-23 21:33:05 +0000239/* XXX This implementation is considered (to quote Tim Peters) "inherently
240 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000241 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000242 - The cast to long is inherently unsafe.
243 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
244 latter return statement (for Alpha OSF/1) are any longer necessary.
245*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 volatile pthread_t threadid;
250 if (!initialized)
251 PyThread_init_thread();
252 /* Jump through some hoops for Alpha OSF/1 */
253 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000254#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000256#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000258#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000259}
260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 dprintf(("PyThread_exit_thread called\n"));
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200265 if (!initialized)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 exit(0);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200267 pthread_exit(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000268}
269
Martin v. Löwiscc898662002-03-17 09:53:51 +0000270#ifdef USE_SEMAPHORES
271
272/*
273 * Lock support.
274 */
275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000277PyThread_allocate_lock(void)
278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 sem_t *lock;
280 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 dprintf(("PyThread_allocate_lock called\n"));
283 if (!initialized)
284 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (lock) {
289 status = sem_init(lock,0,1);
290 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 if (error) {
293 free((void *)lock);
294 lock = NULL;
295 }
296 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
299 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000300}
301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000303PyThread_free_lock(PyThread_type_lock lock)
304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 sem_t *thelock = (sem_t *)lock;
306 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000307
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 free((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
Antoine Pitrou810023d2010-12-15 22:59:16 +0000340 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
341 lock, microseconds, intr_flag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (microseconds > 0)
344 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
345 do {
346 if (microseconds > 0)
347 status = fix_status(sem_timedwait(thelock, &ts));
348 else if (microseconds == 0)
349 status = fix_status(sem_trywait(thelock));
350 else
351 status = fix_status(sem_wait(thelock));
Antoine Pitrou810023d2010-12-15 22:59:16 +0000352 /* Retry if interrupted by a signal, unless the caller wants to be
353 notified. */
354 } while (!intr_flag && status == EINTR);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000355
Antoine Pitrou810023d2010-12-15 22:59:16 +0000356 /* Don't check the status if we're stopping because of an interrupt. */
357 if (!(intr_flag && status == EINTR)) {
358 if (microseconds > 0) {
359 if (status != ETIMEDOUT)
360 CHECK_STATUS("sem_timedwait");
361 }
362 else if (microseconds == 0) {
363 if (status != EAGAIN)
364 CHECK_STATUS("sem_trywait");
365 }
366 else {
367 CHECK_STATUS("sem_wait");
368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000370
Antoine Pitrou810023d2010-12-15 22:59:16 +0000371 if (status == 0) {
372 success = PY_LOCK_ACQUIRED;
373 } else if (intr_flag && status == EINTR) {
374 success = PY_LOCK_INTR;
375 } else {
376 success = PY_LOCK_FAILURE;
377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378
Antoine Pitrou810023d2010-12-15 22:59:16 +0000379 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
380 lock, microseconds, intr_flag, success));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000382}
383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000385PyThread_release_lock(PyThread_type_lock lock)
386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 sem_t *thelock = (sem_t *)lock;
388 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 status = sem_post(thelock);
393 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000394}
395
396#else /* USE_SEMAPHORES */
397
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000398/*
399 * Lock support.
400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000402PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 pthread_lock *lock;
405 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 dprintf(("PyThread_allocate_lock called\n"));
408 if (!initialized)
409 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
412 if (lock) {
413 memset((void *)lock, '\0', sizeof(pthread_lock));
414 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 status = pthread_mutex_init(&lock->mut,
417 pthread_mutexattr_default);
418 CHECK_STATUS("pthread_mutex_init");
419 /* Mark the pthread mutex underlying a Python mutex as
420 pure happens-before. We can't simply mark the
421 Python-level mutex as a mutex because it can be
422 acquired and released in different threads, which
423 will cause errors. */
424 _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 status = pthread_cond_init(&lock->lock_released,
427 pthread_condattr_default);
428 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (error) {
431 free((void *)lock);
432 lock = 0;
433 }
434 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
437 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000438}
439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000441PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 pthread_lock *thelock = (pthread_lock *)lock;
444 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 status = pthread_mutex_destroy( &thelock->mut );
449 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 status = pthread_cond_destroy( &thelock->lock_released );
452 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000455}
456
Antoine Pitrou810023d2010-12-15 22:59:16 +0000457PyLockStatus
458PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
459 int intr_flag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000460{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000461 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 pthread_lock *thelock = (pthread_lock *)lock;
463 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000464
Antoine Pitrou810023d2010-12-15 22:59:16 +0000465 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
466 lock, microseconds, intr_flag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 status = pthread_mutex_lock( &thelock->mut );
469 CHECK_STATUS("pthread_mutex_lock[1]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000470
Antoine Pitrou810023d2010-12-15 22:59:16 +0000471 if (thelock->locked == 0) {
472 success = PY_LOCK_ACQUIRED;
473 } else if (microseconds == 0) {
474 success = PY_LOCK_FAILURE;
475 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 struct timespec ts;
477 if (microseconds > 0)
478 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
479 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 /* mut must be locked by me -- part of the condition
482 * protocol */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000483 success = PY_LOCK_FAILURE;
484 while (success == PY_LOCK_FAILURE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (microseconds > 0) {
486 status = pthread_cond_timedwait(
487 &thelock->lock_released,
488 &thelock->mut, &ts);
489 if (status == ETIMEDOUT)
490 break;
491 CHECK_STATUS("pthread_cond_timed_wait");
492 }
493 else {
494 status = pthread_cond_wait(
495 &thelock->lock_released,
496 &thelock->mut);
497 CHECK_STATUS("pthread_cond_wait");
498 }
Antoine Pitrou810023d2010-12-15 22:59:16 +0000499
500 if (intr_flag && status == 0 && thelock->locked) {
501 /* We were woken up, but didn't get the lock. We probably received
502 * a signal. Return PY_LOCK_INTR to allow the caller to handle
503 * it and retry. */
504 success = PY_LOCK_INTR;
505 break;
506 } else if (status == 0 && !thelock->locked) {
507 success = PY_LOCK_ACQUIRED;
508 } else {
509 success = PY_LOCK_FAILURE;
510 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
Antoine Pitrou810023d2010-12-15 22:59:16 +0000513 if (success == PY_LOCK_ACQUIRED) thelock->locked = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 status = pthread_mutex_unlock( &thelock->mut );
515 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000516
Antoine Pitrou810023d2010-12-15 22:59:16 +0000517 if (error) success = PY_LOCK_FAILURE;
518 dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
519 lock, microseconds, intr_flag, success));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000521}
522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000524PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 pthread_lock *thelock = (pthread_lock *)lock;
527 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 status = pthread_mutex_lock( &thelock->mut );
532 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 status = pthread_mutex_unlock( &thelock->mut );
537 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 /* wake up someone (anyone, if any) waiting on the lock */
540 status = pthread_cond_signal( &thelock->lock_released );
541 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000542}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000543
544#endif /* USE_SEMAPHORES */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000545
Antoine Pitrou810023d2010-12-15 22:59:16 +0000546int
547PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
548{
549 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0, /*intr_flag=*/0);
550}
551
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000552/* set the thread stack size.
553 * Return 0 if size is valid, -1 if size is invalid,
554 * -2 if setting stack size is not supported.
555 */
556static int
557_pythread_pthread_set_stacksize(size_t size)
558{
559#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 pthread_attr_t attrs;
561 size_t tss_min;
562 int rc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563#endif
564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* set to default */
566 if (size == 0) {
567 _pythread_stacksize = 0;
568 return 0;
569 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000570
571#if defined(THREAD_STACK_SIZE)
572#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
574 : THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000575#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 tss_min = THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000577#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (size >= tss_min) {
579 /* validate stack size by setting thread attribute */
580 if (pthread_attr_init(&attrs) == 0) {
581 rc = pthread_attr_setstacksize(&attrs, size);
582 pthread_attr_destroy(&attrs);
583 if (rc == 0) {
584 _pythread_stacksize = size;
585 return 0;
586 }
587 }
588 }
589 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000590#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000592#endif
593}
594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000596
597#define Py_HAVE_NATIVE_TLS
598
599int
600PyThread_create_key(void)
601{
602 pthread_key_t key;
603 int fail = pthread_key_create(&key, NULL);
604 return fail ? -1 : key;
605}
606
607void
608PyThread_delete_key(int key)
609{
610 pthread_key_delete(key);
611}
612
613void
614PyThread_delete_key_value(int key)
615{
616 pthread_setspecific(key, NULL);
617}
618
619int
620PyThread_set_key_value(int key, void *value)
621{
622 int fail;
623 void *oldValue = pthread_getspecific(key);
624 if (oldValue != NULL)
625 return 0;
626 fail = pthread_setspecific(key, value);
627 return fail ? -1 : 0;
628}
629
630void *
631PyThread_get_key_value(int key)
632{
633 return pthread_getspecific(key);
634}
635
636void
637PyThread_ReInitTLS(void)
638{}