blob: 244ebacaadc0b1180d8042be727f8245efb1c1dc [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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +000043 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000044#ifdef _POSIX_SEMAPHORES
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 */
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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000148 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000149#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000150}
151
Guido van Rossum9e46e561998-10-07 16:39:47 +0000152#endif /* !_HAVE_BSDI */
153
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154/*
155 * Thread support.
156 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000157
158
Guido van Rossum3c288632001-10-16 21:13:49 +0000159long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000161{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000162 pthread_t th;
163 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000164#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000166#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167#if defined(THREAD_STACK_SIZE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 size_t tss;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000169#endif
170
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 dprintf(("PyThread_start_new_thread called\n"));
172 if (!initialized)
173 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000174
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000175#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 if (pthread_attr_init(&attrs) != 0)
177 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000178#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000179#if defined(THREAD_STACK_SIZE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000180 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
181 : THREAD_STACK_SIZE;
182 if (tss != 0) {
183 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
184 pthread_attr_destroy(&attrs);
185 return -1;
186 }
187 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000188#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000191#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000192
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000193 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000194#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000196#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000197 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000198#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 (void* (*)(void *))func,
200 (void *)arg
201 );
Guido van Rossum80230992001-10-12 21:49:17 +0000202
Fred Drake03459a52001-11-09 16:00:41 +0000203#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000204 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000205#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000206 if (status != 0)
207 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000208
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000210
Guido van Rossum3c288632001-10-16 21:13:49 +0000211#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000212 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000213#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000215#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000216}
217
Trent Mick635f6fb2000-08-23 21:33:05 +0000218/* XXX This implementation is considered (to quote Tim Peters) "inherently
219 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000220 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000221 - The cast to long is inherently unsafe.
222 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
223 latter return statement (for Alpha OSF/1) are any longer necessary.
224*/
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000225long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000227{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 volatile pthread_t threadid;
229 if (!initialized)
230 PyThread_init_thread();
231 /* Jump through some hoops for Alpha OSF/1 */
232 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000233#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000235#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000237#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000238}
239
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 dprintf(("PyThread_exit_thread called\n"));
244 if (!initialized) {
245 if (no_cleanup)
246 _exit(0);
247 else
248 exit(0);
249 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000250}
251
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000254{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000255 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000256}
257
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000260{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000261 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000262}
263
264#ifndef NO_EXIT_PROG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000267{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000268 dprintf(("PyThread_exit_prog(%d) called\n", status));
269 if (!initialized)
270 if (no_cleanup)
271 _exit(status);
272 else
273 exit(status);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000274}
275
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000278{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000279 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000280}
281
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000282void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000283PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000284{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000286}
287#endif /* NO_EXIT_PROG */
288
Martin v. Löwiscc898662002-03-17 09:53:51 +0000289#ifdef USE_SEMAPHORES
290
291/*
292 * Lock support.
293 */
294
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000295PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000296PyThread_allocate_lock(void)
297{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 sem_t *lock;
299 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000300
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000301 dprintf(("PyThread_allocate_lock called\n"));
302 if (!initialized)
303 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000305 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000306
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000307 if (lock) {
308 status = sem_init(lock,0,1);
309 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000310
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000311 if (error) {
312 free((void *)lock);
313 lock = NULL;
314 }
315 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000317 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
318 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000319}
320
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000321void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000322PyThread_free_lock(PyThread_type_lock lock)
323{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000324 sem_t *thelock = (sem_t *)lock;
325 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000326
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000327 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000328
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000329 if (!thelock)
330 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 status = sem_destroy(thelock);
333 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000334
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 free((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000336}
337
338/*
339 * As of February 2002, Cygwin thread implementations mistakenly report error
340 * codes in the return value of the sem_ calls (like the pthread_ functions).
341 * Correct implementations return -1 and put the code in errno. This supports
342 * either.
343 */
344static int
345fix_status(int status)
346{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000348}
349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000350int
Martin v. Löwiscc898662002-03-17 09:53:51 +0000351PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
352{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 int success;
354 sem_t *thelock = (sem_t *)lock;
355 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000356
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000357 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000358
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 do {
360 if (waitflag)
361 status = fix_status(sem_wait(thelock));
362 else
363 status = fix_status(sem_trywait(thelock));
364 } while (status == EINTR); /* Retry if interrupted by a signal */
Martin v. Löwiscc898662002-03-17 09:53:51 +0000365
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 if (waitflag) {
367 CHECK_STATUS("sem_wait");
368 } else if (status != EAGAIN) {
369 CHECK_STATUS("sem_trywait");
370 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000371
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 success = (status == 0) ? 1 : 0;
373
374 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
375 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000376}
377
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000378void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000379PyThread_release_lock(PyThread_type_lock lock)
380{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000381 sem_t *thelock = (sem_t *)lock;
382 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000383
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000384 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000385
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000386 status = sem_post(thelock);
387 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000388}
389
390#else /* USE_SEMAPHORES */
391
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000392/*
393 * Lock support.
394 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000395PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000396PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000397{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000398 pthread_lock *lock;
399 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000400
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 dprintf(("PyThread_allocate_lock called\n"));
402 if (!initialized)
403 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000404
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
406 if (lock) {
407 memset((void *)lock, '\0', sizeof(pthread_lock));
408 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000409
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 status = pthread_mutex_init(&lock->mut,
411 pthread_mutexattr_default);
412 CHECK_STATUS("pthread_mutex_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000413
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000414 status = pthread_cond_init(&lock->lock_released,
415 pthread_condattr_default);
416 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000417
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000418 if (error) {
419 free((void *)lock);
420 lock = 0;
421 }
422 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000423
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000424 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
425 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000426}
427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000428void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000429PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000430{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000431 pthread_lock *thelock = (pthread_lock *)lock;
432 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000434 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000435
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 status = pthread_mutex_destroy( &thelock->mut );
437 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000438
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000439 status = pthread_cond_destroy( &thelock->lock_released );
440 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000441
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000442 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000443}
444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000446PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000447{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000448 int success;
449 pthread_lock *thelock = (pthread_lock *)lock;
450 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000451
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000452 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000453
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000454 status = pthread_mutex_lock( &thelock->mut );
455 CHECK_STATUS("pthread_mutex_lock[1]");
456 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000457
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 if ( !success && waitflag ) {
459 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000460
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000461 /* mut must be locked by me -- part of the condition
462 * protocol */
463 while ( thelock->locked ) {
464 status = pthread_cond_wait(&thelock->lock_released,
465 &thelock->mut);
466 CHECK_STATUS("pthread_cond_wait");
467 }
468 success = 1;
469 }
470 if (success) thelock->locked = 1;
471 status = pthread_mutex_unlock( &thelock->mut );
472 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000473
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000474 if (error) success = 0;
475 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
476 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000477}
478
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000480PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000481{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 pthread_lock *thelock = (pthread_lock *)lock;
483 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000484
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000485 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000486
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 status = pthread_mutex_lock( &thelock->mut );
488 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000489
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000490 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000491
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000492 status = pthread_mutex_unlock( &thelock->mut );
493 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000494
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000495 /* wake up someone (anyone, if any) waiting on the lock */
496 status = pthread_cond_signal( &thelock->lock_released );
497 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000498}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000499
500#endif /* USE_SEMAPHORES */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000501
502/* set the thread stack size.
503 * Return 0 if size is valid, -1 if size is invalid,
504 * -2 if setting stack size is not supported.
505 */
506static int
507_pythread_pthread_set_stacksize(size_t size)
508{
509#if defined(THREAD_STACK_SIZE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000510 pthread_attr_t attrs;
511 size_t tss_min;
512 int rc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513#endif
514
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 /* set to default */
516 if (size == 0) {
517 _pythread_stacksize = 0;
518 return 0;
519 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520
521#if defined(THREAD_STACK_SIZE)
522#if defined(PTHREAD_STACK_MIN)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000523 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
524 : THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000526 tss_min = THREAD_STACK_MIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000527#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000528 if (size >= tss_min) {
529 /* validate stack size by setting thread attribute */
530 if (pthread_attr_init(&attrs) == 0) {
531 rc = pthread_attr_setstacksize(&attrs, size);
532 pthread_attr_destroy(&attrs);
533 if (rc == 0) {
534 _pythread_stacksize = size;
535 return 0;
536 }
537 }
538 }
539 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000541 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000542#endif
543}
544
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000545#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)