blob: c9ed796cd0fe0afe26af863f249495140ea77a2b [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
Andrew MacIntyre92913322006-06-13 15:04:24 +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 Pitrouc83ea132010-05-09 14:46:46 +000019#define THREAD_STACK_SIZE 0 /* use default stack size */
Andrew MacIntyre92913322006-06-13 15:04:24 +000020#endif
Ned Deily482f9082011-05-28 00:11:54 -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
Andrew MacIntyre92913322006-06-13 15:04:24 +000033/* for safety, ensure a viable minimum stacksize */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000034#define THREAD_STACK_MIN 0x8000 /* 32kB */
Andrew MacIntyre92913322006-06-13 15:04:24 +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 Pitrouc83ea132010-05-09 14:46:46 +000043 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000044#ifdef _POSIX_SEMAPHORES
Antoine Pitrouc83ea132010-05-09 14:46:46 +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
Hye-Shik Changd478f342006-03-23 12:32:36 +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 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000079#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000080# define USE_SEMAPHORES
81#else
82# undef USE_SEMAPHORES
83#endif
84
85
Guido van Rossum80230992001-10-12 21:49:17 +000086/* On platforms that don't use standard POSIX threads pthread_sigmask()
87 * isn't present. DEC threads uses sigprocmask() instead as do most
88 * other UNIX International compliant systems that don't have the full
89 * pthread implementation.
90 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000091#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000092# define SET_THREAD_SIGMASK pthread_sigmask
93#else
94# define SET_THREAD_SIGMASK sigprocmask
95#endif
96
97
Guido van Rossumb98b1b31994-05-11 08:42:04 +000098/* A pthread mutex isn't sufficient to model the Python lock type
99 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
100 * following are undefined:
101 * -> a thread tries to lock a mutex it already has locked
102 * -> a thread tries to unlock a mutex locked by a different thread
103 * pthread mutexes are designed for serializing threads over short pieces
104 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000105 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000106 *
107 * The pthread_lock struct implements a Python lock as a "locked?" bit
108 * and a <condition, mutex> pair. In general, if the bit can be acquired
109 * instantly, it is, else the pair is used to block the thread until the
110 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000111 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000112
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 char locked; /* 0=unlocked, 1=locked */
115 /* a <cond, mutex> pair to handle an acquire of a locked lock */
116 pthread_cond_t lock_released;
117 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000118} pthread_lock;
119
Guido van Rossum9e46e561998-10-07 16:39:47 +0000120#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000121
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000122/*
123 * Initialization.
124 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000125
126#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127static
128void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000129{
130}
131
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000132static void
133PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000135 /* DO AN INIT BY STARTING THE THREAD */
136 static int dummy = 0;
137 pthread_t thread1;
138 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
139 pthread_join(thread1, NULL);
Guido van Rossum9e46e561998-10-07 16:39:47 +0000140}
141
142#else /* !_HAVE_BSDI */
143
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144static void
145PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000147#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrou7e9cec02013-06-18 22:17:48 +0200148 extern void pthread_init(void);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000149 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000150#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000151}
152
Guido van Rossum9e46e561998-10-07 16:39:47 +0000153#endif /* !_HAVE_BSDI */
154
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000155/*
156 * Thread support.
157 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000158
159
Guido van Rossum3c288632001-10-16 21:13:49 +0000160long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000161PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000162{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 pthread_t th;
164 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000165#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000167#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000168#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000169 size_t tss;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000170#endif
171
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 dprintf(("PyThread_start_new_thread called\n"));
173 if (!initialized)
174 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000175
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000176#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 if (pthread_attr_init(&attrs) != 0)
178 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000179#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000180#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
182 : THREAD_STACK_SIZE;
183 if (tss != 0) {
184 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
185 pthread_attr_destroy(&attrs);
186 return -1;
187 }
188 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000189#endif
Hye-Shik Changd478f342006-03-23 12:32:36 +0000190#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000192#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000193
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000194 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000195#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000197#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000198 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000199#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 (void* (*)(void *))func,
201 (void *)arg
202 );
Guido van Rossum80230992001-10-12 21:49:17 +0000203
Fred Drake03459a52001-11-09 16:00:41 +0000204#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000206#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000207 if (status != 0)
208 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000209
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000210 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000211
Guido van Rossum3c288632001-10-16 21:13:49 +0000212#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000214#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000216#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000217}
218
Trent Mick635f6fb2000-08-23 21:33:05 +0000219/* XXX This implementation is considered (to quote Tim Peters) "inherently
220 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000221 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000222 - The cast to long is inherently unsafe.
223 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
224 latter return statement (for Alpha OSF/1) are any longer necessary.
225*/
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000228{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 volatile pthread_t threadid;
230 if (!initialized)
231 PyThread_init_thread();
232 /* Jump through some hoops for Alpha OSF/1 */
233 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000234#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000235 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000236#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000238#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000239}
240
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000242PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000243{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000244 dprintf(("PyThread_exit_thread called\n"));
Antoine Pitroub9a45012014-11-21 02:04:21 +0100245 if (!initialized) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000246 exit(0);
Antoine Pitroub9a45012014-11-21 02:04:21 +0100247 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000248}
249
Martin v. Löwiscc898662002-03-17 09:53:51 +0000250#ifdef USE_SEMAPHORES
251
252/*
253 * Lock support.
254 */
255
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000256PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000257PyThread_allocate_lock(void)
258{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 sem_t *lock;
260 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000261
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000262 dprintf(("PyThread_allocate_lock called\n"));
263 if (!initialized)
264 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000265
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000267
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 if (lock) {
269 status = sem_init(lock,0,1);
270 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000271
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 if (error) {
273 free((void *)lock);
274 lock = NULL;
275 }
276 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000277
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
279 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000280}
281
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000283PyThread_free_lock(PyThread_type_lock lock)
284{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000285 sem_t *thelock = (sem_t *)lock;
286 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000287
Jesus Cea7ddd9c22012-12-05 14:41:11 +0100288 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000289 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000290
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000291 if (!thelock)
292 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000293
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 status = sem_destroy(thelock);
295 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000296
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 free((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000298}
299
300/*
301 * As of February 2002, Cygwin thread implementations mistakenly report error
302 * codes in the return value of the sem_ calls (like the pthread_ functions).
303 * Correct implementations return -1 and put the code in errno. This supports
304 * either.
305 */
306static int
307fix_status(int status)
308{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000310}
311
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312int
Martin v. Löwiscc898662002-03-17 09:53:51 +0000313PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
314{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 int success;
316 sem_t *thelock = (sem_t *)lock;
317 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000318
Jesus Cea7ddd9c22012-12-05 14:41:11 +0100319 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000320 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000321
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 do {
323 if (waitflag)
324 status = fix_status(sem_wait(thelock));
325 else
326 status = fix_status(sem_trywait(thelock));
327 } while (status == EINTR); /* Retry if interrupted by a signal */
Martin v. Löwiscc898662002-03-17 09:53:51 +0000328
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 if (waitflag) {
330 CHECK_STATUS("sem_wait");
331 } else if (status != EAGAIN) {
332 CHECK_STATUS("sem_trywait");
333 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 success = (status == 0) ? 1 : 0;
336
337 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
338 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000339}
340
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000342PyThread_release_lock(PyThread_type_lock lock)
343{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 sem_t *thelock = (sem_t *)lock;
345 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000346
Jesus Cea7ddd9c22012-12-05 14:41:11 +0100347 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000348 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000349
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 status = sem_post(thelock);
351 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000352}
353
354#else /* USE_SEMAPHORES */
355
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000356/*
357 * Lock support.
358 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000359PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000360PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000361{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 pthread_lock *lock;
363 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000364
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 dprintf(("PyThread_allocate_lock called\n"));
366 if (!initialized)
367 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000368
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
370 if (lock) {
371 memset((void *)lock, '\0', sizeof(pthread_lock));
372 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000373
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374 status = pthread_mutex_init(&lock->mut,
375 pthread_mutexattr_default);
376 CHECK_STATUS("pthread_mutex_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000377
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 status = pthread_cond_init(&lock->lock_released,
379 pthread_condattr_default);
380 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000381
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 if (error) {
383 free((void *)lock);
384 lock = 0;
385 }
386 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000387
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
389 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000390}
391
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000394{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000395 pthread_lock *thelock = (pthread_lock *)lock;
396 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000397
Antoine Pitrou7e9cec02013-06-18 22:17:48 +0200398 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000400
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000401 status = pthread_mutex_destroy( &thelock->mut );
402 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000403
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 status = pthread_cond_destroy( &thelock->lock_released );
405 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000406
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000408}
409
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000411PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000412{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 int success;
414 pthread_lock *thelock = (pthread_lock *)lock;
415 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000416
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000417 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000418
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000419 status = pthread_mutex_lock( &thelock->mut );
420 CHECK_STATUS("pthread_mutex_lock[1]");
421 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000422
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000423 if ( !success && waitflag ) {
424 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000425
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 /* mut must be locked by me -- part of the condition
427 * protocol */
428 while ( thelock->locked ) {
429 status = pthread_cond_wait(&thelock->lock_released,
430 &thelock->mut);
431 CHECK_STATUS("pthread_cond_wait");
432 }
433 success = 1;
434 }
435 if (success) thelock->locked = 1;
436 status = pthread_mutex_unlock( &thelock->mut );
437 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000438
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000439 if (error) success = 0;
440 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
441 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000442}
443
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000444void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000445PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000446{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000447 pthread_lock *thelock = (pthread_lock *)lock;
448 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000449
Antoine Pitrou7e9cec02013-06-18 22:17:48 +0200450 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000451 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000452
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 status = pthread_mutex_lock( &thelock->mut );
454 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000455
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000456 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000457
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 status = pthread_mutex_unlock( &thelock->mut );
459 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000460
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 /* wake up someone (anyone, if any) waiting on the lock */
462 status = pthread_cond_signal( &thelock->lock_released );
463 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000464}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000465
466#endif /* USE_SEMAPHORES */
Andrew MacIntyre92913322006-06-13 15:04:24 +0000467
468/* set the thread stack size.
469 * Return 0 if size is valid, -1 if size is invalid,
470 * -2 if setting stack size is not supported.
471 */
472static int
473_pythread_pthread_set_stacksize(size_t size)
474{
475#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 pthread_attr_t attrs;
477 size_t tss_min;
478 int rc = 0;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000479#endif
480
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000481 /* set to default */
482 if (size == 0) {
483 _pythread_stacksize = 0;
484 return 0;
485 }
Andrew MacIntyre92913322006-06-13 15:04:24 +0000486
487#if defined(THREAD_STACK_SIZE)
488#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000489 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
490 : THREAD_STACK_MIN;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000491#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 tss_min = THREAD_STACK_MIN;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000493#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 if (size >= tss_min) {
495 /* validate stack size by setting thread attribute */
496 if (pthread_attr_init(&attrs) == 0) {
497 rc = pthread_attr_setstacksize(&attrs, size);
498 pthread_attr_destroy(&attrs);
499 if (rc == 0) {
500 _pythread_stacksize = size;
501 return 0;
502 }
503 }
504 }
505 return -1;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000506#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 return -2;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000508#endif
509}
510
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)