blob: 4305a198badba0ba8af9bfe8ce28e3d65f4ada06 [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
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#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
24#ifdef THREAD_STACK_SIZE
25#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
26#endif
27#endif
28
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000029/* The POSIX spec says that implementations supporting the sem_*
30 family of functions must indicate this by defining
31 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000032#ifdef _POSIX_SEMAPHORES
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000033/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
34 we need to add 0 to make it work there as well. */
35#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000036#define HAVE_BROKEN_POSIX_SEMAPHORES
37#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000038#include <semaphore.h>
39#include <errno.h>
40#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000041#endif
Guido van Rossum66020991996-06-11 18:32:18 +000042
Hye-Shik Changd478f342006-03-23 12:32:36 +000043/* Before FreeBSD 5.4, system scope threads was very limited resource
44 in default setting. So the process scope is preferred to get
45 enough number of threads to work. */
46#ifdef __FreeBSD__
47#include <osreldate.h>
48#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
49#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
50#endif
51#endif
52
Martin v. Löwisb0233812002-12-11 13:12:30 +000053#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000054# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000055#endif
56#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000057# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000058#endif
59#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000060# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000061#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000062
Guido van Rossumd6353e21997-05-13 17:51:13 +000063
Martin v. Löwiscc898662002-03-17 09:53:51 +000064/* Whether or not to use semaphores directly rather than emulating them with
65 * mutexes and condition variables:
66 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000067#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000068# define USE_SEMAPHORES
69#else
70# undef USE_SEMAPHORES
71#endif
72
73
Guido van Rossum80230992001-10-12 21:49:17 +000074/* On platforms that don't use standard POSIX threads pthread_sigmask()
75 * isn't present. DEC threads uses sigprocmask() instead as do most
76 * other UNIX International compliant systems that don't have the full
77 * pthread implementation.
78 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000079#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000080# define SET_THREAD_SIGMASK pthread_sigmask
81#else
82# define SET_THREAD_SIGMASK sigprocmask
83#endif
84
85
Guido van Rossumb98b1b31994-05-11 08:42:04 +000086/* A pthread mutex isn't sufficient to model the Python lock type
87 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
88 * following are undefined:
89 * -> a thread tries to lock a mutex it already has locked
90 * -> a thread tries to unlock a mutex locked by a different thread
91 * pthread mutexes are designed for serializing threads over short pieces
92 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000093 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000094 *
95 * The pthread_lock struct implements a Python lock as a "locked?" bit
96 * and a <condition, mutex> pair. In general, if the bit can be acquired
97 * instantly, it is, else the pair is used to block the thread until the
98 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000099 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000100
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000101typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000102 char locked; /* 0=unlocked, 1=locked */
103 /* a <cond, mutex> pair to handle an acquire of a locked lock */
104 pthread_cond_t lock_released;
105 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000106} pthread_lock;
107
Guido van Rossum9e46e561998-10-07 16:39:47 +0000108#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000109
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000110/*
111 * Initialization.
112 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000113
114#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000115static
116void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000117{
118}
119
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000120static void
121PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000122{
123 /* DO AN INIT BY STARTING THE THREAD */
124 static int dummy = 0;
125 pthread_t thread1;
126 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
127 pthread_join(thread1, NULL);
128}
129
130#else /* !_HAVE_BSDI */
131
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000132static void
133PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000134{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000135#if defined(_AIX) && defined(__GNUC__)
136 pthread_init();
137#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138}
139
Guido van Rossum9e46e561998-10-07 16:39:47 +0000140#endif /* !_HAVE_BSDI */
141
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000142/*
143 * Thread support.
144 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000145
146
Guido van Rossum3c288632001-10-16 21:13:49 +0000147long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000148PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000149{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000150 pthread_t th;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000151 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000152#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000153 pthread_attr_t attrs;
154#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000155#if defined(THREAD_STACK_SIZE)
156 size_t tss;
157#endif
158
Guido van Rossum65d5b571998-12-21 19:32:43 +0000159 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000160 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000161 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000162
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000163#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Andrew MacIntyre92913322006-06-13 15:04:24 +0000164 if (pthread_attr_init(&attrs) != 0)
165 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000166#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000167#if defined(THREAD_STACK_SIZE)
168 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
169 : THREAD_STACK_SIZE;
170 if (tss != 0) {
171 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
172 pthread_attr_destroy(&attrs);
173 return -1;
174 }
175 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000176#endif
Hye-Shik Changd478f342006-03-23 12:32:36 +0000177#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000178 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
179#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000180
Martin v. Löwis910ae622003-04-19 07:44:52 +0000181 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000182#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000183 &attrs,
184#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000185 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000186#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000188 (void *)arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000189 );
Guido van Rossum80230992001-10-12 21:49:17 +0000190
Fred Drake03459a52001-11-09 16:00:41 +0000191#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000192 pthread_attr_destroy(&attrs);
193#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000194 if (status != 0)
195 return -1;
196
Martin v. Löwis910ae622003-04-19 07:44:52 +0000197 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000198
Guido van Rossum3c288632001-10-16 21:13:49 +0000199#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
200 return (long) th;
201#else
202 return (long) *(long *) &th;
203#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000204}
205
Trent Mick635f6fb2000-08-23 21:33:05 +0000206/* XXX This implementation is considered (to quote Tim Peters) "inherently
207 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000208 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000209 - The cast to long is inherently unsafe.
210 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
211 latter return statement (for Alpha OSF/1) are any longer necessary.
212*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213long
214PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000215{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000216 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000217 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000218 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000219 /* Jump through some hoops for Alpha OSF/1 */
220 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000221#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
222 return (long) threadid;
223#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000224 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000225#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000226}
227
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228void
229PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000230{
Amaury Forgeot d'Arcadfc80b2010-02-23 23:19:39 +0000231 dprintf(("PyThread_exit_thread called\n"));
232 if (!initialized) {
233 exit(0);
234 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000235}
236
Martin v. Löwiscc898662002-03-17 09:53:51 +0000237#ifdef USE_SEMAPHORES
238
239/*
240 * Lock support.
241 */
242
243PyThread_type_lock
244PyThread_allocate_lock(void)
245{
246 sem_t *lock;
247 int status, error = 0;
248
249 dprintf(("PyThread_allocate_lock called\n"));
250 if (!initialized)
251 PyThread_init_thread();
252
253 lock = (sem_t *)malloc(sizeof(sem_t));
254
255 if (lock) {
256 status = sem_init(lock,0,1);
257 CHECK_STATUS("sem_init");
258
259 if (error) {
260 free((void *)lock);
261 lock = NULL;
262 }
263 }
264
265 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
266 return (PyThread_type_lock)lock;
267}
268
269void
270PyThread_free_lock(PyThread_type_lock lock)
271{
272 sem_t *thelock = (sem_t *)lock;
273 int status, error = 0;
274
275 dprintf(("PyThread_free_lock(%p) called\n", lock));
276
277 if (!thelock)
278 return;
279
280 status = sem_destroy(thelock);
281 CHECK_STATUS("sem_destroy");
282
283 free((void *)thelock);
284}
285
286/*
287 * As of February 2002, Cygwin thread implementations mistakenly report error
288 * codes in the return value of the sem_ calls (like the pthread_ functions).
289 * Correct implementations return -1 and put the code in errno. This supports
290 * either.
291 */
292static int
293fix_status(int status)
294{
295 return (status == -1) ? errno : status;
296}
297
298int
299PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
300{
301 int success;
302 sem_t *thelock = (sem_t *)lock;
303 int status, error = 0;
304
305 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
306
307 do {
308 if (waitflag)
309 status = fix_status(sem_wait(thelock));
310 else
311 status = fix_status(sem_trywait(thelock));
312 } while (status == EINTR); /* Retry if interrupted by a signal */
313
314 if (waitflag) {
315 CHECK_STATUS("sem_wait");
316 } else if (status != EAGAIN) {
317 CHECK_STATUS("sem_trywait");
318 }
319
320 success = (status == 0) ? 1 : 0;
321
322 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
323 return success;
324}
325
326void
327PyThread_release_lock(PyThread_type_lock lock)
328{
329 sem_t *thelock = (sem_t *)lock;
330 int status, error = 0;
331
332 dprintf(("PyThread_release_lock(%p) called\n", lock));
333
334 status = sem_post(thelock);
335 CHECK_STATUS("sem_post");
336}
337
338#else /* USE_SEMAPHORES */
339
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000340/*
341 * Lock support.
342 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000343PyThread_type_lock
344PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000345{
346 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000347 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000348
Guido van Rossum65d5b571998-12-21 19:32:43 +0000349 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000350 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000351 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000352
353 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000354 if (lock) {
Armin Rigoa6eb56c2005-09-20 18:07:47 +0000355 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000356 lock->locked = 0;
357
358 status = pthread_mutex_init(&lock->mut,
359 pthread_mutexattr_default);
360 CHECK_STATUS("pthread_mutex_init");
361
362 status = pthread_cond_init(&lock->lock_released,
363 pthread_condattr_default);
364 CHECK_STATUS("pthread_cond_init");
365
366 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000367 free((void *)lock);
368 lock = 0;
369 }
370 }
371
Fred Drakea44d3532000-06-30 15:01:00 +0000372 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000373 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000374}
375
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376void
377PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000378{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000379 pthread_lock *thelock = (pthread_lock *)lock;
380 int status, error = 0;
381
Fred Drakea44d3532000-06-30 15:01:00 +0000382 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000383
384 status = pthread_mutex_destroy( &thelock->mut );
385 CHECK_STATUS("pthread_mutex_destroy");
386
387 status = pthread_cond_destroy( &thelock->lock_released );
388 CHECK_STATUS("pthread_cond_destroy");
389
390 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000391}
392
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393int
394PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000395{
396 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000397 pthread_lock *thelock = (pthread_lock *)lock;
398 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000399
Fred Drakea44d3532000-06-30 15:01:00 +0000400 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000401
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000402 status = pthread_mutex_lock( &thelock->mut );
403 CHECK_STATUS("pthread_mutex_lock[1]");
404 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000405
406 if ( !success && waitflag ) {
407 /* continue trying until we get the lock */
408
409 /* mut must be locked by me -- part of the condition
410 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000411 while ( thelock->locked ) {
412 status = pthread_cond_wait(&thelock->lock_released,
413 &thelock->mut);
414 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000415 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000416 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000417 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000418 if (success) thelock->locked = 1;
419 status = pthread_mutex_unlock( &thelock->mut );
420 CHECK_STATUS("pthread_mutex_unlock[1]");
421
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000422 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000423 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000424 return success;
425}
426
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000427void
428PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000429{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000430 pthread_lock *thelock = (pthread_lock *)lock;
431 int status, error = 0;
432
Fred Drakea44d3532000-06-30 15:01:00 +0000433 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000434
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000435 status = pthread_mutex_lock( &thelock->mut );
436 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000438 thelock->locked = 0;
439
440 status = pthread_mutex_unlock( &thelock->mut );
441 CHECK_STATUS("pthread_mutex_unlock[3]");
442
443 /* wake up someone (anyone, if any) waiting on the lock */
444 status = pthread_cond_signal( &thelock->lock_released );
445 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000446}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000447
448#endif /* USE_SEMAPHORES */
Andrew MacIntyre92913322006-06-13 15:04:24 +0000449
450/* set the thread stack size.
451 * Return 0 if size is valid, -1 if size is invalid,
452 * -2 if setting stack size is not supported.
453 */
454static int
455_pythread_pthread_set_stacksize(size_t size)
456{
457#if defined(THREAD_STACK_SIZE)
458 pthread_attr_t attrs;
459 size_t tss_min;
460 int rc = 0;
461#endif
462
463 /* set to default */
464 if (size == 0) {
465 _pythread_stacksize = 0;
466 return 0;
467 }
468
469#if defined(THREAD_STACK_SIZE)
470#if defined(PTHREAD_STACK_MIN)
471 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
472 : THREAD_STACK_MIN;
473#else
474 tss_min = THREAD_STACK_MIN;
475#endif
476 if (size >= tss_min) {
477 /* validate stack size by setting thread attribute */
478 if (pthread_attr_init(&attrs) == 0) {
479 rc = pthread_attr_setstacksize(&attrs, size);
480 pthread_attr_destroy(&attrs);
481 if (rc == 0) {
482 _pythread_stacksize = size;
483 return 0;
484 }
485 }
486 }
487 return -1;
488#else
489 return -2;
490#endif
491}
492
493#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)