blob: 44e2552a8a87ea0d9709c458499fd11c951c8005 [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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000165 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000166#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000167#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 size_t tss;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000169#endif
170
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000176 if (pthread_attr_init(&attrs) != 0)
177 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000178#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000179#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +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
Hye-Shik Changd478f342006-03-23 12:32:36 +0000189#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000195 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000196#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000198#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000204 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000205#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000206 if (status != 0)
207 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000208
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000212 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000213#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000225long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000227{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000234 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000235#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 dprintf(("PyThread_exit_thread called\n"));
244 if (!initialized) {
245 exit(0);
246 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000247}
248
Martin v. Löwiscc898662002-03-17 09:53:51 +0000249#ifdef USE_SEMAPHORES
250
251/*
252 * Lock support.
253 */
254
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000255PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000256PyThread_allocate_lock(void)
257{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 sem_t *lock;
259 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000260
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 dprintf(("PyThread_allocate_lock called\n"));
262 if (!initialized)
263 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000264
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000266
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 if (lock) {
268 status = sem_init(lock,0,1);
269 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000270
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 if (error) {
272 free((void *)lock);
273 lock = NULL;
274 }
275 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000276
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000277 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
278 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000279}
280
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000282PyThread_free_lock(PyThread_type_lock lock)
283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 sem_t *thelock = (sem_t *)lock;
285 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000286
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000288
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000289 if (!thelock)
290 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000291
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 status = sem_destroy(thelock);
293 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000294
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000295 free((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000296}
297
298/*
299 * As of February 2002, Cygwin thread implementations mistakenly report error
300 * codes in the return value of the sem_ calls (like the pthread_ functions).
301 * Correct implementations return -1 and put the code in errno. This supports
302 * either.
303 */
304static int
305fix_status(int status)
306{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000307 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000308}
309
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310int
Martin v. Löwiscc898662002-03-17 09:53:51 +0000311PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
312{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 int success;
314 sem_t *thelock = (sem_t *)lock;
315 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000316
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000317 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000318
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000319 do {
320 if (waitflag)
321 status = fix_status(sem_wait(thelock));
322 else
323 status = fix_status(sem_trywait(thelock));
324 } while (status == EINTR); /* Retry if interrupted by a signal */
Martin v. Löwiscc898662002-03-17 09:53:51 +0000325
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 if (waitflag) {
327 CHECK_STATUS("sem_wait");
328 } else if (status != EAGAIN) {
329 CHECK_STATUS("sem_trywait");
330 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000331
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 success = (status == 0) ? 1 : 0;
333
334 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
335 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000336}
337
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000338void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000339PyThread_release_lock(PyThread_type_lock lock)
340{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 sem_t *thelock = (sem_t *)lock;
342 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000343
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000345
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 status = sem_post(thelock);
347 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000348}
349
350#else /* USE_SEMAPHORES */
351
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000352/*
353 * Lock support.
354 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000355PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000357{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 pthread_lock *lock;
359 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000360
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000361 dprintf(("PyThread_allocate_lock called\n"));
362 if (!initialized)
363 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000364
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
366 if (lock) {
367 memset((void *)lock, '\0', sizeof(pthread_lock));
368 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 status = pthread_mutex_init(&lock->mut,
371 pthread_mutexattr_default);
372 CHECK_STATUS("pthread_mutex_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000373
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374 status = pthread_cond_init(&lock->lock_released,
375 pthread_condattr_default);
376 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000377
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 if (error) {
379 free((void *)lock);
380 lock = 0;
381 }
382 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000383
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000384 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
385 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000386}
387
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000389PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000390{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000391 pthread_lock *thelock = (pthread_lock *)lock;
392 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000393
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000395
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000396 status = pthread_mutex_destroy( &thelock->mut );
397 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000398
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 status = pthread_cond_destroy( &thelock->lock_released );
400 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000401
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000402 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000403}
404
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000406PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000407{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408 int success;
409 pthread_lock *thelock = (pthread_lock *)lock;
410 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000411
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000413
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000414 status = pthread_mutex_lock( &thelock->mut );
415 CHECK_STATUS("pthread_mutex_lock[1]");
416 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000417
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 if ( !success && waitflag ) {
419 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000420
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000421 /* mut must be locked by me -- part of the condition
422 * protocol */
423 while ( thelock->locked ) {
424 status = pthread_cond_wait(&thelock->lock_released,
425 &thelock->mut);
426 CHECK_STATUS("pthread_cond_wait");
427 }
428 success = 1;
429 }
430 if (success) thelock->locked = 1;
431 status = pthread_mutex_unlock( &thelock->mut );
432 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000433
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 if (error) success = 0;
435 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
436 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437}
438
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000439void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000441{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000442 pthread_lock *thelock = (pthread_lock *)lock;
443 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000444
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000446
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000447 status = pthread_mutex_lock( &thelock->mut );
448 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000449
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000450 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000451
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 status = pthread_mutex_unlock( &thelock->mut );
453 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000454
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000455 /* wake up someone (anyone, if any) waiting on the lock */
456 status = pthread_cond_signal( &thelock->lock_released );
457 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000458}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000459
460#endif /* USE_SEMAPHORES */
Andrew MacIntyre92913322006-06-13 15:04:24 +0000461
462/* set the thread stack size.
463 * Return 0 if size is valid, -1 if size is invalid,
464 * -2 if setting stack size is not supported.
465 */
466static int
467_pythread_pthread_set_stacksize(size_t size)
468{
469#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 pthread_attr_t attrs;
471 size_t tss_min;
472 int rc = 0;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000473#endif
474
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000475 /* set to default */
476 if (size == 0) {
477 _pythread_stacksize = 0;
478 return 0;
479 }
Andrew MacIntyre92913322006-06-13 15:04:24 +0000480
481#if defined(THREAD_STACK_SIZE)
482#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
484 : THREAD_STACK_MIN;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000485#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000486 tss_min = THREAD_STACK_MIN;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000487#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 if (size >= tss_min) {
489 /* validate stack size by setting thread attribute */
490 if (pthread_attr_init(&attrs) == 0) {
491 rc = pthread_attr_setstacksize(&attrs, size);
492 pthread_attr_destroy(&attrs);
493 if (rc == 0) {
494 _pythread_stacksize = size;
495 return 0;
496 }
497 }
498 }
499 return -1;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000500#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 return -2;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000502#endif
503}
504
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000505#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)