blob: a529b7a7eb191cd1778605c7a5cd10269fd3bb26 [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
21/* for safety, ensure a viable minimum stacksize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022#define THREAD_STACK_MIN 0x8000 /* 32kB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
24#ifdef THREAD_STACK_SIZE
25#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
26#endif
27#endif
28
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000029/* The POSIX spec says that implementations supporting the sem_*
30 family of functions must indicate this by defining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000032#ifdef _POSIX_SEMAPHORES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000034 we need to add 0 to make it work there as well. */
35#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000036#define HAVE_BROKEN_POSIX_SEMAPHORES
37#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000038#include <semaphore.h>
39#include <errno.h>
40#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000041#endif
Guido van Rossum66020991996-06-11 18:32:18 +000042
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043/* Before FreeBSD 5.4, system scope threads was very limited resource
44 in default setting. So the process scope is preferred to get
45 enough number of threads to work. */
46#ifdef __FreeBSD__
47#include <osreldate.h>
48#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
49#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
50#endif
51#endif
52
Martin v. Löwisb0233812002-12-11 13:12:30 +000053#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000054# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000055#endif
56#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000057# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000058#endif
59#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000060# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000061#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000062
Guido van Rossumd6353e21997-05-13 17:51:13 +000063
Martin v. Löwiscc898662002-03-17 09:53:51 +000064/* Whether or not to use semaphores directly rather than emulating them with
65 * mutexes and condition variables:
66 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000067#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000068# define USE_SEMAPHORES
69#else
70# undef USE_SEMAPHORES
71#endif
72
73
Guido van Rossum80230992001-10-12 21:49:17 +000074/* On platforms that don't use standard POSIX threads pthread_sigmask()
75 * isn't present. DEC threads uses sigprocmask() instead as do most
76 * other UNIX International compliant systems that don't have the full
77 * pthread implementation.
78 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000079#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000080# define SET_THREAD_SIGMASK pthread_sigmask
81#else
82# define SET_THREAD_SIGMASK sigprocmask
83#endif
84
85
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000086/* We assume all modern POSIX systems have gettimeofday() */
87#ifdef GETTIMEOFDAY_NO_TZ
88#define GETTIMEOFDAY(ptv) gettimeofday(ptv)
89#else
90#define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
91#endif
92
93#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \
94do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 struct timeval tv; \
96 GETTIMEOFDAY(&tv); \
97 tv.tv_usec += microseconds % 1000000; \
98 tv.tv_sec += microseconds / 1000000; \
99 tv.tv_sec += tv.tv_usec / 1000000; \
100 tv.tv_usec %= 1000000; \
101 ts.tv_sec = tv.tv_sec; \
102 ts.tv_nsec = tv.tv_usec * 1000; \
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000103} while(0)
104
105
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000106/* A pthread mutex isn't sufficient to model the Python lock type
107 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
108 * following are undefined:
109 * -> a thread tries to lock a mutex it already has locked
110 * -> a thread tries to unlock a mutex locked by a different thread
111 * pthread mutexes are designed for serializing threads over short pieces
112 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000114 *
115 * The pthread_lock struct implements a Python lock as a "locked?" bit
116 * and a <condition, mutex> pair. In general, if the bit can be acquired
117 * instantly, it is, else the pair is used to block the thread until the
118 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000119 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000120
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000121typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 char locked; /* 0=unlocked, 1=locked */
123 /* a <cond, mutex> pair to handle an acquire of a locked lock */
124 pthread_cond_t lock_released;
125 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126} pthread_lock;
127
Guido van Rossum9e46e561998-10-07 16:39:47 +0000128#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000129
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000130/*
131 * Initialization.
132 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000133
134#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135static
136void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000137{
138}
139
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000140static void
141PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 /* DO AN INIT BY STARTING THE THREAD */
144 static int dummy = 0;
145 pthread_t thread1;
146 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
147 pthread_join(thread1, NULL);
Guido van Rossum9e46e561998-10-07 16:39:47 +0000148}
149
150#else /* !_HAVE_BSDI */
151
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152static void
153PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000155#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000157#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000158}
159
Guido van Rossum9e46e561998-10-07 16:39:47 +0000160#endif /* !_HAVE_BSDI */
161
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000162/*
163 * Thread support.
164 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000165
166
Guido van Rossum3c288632001-10-16 21:13:49 +0000167long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000168PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 pthread_t th;
171 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000172#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000174#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000175#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 size_t tss;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000177#endif
178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 dprintf(("PyThread_start_new_thread called\n"));
180 if (!initialized)
181 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000182
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000183#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (pthread_attr_init(&attrs) != 0)
185 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000186#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000187#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
189 : THREAD_STACK_SIZE;
190 if (tss != 0) {
191 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
192 pthread_attr_destroy(&attrs);
193 return -1;
194 }
195 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000196#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000199#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000202#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000204#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 (void* (*)(void *))func,
208 (void *)arg
209 );
Guido van Rossum80230992001-10-12 21:49:17 +0000210
Fred Drake03459a52001-11-09 16:00:41 +0000211#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000213#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (status != 0)
215 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000218
Guido van Rossum3c288632001-10-16 21:13:49 +0000219#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000221#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000223#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000224}
225
Trent Mick635f6fb2000-08-23 21:33:05 +0000226/* XXX This implementation is considered (to quote Tim Peters) "inherently
227 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000228 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000229 - The cast to long is inherently unsafe.
230 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
231 latter return statement (for Alpha OSF/1) are any longer necessary.
232*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 volatile pthread_t threadid;
237 if (!initialized)
238 PyThread_init_thread();
239 /* Jump through some hoops for Alpha OSF/1 */
240 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000241#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000243#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000245#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000246}
247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 dprintf(("PyThread_exit_thread called\n"));
252 if (!initialized) {
253 exit(0);
254 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000255}
256
Martin v. Löwiscc898662002-03-17 09:53:51 +0000257#ifdef USE_SEMAPHORES
258
259/*
260 * Lock support.
261 */
262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000264PyThread_allocate_lock(void)
265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 sem_t *lock;
267 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 dprintf(("PyThread_allocate_lock called\n"));
270 if (!initialized)
271 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 if (lock) {
276 status = sem_init(lock,0,1);
277 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (error) {
280 free((void *)lock);
281 lock = NULL;
282 }
283 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
286 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000287}
288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000290PyThread_free_lock(PyThread_type_lock lock)
291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 sem_t *thelock = (sem_t *)lock;
293 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (!thelock)
298 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 status = sem_destroy(thelock);
301 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 free((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000304}
305
306/*
307 * As of February 2002, Cygwin thread implementations mistakenly report error
308 * codes in the return value of the sem_ calls (like the pthread_ functions).
309 * Correct implementations return -1 and put the code in errno. This supports
310 * either.
311 */
312static int
313fix_status(int status)
314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000316}
317
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000318int
319PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
Martin v. Löwiscc898662002-03-17 09:53:51 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 int success;
322 sem_t *thelock = (sem_t *)lock;
323 int status, error = 0;
324 struct timespec ts;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 dprintf(("PyThread_acquire_lock_timed(%p, %lld) called\n",
327 lock, microseconds));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (microseconds > 0)
330 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
331 do {
332 if (microseconds > 0)
333 status = fix_status(sem_timedwait(thelock, &ts));
334 else if (microseconds == 0)
335 status = fix_status(sem_trywait(thelock));
336 else
337 status = fix_status(sem_wait(thelock));
338 } while (status == EINTR); /* Retry if interrupted by a signal */
Martin v. Löwiscc898662002-03-17 09:53:51 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (microseconds > 0) {
341 if (status != ETIMEDOUT)
342 CHECK_STATUS("sem_timedwait");
343 }
344 else if (microseconds == 0) {
345 if (status != EAGAIN)
346 CHECK_STATUS("sem_trywait");
347 }
348 else {
349 CHECK_STATUS("sem_wait");
350 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 success = (status == 0) ? 1 : 0;
353
354 dprintf(("PyThread_acquire_lock_timed(%p, %lld) -> %d\n",
355 lock, microseconds, success));
356 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000357}
358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359int
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000360PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000363}
364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000366PyThread_release_lock(PyThread_type_lock lock)
367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 sem_t *thelock = (sem_t *)lock;
369 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 status = sem_post(thelock);
374 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000375}
376
377#else /* USE_SEMAPHORES */
378
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000379/*
380 * Lock support.
381 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 pthread_lock *lock;
386 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 dprintf(("PyThread_allocate_lock called\n"));
389 if (!initialized)
390 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
393 if (lock) {
394 memset((void *)lock, '\0', sizeof(pthread_lock));
395 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 status = pthread_mutex_init(&lock->mut,
398 pthread_mutexattr_default);
399 CHECK_STATUS("pthread_mutex_init");
400 /* Mark the pthread mutex underlying a Python mutex as
401 pure happens-before. We can't simply mark the
402 Python-level mutex as a mutex because it can be
403 acquired and released in different threads, which
404 will cause errors. */
405 _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 status = pthread_cond_init(&lock->lock_released,
408 pthread_condattr_default);
409 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (error) {
412 free((void *)lock);
413 lock = 0;
414 }
415 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
418 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000419}
420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000422PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 pthread_lock *thelock = (pthread_lock *)lock;
425 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 status = pthread_mutex_destroy( &thelock->mut );
430 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 status = pthread_cond_destroy( &thelock->lock_released );
433 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000436}
437
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000438int
439PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 int success;
442 pthread_lock *thelock = (pthread_lock *)lock;
443 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 dprintf(("PyThread_acquire_lock_timed(%p, %lld) called\n",
446 lock, microseconds));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 status = pthread_mutex_lock( &thelock->mut );
449 CHECK_STATUS("pthread_mutex_lock[1]");
450 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (!success && microseconds != 0) {
453 struct timespec ts;
454 if (microseconds > 0)
455 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
456 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* mut must be locked by me -- part of the condition
459 * protocol */
460 while (thelock->locked) {
461 if (microseconds > 0) {
462 status = pthread_cond_timedwait(
463 &thelock->lock_released,
464 &thelock->mut, &ts);
465 if (status == ETIMEDOUT)
466 break;
467 CHECK_STATUS("pthread_cond_timed_wait");
468 }
469 else {
470 status = pthread_cond_wait(
471 &thelock->lock_released,
472 &thelock->mut);
473 CHECK_STATUS("pthread_cond_wait");
474 }
475 }
476 success = (status == 0);
477 }
478 if (success) thelock->locked = 1;
479 status = pthread_mutex_unlock( &thelock->mut );
480 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (error) success = 0;
483 dprintf(("PyThread_acquire_lock_timed(%p, %lld) -> %d\n",
484 lock, microseconds, success));
485 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000486}
487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488int
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000489PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000492}
493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000495PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 pthread_lock *thelock = (pthread_lock *)lock;
498 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 status = pthread_mutex_lock( &thelock->mut );
503 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 status = pthread_mutex_unlock( &thelock->mut );
508 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* wake up someone (anyone, if any) waiting on the lock */
511 status = pthread_cond_signal( &thelock->lock_released );
512 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000513}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000514
515#endif /* USE_SEMAPHORES */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000516
517/* set the thread stack size.
518 * Return 0 if size is valid, -1 if size is invalid,
519 * -2 if setting stack size is not supported.
520 */
521static int
522_pythread_pthread_set_stacksize(size_t size)
523{
524#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 pthread_attr_t attrs;
526 size_t tss_min;
527 int rc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528#endif
529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* set to default */
531 if (size == 0) {
532 _pythread_stacksize = 0;
533 return 0;
534 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000535
536#if defined(THREAD_STACK_SIZE)
537#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
539 : THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 tss_min = THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (size >= tss_min) {
544 /* validate stack size by setting thread attribute */
545 if (pthread_attr_init(&attrs) == 0) {
546 rc = pthread_attr_setstacksize(&attrs, size);
547 pthread_attr_destroy(&attrs);
548 if (rc == 0) {
549 _pythread_stacksize = size;
550 return 0;
551 }
552 }
553 }
554 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000555#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557#endif
558}
559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000561
562#define Py_HAVE_NATIVE_TLS
563
564int
565PyThread_create_key(void)
566{
567 pthread_key_t key;
568 int fail = pthread_key_create(&key, NULL);
569 return fail ? -1 : key;
570}
571
572void
573PyThread_delete_key(int key)
574{
575 pthread_key_delete(key);
576}
577
578void
579PyThread_delete_key_value(int key)
580{
581 pthread_setspecific(key, NULL);
582}
583
584int
585PyThread_set_key_value(int key, void *value)
586{
587 int fail;
588 void *oldValue = pthread_getspecific(key);
589 if (oldValue != NULL)
590 return 0;
591 fail = pthread_setspecific(key, value);
592 return fail ? -1 : 0;
593}
594
595void *
596PyThread_get_key_value(int key)
597{
598 return pthread_getspecific(key);
599}
600
601void
602PyThread_ReInitTLS(void)
603{}