blob: db3115ad2eca0fb243bc1db3671f3b2531bc2024 [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>
Guido van Rossum66020991996-06-11 18:32:18 +00006#include <pthread.h>
7
Guido van Rossum1a623111996-08-08 18:53:41 +00008
Guido van Rossumd6353e21997-05-13 17:51:13 +00009/* try to determine what version of the Pthread Standard is installed.
10 * this is important, since all sorts of parameter types changed from
11 * draft to draft and there are several (incompatible) drafts in
12 * common use. these macros are a start, at least.
13 * 12 May 1997 -- david arnold <davida@pobox.com>
14 */
15
16#if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
17/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
18# define PY_PTHREAD_D4
19
20#elif defined(__osf__) && defined (__alpha)
21/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
22# if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
23# define PY_PTHREAD_D4
24# else
25# define PY_PTHREAD_STD
26# endif
27
28#elif defined(_AIX)
Guido van Rossum1a623111996-08-08 18:53:41 +000029/* SCHED_BG_NP is defined if using AIX DCE pthreads
30 * but it is unsupported by AIX 4 pthreads. Default
31 * attributes for AIX 4 pthreads equal to NULL. For
32 * AIX DCE pthreads they should be left unchanged.
33 */
Guido van Rossumd6353e21997-05-13 17:51:13 +000034# if !defined(SCHED_BG_NP)
35# define PY_PTHREAD_STD
36# else
37# define PY_PTHREAD_D7
38# endif
39
Guido van Rossum64f91051997-05-22 20:41:59 +000040#elif defined(__DGUX)
41# define PY_PTHREAD_D6
Guido van Rossum46ff1901997-06-02 22:25:45 +000042
Guido van Rossum532246e1998-05-14 21:01:27 +000043#elif defined(__hpux) && defined(_DECTHREADS_)
Guido van Rossum89df70b1998-05-07 13:28:23 +000044# define PY_PTHREAD_D4
45
Guido van Rossum46ff1901997-06-02 22:25:45 +000046#else /* Default case */
47# define PY_PTHREAD_STD
48
Guido van Rossum1a623111996-08-08 18:53:41 +000049#endif
50
Guido van Rossumd6353e21997-05-13 17:51:13 +000051
52/* set default attribute object for different versions */
53
54#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
55# define pthread_attr_default pthread_attr_default
56# define pthread_mutexattr_default pthread_mutexattr_default
57# define pthread_condattr_default pthread_condattr_default
Guido van Rossum64f91051997-05-22 20:41:59 +000058#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Guido van Rossumd6353e21997-05-13 17:51:13 +000059# define pthread_attr_default ((pthread_attr_t *)NULL)
60# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
61# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000062#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000063
Guido van Rossumd6353e21997-05-13 17:51:13 +000064
Guido van Rossumb98b1b31994-05-11 08:42:04 +000065/* A pthread mutex isn't sufficient to model the Python lock type
66 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
67 * following are undefined:
68 * -> a thread tries to lock a mutex it already has locked
69 * -> a thread tries to unlock a mutex locked by a different thread
70 * pthread mutexes are designed for serializing threads over short pieces
71 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000072 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000073 *
74 * The pthread_lock struct implements a Python lock as a "locked?" bit
75 * and a <condition, mutex> pair. In general, if the bit can be acquired
76 * instantly, it is, else the pair is used to block the thread until the
77 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000078 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +000079
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000080typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +000081 char locked; /* 0=unlocked, 1=locked */
82 /* a <cond, mutex> pair to handle an acquire of a locked lock */
83 pthread_cond_t lock_released;
84 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000085} pthread_lock;
86
Guido van Rossum9e46e561998-10-07 16:39:47 +000087#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +000088
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089/*
90 * Initialization.
91 */
Guido van Rossum9e46e561998-10-07 16:39:47 +000092
93#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000094static
95void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +000096{
97}
98
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099static void
100PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000101{
102 /* DO AN INIT BY STARTING THE THREAD */
103 static int dummy = 0;
104 pthread_t thread1;
105 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
106 pthread_join(thread1, NULL);
107}
108
109#else /* !_HAVE_BSDI */
110
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111static void
112PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000114#if defined(_AIX) && defined(__GNUC__)
115 pthread_init();
116#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000117}
118
Guido van Rossum9e46e561998-10-07 16:39:47 +0000119#endif /* !_HAVE_BSDI */
120
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000121/*
122 * Thread support.
123 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000124
125
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000126int
127PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000128{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000129 pthread_t th;
Guido van Rossume944da81994-05-23 12:43:41 +0000130 int success;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000131 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000132 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000133 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000134
135 success = pthread_create(&th,
136#if defined(PY_PTHREAD_D4)
137 pthread_attr_default,
138 (pthread_startroutine_t)func,
139 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000140#elif defined(PY_PTHREAD_D6)
141 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000143 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000144#elif defined(PY_PTHREAD_D7)
145 pthread_attr_default,
146 func,
147 arg
148#elif defined(PY_PTHREAD_STD)
149 (pthread_attr_t*)NULL,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000150 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000151 (void *)arg
152#endif
153 );
154
Guido van Rossum701f25e1999-03-15 20:27:53 +0000155 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000156#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000157 pthread_detach(&th);
158#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000159 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000160#endif
161 }
Guido van Rossum701f25e1999-03-15 20:27:53 +0000162 return success != 0 ? 0 : 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000163}
164
Trent Mick635f6fb2000-08-23 21:33:05 +0000165/* XXX This implementation is considered (to quote Tim Peters) "inherently
166 hosed" because:
167 - It does not guanrantee the promise that a non-zero integer is returned.
168 - The cast to long is inherently unsafe.
169 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
170 latter return statement (for Alpha OSF/1) are any longer necessary.
171*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000172long
173PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000174{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000175 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000176 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000177 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000178 /* Jump through some hoops for Alpha OSF/1 */
179 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000180#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
181 return (long) threadid;
182#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000183 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000184#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000185}
186
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187static void
188do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000189{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000190 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000191 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000192 if (no_cleanup)
193 _exit(0);
194 else
195 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000196 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000197}
198
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000199void
200PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000201{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000202 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000203}
204
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205void
206PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000207{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000208 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000209}
210
211#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000212static void
213do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000214{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000215 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000216 if (!initialized)
217 if (no_cleanup)
218 _exit(status);
219 else
220 exit(status);
221}
222
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223void
224PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000225{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000226 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000227}
228
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229void
230PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000231{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000233}
234#endif /* NO_EXIT_PROG */
235
236/*
237 * Lock support.
238 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyThread_type_lock
240PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000241{
242 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000243 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000244
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000246 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000248
249 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000250 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000251 if (lock) {
252 lock->locked = 0;
253
254 status = pthread_mutex_init(&lock->mut,
255 pthread_mutexattr_default);
256 CHECK_STATUS("pthread_mutex_init");
257
258 status = pthread_cond_init(&lock->lock_released,
259 pthread_condattr_default);
260 CHECK_STATUS("pthread_cond_init");
261
262 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000263 free((void *)lock);
264 lock = 0;
265 }
266 }
267
Fred Drakea44d3532000-06-30 15:01:00 +0000268 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000270}
271
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272void
273PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000274{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000275 pthread_lock *thelock = (pthread_lock *)lock;
276 int status, error = 0;
277
Fred Drakea44d3532000-06-30 15:01:00 +0000278 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000279
280 status = pthread_mutex_destroy( &thelock->mut );
281 CHECK_STATUS("pthread_mutex_destroy");
282
283 status = pthread_cond_destroy( &thelock->lock_released );
284 CHECK_STATUS("pthread_cond_destroy");
285
286 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000287}
288
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289int
290PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000291{
292 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000293 pthread_lock *thelock = (pthread_lock *)lock;
294 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000295
Fred Drakea44d3532000-06-30 15:01:00 +0000296 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000297
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000298 status = pthread_mutex_lock( &thelock->mut );
299 CHECK_STATUS("pthread_mutex_lock[1]");
300 success = thelock->locked == 0;
301 if (success) thelock->locked = 1;
302 status = pthread_mutex_unlock( &thelock->mut );
303 CHECK_STATUS("pthread_mutex_unlock[1]");
304
305 if ( !success && waitflag ) {
306 /* continue trying until we get the lock */
307
308 /* mut must be locked by me -- part of the condition
309 * protocol */
310 status = pthread_mutex_lock( &thelock->mut );
311 CHECK_STATUS("pthread_mutex_lock[2]");
312 while ( thelock->locked ) {
313 status = pthread_cond_wait(&thelock->lock_released,
314 &thelock->mut);
315 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000316 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000317 thelock->locked = 1;
318 status = pthread_mutex_unlock( &thelock->mut );
319 CHECK_STATUS("pthread_mutex_unlock[2]");
320 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000321 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000322 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000323 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000324 return success;
325}
326
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327void
328PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000329{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000330 pthread_lock *thelock = (pthread_lock *)lock;
331 int status, error = 0;
332
Fred Drakea44d3532000-06-30 15:01:00 +0000333 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000334
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000335 status = pthread_mutex_lock( &thelock->mut );
336 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000337
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000338 thelock->locked = 0;
339
340 status = pthread_mutex_unlock( &thelock->mut );
341 CHECK_STATUS("pthread_mutex_unlock[3]");
342
343 /* wake up someone (anyone, if any) waiting on the lock */
344 status = pthread_cond_signal( &thelock->lock_released );
345 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000346}
347
348/*
349 * Semaphore support.
350 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000351
352struct semaphore {
353 pthread_mutex_t mutex;
354 pthread_cond_t cond;
355 int value;
356};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000357
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyThread_type_sema
359PyThread_allocate_sema(int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000360{
Guido van Rossum7af81301997-01-17 21:06:41 +0000361 struct semaphore *sema;
362 int status, error = 0;
363
Guido van Rossum65d5b571998-12-21 19:32:43 +0000364 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000365 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000366 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000367
Guido van Rossum7af81301997-01-17 21:06:41 +0000368 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
369 if (sema != NULL) {
370 sema->value = value;
371 status = pthread_mutex_init(&sema->mutex,
372 pthread_mutexattr_default);
373 CHECK_STATUS("pthread_mutex_init");
374 status = pthread_cond_init(&sema->cond,
375 pthread_condattr_default);
376 CHECK_STATUS("pthread_cond_init");
377 if (error) {
378 free((void *) sema);
379 sema = NULL;
380 }
381 }
Fred Drakea44d3532000-06-30 15:01:00 +0000382 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000383 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000384}
385
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386void
387PyThread_free_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000388{
Guido van Rossum7af81301997-01-17 21:06:41 +0000389 int status, error = 0;
390 struct semaphore *thesema = (struct semaphore *) sema;
391
Fred Drakea44d3532000-06-30 15:01:00 +0000392 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000393 status = pthread_cond_destroy(&thesema->cond);
394 CHECK_STATUS("pthread_cond_destroy");
395 status = pthread_mutex_destroy(&thesema->mutex);
396 CHECK_STATUS("pthread_mutex_destroy");
397 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000398}
399
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000400int
401PyThread_down_sema(PyThread_type_sema sema, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000402{
Guido van Rossum7af81301997-01-17 21:06:41 +0000403 int status, error = 0, success;
404 struct semaphore *thesema = (struct semaphore *) sema;
405
Fred Drakea44d3532000-06-30 15:01:00 +0000406 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000407 status = pthread_mutex_lock(&thesema->mutex);
408 CHECK_STATUS("pthread_mutex_lock");
409 if (waitflag) {
410 while (!error && thesema->value <= 0) {
411 status = pthread_cond_wait(&thesema->cond,
412 &thesema->mutex);
413 CHECK_STATUS("pthread_cond_wait");
414 }
415 }
416 if (error)
417 success = 0;
418 else if (thesema->value > 0) {
419 thesema->value--;
420 success = 1;
421 }
422 else
423 success = 0;
424 status = pthread_mutex_unlock(&thesema->mutex);
425 CHECK_STATUS("pthread_mutex_unlock");
Fred Drakea44d3532000-06-30 15:01:00 +0000426 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000427 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000428}
429
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430void
431PyThread_up_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000432{
Guido van Rossum7af81301997-01-17 21:06:41 +0000433 int status, error = 0;
434 struct semaphore *thesema = (struct semaphore *) sema;
435
Fred Drakea44d3532000-06-30 15:01:00 +0000436 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000437 status = pthread_mutex_lock(&thesema->mutex);
438 CHECK_STATUS("pthread_mutex_lock");
439 thesema->value++;
440 status = pthread_cond_signal(&thesema->cond);
441 CHECK_STATUS("pthread_cond_signal");
442 status = pthread_mutex_unlock(&thesema->mutex);
443 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000444}