blob: e2907e08b2e4dae0f5eba7c676bf983100118f09 [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 MacIntyre6539d2d2006-06-04 12:31:09 +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
19#define THREAD_STACK_SIZE 0 /* use default stack size */
20#endif
21/* for safety, ensure a viable minimum stacksize */
22#define THREAD_STACK_MIN 0x8000 /* 32kB */
23#if THREAD_STACK_MIN < PTHREAD_STACK_MIN
24#undef THREAD_STACK_MIN
25#define THREAD_STACK_MIN PTHREAD_STACK_MIN
26#endif
27#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
28#ifdef THREAD_STACK_SIZE
29#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
30#endif
31#endif
32
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000033/* The POSIX spec says that implementations supporting the sem_*
34 family of functions must indicate this by defining
35 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000036#ifdef _POSIX_SEMAPHORES
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000037/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
38 we need to add 0 to make it work there as well. */
39#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000040#define HAVE_BROKEN_POSIX_SEMAPHORES
41#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000042#include <semaphore.h>
43#include <errno.h>
44#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000045#endif
Guido van Rossum66020991996-06-11 18:32:18 +000046
Hye-Shik Changd478f342006-03-23 12:32:36 +000047/* Before FreeBSD 5.4, system scope threads was very limited resource
48 in default setting. So the process scope is preferred to get
49 enough number of threads to work. */
50#ifdef __FreeBSD__
51#include <osreldate.h>
52#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
53#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
54#endif
55#endif
56
Martin v. Löwisb0233812002-12-11 13:12:30 +000057#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000058# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000059#endif
60#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000061# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000062#endif
63#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000064# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000065#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000066
Guido van Rossumd6353e21997-05-13 17:51:13 +000067
Martin v. Löwiscc898662002-03-17 09:53:51 +000068/* Whether or not to use semaphores directly rather than emulating them with
69 * mutexes and condition variables:
70 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000071#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000072# define USE_SEMAPHORES
73#else
74# undef USE_SEMAPHORES
75#endif
76
77
Guido van Rossum80230992001-10-12 21:49:17 +000078/* On platforms that don't use standard POSIX threads pthread_sigmask()
79 * isn't present. DEC threads uses sigprocmask() instead as do most
80 * other UNIX International compliant systems that don't have the full
81 * pthread implementation.
82 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000083#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000084# define SET_THREAD_SIGMASK pthread_sigmask
85#else
86# define SET_THREAD_SIGMASK sigprocmask
87#endif
88
89
Guido van Rossumb98b1b31994-05-11 08:42:04 +000090/* A pthread mutex isn't sufficient to model the Python lock type
91 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
92 * following are undefined:
93 * -> a thread tries to lock a mutex it already has locked
94 * -> a thread tries to unlock a mutex locked by a different thread
95 * pthread mutexes are designed for serializing threads over short pieces
96 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000097 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000098 *
99 * The pthread_lock struct implements a Python lock as a "locked?" bit
100 * and a <condition, mutex> pair. In general, if the bit can be acquired
101 * instantly, it is, else the pair is used to block the thread until the
102 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000103 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000104
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000105typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000106 char locked; /* 0=unlocked, 1=locked */
107 /* a <cond, mutex> pair to handle an acquire of a locked lock */
108 pthread_cond_t lock_released;
109 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000110} pthread_lock;
111
Guido van Rossum9e46e561998-10-07 16:39:47 +0000112#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000113
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000114/*
115 * Initialization.
116 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000117
118#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000119static
120void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000121{
122}
123
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124static void
125PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000126{
127 /* DO AN INIT BY STARTING THE THREAD */
128 static int dummy = 0;
129 pthread_t thread1;
130 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
131 pthread_join(thread1, NULL);
132}
133
134#else /* !_HAVE_BSDI */
135
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136static void
137PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000139#if defined(_AIX) && defined(__GNUC__)
140 pthread_init();
141#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000142}
143
Guido van Rossum9e46e561998-10-07 16:39:47 +0000144#endif /* !_HAVE_BSDI */
145
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146/*
147 * Thread support.
148 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000149
150
Guido van Rossum3c288632001-10-16 21:13:49 +0000151long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000153{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154 pthread_t th;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000155 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000156#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000157 pthread_attr_t attrs;
158#endif
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +0000159#if defined(THREAD_STACK_SIZE)
160 size_t tss;
161#endif
162
Guido van Rossum65d5b571998-12-21 19:32:43 +0000163 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000164 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000165 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000166
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000167#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000168 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000169#endif
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +0000170#if defined(THREAD_STACK_SIZE)
171 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
172 : THREAD_STACK_SIZE;
173 if (tss != 0) {
174 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
175 pthread_attr_destroy(&attrs);
176 return -1;
177 }
178 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000179#endif
Hye-Shik Changd478f342006-03-23 12:32:36 +0000180#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000181 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
182#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000183
Martin v. Löwis910ae622003-04-19 07:44:52 +0000184 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000185#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000186 &attrs,
187#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000188 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000189#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000190 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000191 (void *)arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000192 );
Guido van Rossum80230992001-10-12 21:49:17 +0000193
Fred Drake03459a52001-11-09 16:00:41 +0000194#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000195 pthread_attr_destroy(&attrs);
196#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000197 if (status != 0)
198 return -1;
199
Martin v. Löwis910ae622003-04-19 07:44:52 +0000200 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000201
Guido van Rossum3c288632001-10-16 21:13:49 +0000202#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
203 return (long) th;
204#else
205 return (long) *(long *) &th;
206#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000207}
208
Trent Mick635f6fb2000-08-23 21:33:05 +0000209/* XXX This implementation is considered (to quote Tim Peters) "inherently
210 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000211 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000212 - The cast to long is inherently unsafe.
213 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
214 latter return statement (for Alpha OSF/1) are any longer necessary.
215*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216long
217PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000218{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000219 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000220 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000222 /* Jump through some hoops for Alpha OSF/1 */
223 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000224#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
225 return (long) threadid;
226#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000227 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000228#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000229}
230
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000231static void
232do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000233{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000234 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000235 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000236 if (no_cleanup)
237 _exit(0);
238 else
239 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000240 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000241}
242
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243void
244PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000245{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000247}
248
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249void
250PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000251{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000252 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000253}
254
255#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256static void
257do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000258{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000260 if (!initialized)
261 if (no_cleanup)
262 _exit(status);
263 else
264 exit(status);
265}
266
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000267void
268PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000269{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000270 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000271}
272
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000273void
274PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000275{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000276 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000277}
278#endif /* NO_EXIT_PROG */
279
Martin v. Löwiscc898662002-03-17 09:53:51 +0000280#ifdef USE_SEMAPHORES
281
282/*
283 * Lock support.
284 */
285
286PyThread_type_lock
287PyThread_allocate_lock(void)
288{
289 sem_t *lock;
290 int status, error = 0;
291
292 dprintf(("PyThread_allocate_lock called\n"));
293 if (!initialized)
294 PyThread_init_thread();
295
296 lock = (sem_t *)malloc(sizeof(sem_t));
297
298 if (lock) {
299 status = sem_init(lock,0,1);
300 CHECK_STATUS("sem_init");
301
302 if (error) {
303 free((void *)lock);
304 lock = NULL;
305 }
306 }
307
308 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
309 return (PyThread_type_lock)lock;
310}
311
312void
313PyThread_free_lock(PyThread_type_lock lock)
314{
315 sem_t *thelock = (sem_t *)lock;
316 int status, error = 0;
317
318 dprintf(("PyThread_free_lock(%p) called\n", lock));
319
320 if (!thelock)
321 return;
322
323 status = sem_destroy(thelock);
324 CHECK_STATUS("sem_destroy");
325
326 free((void *)thelock);
327}
328
329/*
330 * As of February 2002, Cygwin thread implementations mistakenly report error
331 * codes in the return value of the sem_ calls (like the pthread_ functions).
332 * Correct implementations return -1 and put the code in errno. This supports
333 * either.
334 */
335static int
336fix_status(int status)
337{
338 return (status == -1) ? errno : status;
339}
340
341int
342PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
343{
344 int success;
345 sem_t *thelock = (sem_t *)lock;
346 int status, error = 0;
347
348 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
349
350 do {
351 if (waitflag)
352 status = fix_status(sem_wait(thelock));
353 else
354 status = fix_status(sem_trywait(thelock));
355 } while (status == EINTR); /* Retry if interrupted by a signal */
356
357 if (waitflag) {
358 CHECK_STATUS("sem_wait");
359 } else if (status != EAGAIN) {
360 CHECK_STATUS("sem_trywait");
361 }
362
363 success = (status == 0) ? 1 : 0;
364
365 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
366 return success;
367}
368
369void
370PyThread_release_lock(PyThread_type_lock lock)
371{
372 sem_t *thelock = (sem_t *)lock;
373 int status, error = 0;
374
375 dprintf(("PyThread_release_lock(%p) called\n", lock));
376
377 status = sem_post(thelock);
378 CHECK_STATUS("sem_post");
379}
380
381#else /* USE_SEMAPHORES */
382
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000383/*
384 * Lock support.
385 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386PyThread_type_lock
387PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000388{
389 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000390 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000391
Guido van Rossum65d5b571998-12-21 19:32:43 +0000392 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000393 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000394 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000395
396 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000397 if (lock) {
Armin Rigoa6eb56c2005-09-20 18:07:47 +0000398 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000399 lock->locked = 0;
400
401 status = pthread_mutex_init(&lock->mut,
402 pthread_mutexattr_default);
403 CHECK_STATUS("pthread_mutex_init");
404
405 status = pthread_cond_init(&lock->lock_released,
406 pthread_condattr_default);
407 CHECK_STATUS("pthread_cond_init");
408
409 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000410 free((void *)lock);
411 lock = 0;
412 }
413 }
414
Fred Drakea44d3532000-06-30 15:01:00 +0000415 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000416 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000417}
418
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000419void
420PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000421{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000422 pthread_lock *thelock = (pthread_lock *)lock;
423 int status, error = 0;
424
Fred Drakea44d3532000-06-30 15:01:00 +0000425 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000426
427 status = pthread_mutex_destroy( &thelock->mut );
428 CHECK_STATUS("pthread_mutex_destroy");
429
430 status = pthread_cond_destroy( &thelock->lock_released );
431 CHECK_STATUS("pthread_cond_destroy");
432
433 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000434}
435
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436int
437PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000438{
439 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000440 pthread_lock *thelock = (pthread_lock *)lock;
441 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000442
Fred Drakea44d3532000-06-30 15:01:00 +0000443 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000444
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000445 status = pthread_mutex_lock( &thelock->mut );
446 CHECK_STATUS("pthread_mutex_lock[1]");
447 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000448
449 if ( !success && waitflag ) {
450 /* continue trying until we get the lock */
451
452 /* mut must be locked by me -- part of the condition
453 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000454 while ( thelock->locked ) {
455 status = pthread_cond_wait(&thelock->lock_released,
456 &thelock->mut);
457 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000458 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000459 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000460 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000461 if (success) thelock->locked = 1;
462 status = pthread_mutex_unlock( &thelock->mut );
463 CHECK_STATUS("pthread_mutex_unlock[1]");
464
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000465 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000466 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000467 return success;
468}
469
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000470void
471PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000472{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000473 pthread_lock *thelock = (pthread_lock *)lock;
474 int status, error = 0;
475
Fred Drakea44d3532000-06-30 15:01:00 +0000476 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000477
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000478 status = pthread_mutex_lock( &thelock->mut );
479 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000480
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000481 thelock->locked = 0;
482
483 status = pthread_mutex_unlock( &thelock->mut );
484 CHECK_STATUS("pthread_mutex_unlock[3]");
485
486 /* wake up someone (anyone, if any) waiting on the lock */
487 status = pthread_cond_signal( &thelock->lock_released );
488 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000489}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000490
491#endif /* USE_SEMAPHORES */
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +0000492
493/* set the thread stack size.
494 * Return 1 if an exception is pending, 0 otherwise.
495 */
496static int
497_pythread_pthread_set_stacksize(size_t size)
498{
499 /* set to default */
500 if (size == 0) {
501 _pythread_stacksize = 0;
502 return 0;
503 }
504
505 /* valid range? */
506 if (size >= THREAD_STACK_MIN) {
507 _pythread_stacksize = size;
508 return 0;
509 }
510 else {
511 char warning[128];
512 snprintf(warning,
513 128,
514 "thread stack size of %#x bytes not supported",
515 size);
516 return PyErr_Warn(PyExc_RuntimeWarning, warning);
517 }
518}
519
520#undef THREAD_SET_STACKSIZE
521#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)