blob: c007c8b90674ade2e51108a820ea5f5c6b824b5e [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 */
Antoine Pitrou19f8edc2010-10-10 08:37:22 +000067#if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \
68 defined(HAVE_SEM_TIMEDWAIT))
Martin v. Löwiscc898662002-03-17 09:53:51 +000069# define USE_SEMAPHORES
70#else
71# undef USE_SEMAPHORES
72#endif
73
74
Guido van Rossum80230992001-10-12 21:49:17 +000075/* On platforms that don't use standard POSIX threads pthread_sigmask()
76 * isn't present. DEC threads uses sigprocmask() instead as do most
77 * other UNIX International compliant systems that don't have the full
78 * pthread implementation.
79 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000080#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000081# define SET_THREAD_SIGMASK pthread_sigmask
82#else
83# define SET_THREAD_SIGMASK sigprocmask
84#endif
85
86
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000087/* We assume all modern POSIX systems have gettimeofday() */
88#ifdef GETTIMEOFDAY_NO_TZ
89#define GETTIMEOFDAY(ptv) gettimeofday(ptv)
90#else
91#define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
92#endif
93
94#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \
95do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 struct timeval tv; \
97 GETTIMEOFDAY(&tv); \
98 tv.tv_usec += microseconds % 1000000; \
99 tv.tv_sec += microseconds / 1000000; \
100 tv.tv_sec += tv.tv_usec / 1000000; \
101 tv.tv_usec %= 1000000; \
102 ts.tv_sec = tv.tv_sec; \
103 ts.tv_nsec = tv.tv_usec * 1000; \
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000104} while(0)
105
106
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000107/* A pthread mutex isn't sufficient to model the Python lock type
108 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
109 * following are undefined:
110 * -> a thread tries to lock a mutex it already has locked
111 * -> a thread tries to unlock a mutex locked by a different thread
112 * pthread mutexes are designed for serializing threads over short pieces
113 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000114 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000115 *
116 * The pthread_lock struct implements a Python lock as a "locked?" bit
117 * and a <condition, mutex> pair. In general, if the bit can be acquired
118 * instantly, it is, else the pair is used to block the thread until the
119 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000120 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000121
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000122typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 char locked; /* 0=unlocked, 1=locked */
124 /* a <cond, mutex> pair to handle an acquire of a locked lock */
125 pthread_cond_t lock_released;
126 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127} pthread_lock;
128
Guido van Rossum9e46e561998-10-07 16:39:47 +0000129#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000130
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000131/*
132 * Initialization.
133 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000134
135#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136static
137void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000138{
139}
140
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000141static void
142PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 /* DO AN INIT BY STARTING THE THREAD */
145 static int dummy = 0;
146 pthread_t thread1;
147 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
148 pthread_join(thread1, NULL);
Guido van Rossum9e46e561998-10-07 16:39:47 +0000149}
150
151#else /* !_HAVE_BSDI */
152
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153static void
154PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000155{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000156#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000158#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000159}
160
Guido van Rossum9e46e561998-10-07 16:39:47 +0000161#endif /* !_HAVE_BSDI */
162
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000163/*
164 * Thread support.
165 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000166
167
Guido van Rossum3c288632001-10-16 21:13:49 +0000168long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000169PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 pthread_t th;
172 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000173#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000175#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 size_t tss;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178#endif
179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 dprintf(("PyThread_start_new_thread called\n"));
181 if (!initialized)
182 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000183
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000184#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (pthread_attr_init(&attrs) != 0)
186 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000187#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
190 : THREAD_STACK_SIZE;
191 if (tss != 0) {
192 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
193 pthread_attr_destroy(&attrs);
194 return -1;
195 }
196 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000197#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000200#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000203#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000205#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000207#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 (void* (*)(void *))func,
209 (void *)arg
210 );
Guido van Rossum80230992001-10-12 21:49:17 +0000211
Fred Drake03459a52001-11-09 16:00:41 +0000212#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000214#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (status != 0)
216 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000219
Guido van Rossum3c288632001-10-16 21:13:49 +0000220#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000222#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000224#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000225}
226
Trent Mick635f6fb2000-08-23 21:33:05 +0000227/* XXX This implementation is considered (to quote Tim Peters) "inherently
228 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000229 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000230 - The cast to long is inherently unsafe.
231 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
232 latter return statement (for Alpha OSF/1) are any longer necessary.
233*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 volatile pthread_t threadid;
238 if (!initialized)
239 PyThread_init_thread();
240 /* Jump through some hoops for Alpha OSF/1 */
241 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000242#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000244#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000246#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000247}
248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000250PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 dprintf(("PyThread_exit_thread called\n"));
253 if (!initialized) {
254 exit(0);
255 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000256}
257
Martin v. Löwiscc898662002-03-17 09:53:51 +0000258#ifdef USE_SEMAPHORES
259
260/*
261 * Lock support.
262 */
263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000265PyThread_allocate_lock(void)
266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 sem_t *lock;
268 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 dprintf(("PyThread_allocate_lock called\n"));
271 if (!initialized)
272 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (lock) {
277 status = sem_init(lock,0,1);
278 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (error) {
281 free((void *)lock);
282 lock = NULL;
283 }
284 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
287 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000288}
289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000291PyThread_free_lock(PyThread_type_lock lock)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 sem_t *thelock = (sem_t *)lock;
294 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (!thelock)
299 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 status = sem_destroy(thelock);
302 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 free((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000305}
306
307/*
308 * As of February 2002, Cygwin thread implementations mistakenly report error
309 * codes in the return value of the sem_ calls (like the pthread_ functions).
310 * Correct implementations return -1 and put the code in errno. This supports
311 * either.
312 */
313static int
314fix_status(int status)
315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000317}
318
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000319int
320PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
Martin v. Löwiscc898662002-03-17 09:53:51 +0000321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 int success;
323 sem_t *thelock = (sem_t *)lock;
324 int status, error = 0;
325 struct timespec ts;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 dprintf(("PyThread_acquire_lock_timed(%p, %lld) called\n",
328 lock, microseconds));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (microseconds > 0)
331 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
332 do {
333 if (microseconds > 0)
334 status = fix_status(sem_timedwait(thelock, &ts));
335 else if (microseconds == 0)
336 status = fix_status(sem_trywait(thelock));
337 else
338 status = fix_status(sem_wait(thelock));
339 } while (status == EINTR); /* Retry if interrupted by a signal */
Martin v. Löwiscc898662002-03-17 09:53:51 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (microseconds > 0) {
342 if (status != ETIMEDOUT)
343 CHECK_STATUS("sem_timedwait");
344 }
345 else if (microseconds == 0) {
346 if (status != EAGAIN)
347 CHECK_STATUS("sem_trywait");
348 }
349 else {
350 CHECK_STATUS("sem_wait");
351 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 success = (status == 0) ? 1 : 0;
354
355 dprintf(("PyThread_acquire_lock_timed(%p, %lld) -> %d\n",
356 lock, microseconds, success));
357 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000358}
359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360int
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000361PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000364}
365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000367PyThread_release_lock(PyThread_type_lock lock)
368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 sem_t *thelock = (sem_t *)lock;
370 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 status = sem_post(thelock);
375 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000376}
377
378#else /* USE_SEMAPHORES */
379
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000380/*
381 * Lock support.
382 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 pthread_lock *lock;
387 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 dprintf(("PyThread_allocate_lock called\n"));
390 if (!initialized)
391 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
394 if (lock) {
395 memset((void *)lock, '\0', sizeof(pthread_lock));
396 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 status = pthread_mutex_init(&lock->mut,
399 pthread_mutexattr_default);
400 CHECK_STATUS("pthread_mutex_init");
401 /* Mark the pthread mutex underlying a Python mutex as
402 pure happens-before. We can't simply mark the
403 Python-level mutex as a mutex because it can be
404 acquired and released in different threads, which
405 will cause errors. */
406 _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 status = pthread_cond_init(&lock->lock_released,
409 pthread_condattr_default);
410 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (error) {
413 free((void *)lock);
414 lock = 0;
415 }
416 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
419 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000420}
421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 pthread_lock *thelock = (pthread_lock *)lock;
426 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 status = pthread_mutex_destroy( &thelock->mut );
431 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 status = pthread_cond_destroy( &thelock->lock_released );
434 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437}
438
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000439int
440PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 int success;
443 pthread_lock *thelock = (pthread_lock *)lock;
444 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 dprintf(("PyThread_acquire_lock_timed(%p, %lld) called\n",
447 lock, microseconds));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 status = pthread_mutex_lock( &thelock->mut );
450 CHECK_STATUS("pthread_mutex_lock[1]");
451 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (!success && microseconds != 0) {
454 struct timespec ts;
455 if (microseconds > 0)
456 MICROSECONDS_TO_TIMESPEC(microseconds, ts);
457 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* mut must be locked by me -- part of the condition
460 * protocol */
461 while (thelock->locked) {
462 if (microseconds > 0) {
463 status = pthread_cond_timedwait(
464 &thelock->lock_released,
465 &thelock->mut, &ts);
466 if (status == ETIMEDOUT)
467 break;
468 CHECK_STATUS("pthread_cond_timed_wait");
469 }
470 else {
471 status = pthread_cond_wait(
472 &thelock->lock_released,
473 &thelock->mut);
474 CHECK_STATUS("pthread_cond_wait");
475 }
476 }
477 success = (status == 0);
478 }
479 if (success) thelock->locked = 1;
480 status = pthread_mutex_unlock( &thelock->mut );
481 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (error) success = 0;
484 dprintf(("PyThread_acquire_lock_timed(%p, %lld) -> %d\n",
485 lock, microseconds, success));
486 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000487}
488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489int
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000490PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000493}
494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000496PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 pthread_lock *thelock = (pthread_lock *)lock;
499 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 status = pthread_mutex_lock( &thelock->mut );
504 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 status = pthread_mutex_unlock( &thelock->mut );
509 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* wake up someone (anyone, if any) waiting on the lock */
512 status = pthread_cond_signal( &thelock->lock_released );
513 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000514}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000515
516#endif /* USE_SEMAPHORES */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517
518/* set the thread stack size.
519 * Return 0 if size is valid, -1 if size is invalid,
520 * -2 if setting stack size is not supported.
521 */
522static int
523_pythread_pthread_set_stacksize(size_t size)
524{
525#if defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 pthread_attr_t attrs;
527 size_t tss_min;
528 int rc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000529#endif
530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 /* set to default */
532 if (size == 0) {
533 _pythread_stacksize = 0;
534 return 0;
535 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536
537#if defined(THREAD_STACK_SIZE)
538#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
540 : THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000541#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 tss_min = THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000543#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (size >= tss_min) {
545 /* validate stack size by setting thread attribute */
546 if (pthread_attr_init(&attrs) == 0) {
547 rc = pthread_attr_setstacksize(&attrs, size);
548 pthread_attr_destroy(&attrs);
549 if (rc == 0) {
550 _pythread_stacksize = size;
551 return 0;
552 }
553 }
554 }
555 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000556#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000558#endif
559}
560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000562
563#define Py_HAVE_NATIVE_TLS
564
565int
566PyThread_create_key(void)
567{
568 pthread_key_t key;
569 int fail = pthread_key_create(&key, NULL);
570 return fail ? -1 : key;
571}
572
573void
574PyThread_delete_key(int key)
575{
576 pthread_key_delete(key);
577}
578
579void
580PyThread_delete_key_value(int key)
581{
582 pthread_setspecific(key, NULL);
583}
584
585int
586PyThread_set_key_value(int key, void *value)
587{
588 int fail;
589 void *oldValue = pthread_getspecific(key);
590 if (oldValue != NULL)
591 return 0;
592 fail = pthread_setspecific(key, value);
593 return fail ? -1 : 0;
594}
595
596void *
597PyThread_get_key_value(int key)
598{
599 return pthread_getspecific(key);
600}
601
602void
603PyThread_ReInitTLS(void)
604{}