blob: 2ef46c098971f3691c4d8b47bbd6cd83efe6afd3 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00009******************************************************************/
10
Guido van Rossum66020991996-06-11 18:32:18 +000011/* Posix threads interface */
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000012
Guido van Rossum66020991996-06-11 18:32:18 +000013#include <stdlib.h>
Guido van Rossum9e46e561998-10-07 16:39:47 +000014#include <string.h>
Guido van Rossum66020991996-06-11 18:32:18 +000015#include <pthread.h>
16
Guido van Rossum1a623111996-08-08 18:53:41 +000017
Guido van Rossumd6353e21997-05-13 17:51:13 +000018/* try to determine what version of the Pthread Standard is installed.
19 * this is important, since all sorts of parameter types changed from
20 * draft to draft and there are several (incompatible) drafts in
21 * common use. these macros are a start, at least.
22 * 12 May 1997 -- david arnold <davida@pobox.com>
23 */
24
25#if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
26/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
27# define PY_PTHREAD_D4
28
29#elif defined(__osf__) && defined (__alpha)
30/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
31# if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
32# define PY_PTHREAD_D4
33# else
34# define PY_PTHREAD_STD
35# endif
36
37#elif defined(_AIX)
Guido van Rossum1a623111996-08-08 18:53:41 +000038/* SCHED_BG_NP is defined if using AIX DCE pthreads
39 * but it is unsupported by AIX 4 pthreads. Default
40 * attributes for AIX 4 pthreads equal to NULL. For
41 * AIX DCE pthreads they should be left unchanged.
42 */
Guido van Rossumd6353e21997-05-13 17:51:13 +000043# if !defined(SCHED_BG_NP)
44# define PY_PTHREAD_STD
45# else
46# define PY_PTHREAD_D7
47# endif
48
Guido van Rossum64f91051997-05-22 20:41:59 +000049#elif defined(__DGUX)
50# define PY_PTHREAD_D6
Guido van Rossum46ff1901997-06-02 22:25:45 +000051
Guido van Rossum532246e1998-05-14 21:01:27 +000052#elif defined(__hpux) && defined(_DECTHREADS_)
Guido van Rossum89df70b1998-05-07 13:28:23 +000053# define PY_PTHREAD_D4
54
Guido van Rossum46ff1901997-06-02 22:25:45 +000055#else /* Default case */
56# define PY_PTHREAD_STD
57
Guido van Rossum1a623111996-08-08 18:53:41 +000058#endif
59
Guido van Rossumd6353e21997-05-13 17:51:13 +000060
61/* set default attribute object for different versions */
62
63#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
64# define pthread_attr_default pthread_attr_default
65# define pthread_mutexattr_default pthread_mutexattr_default
66# define pthread_condattr_default pthread_condattr_default
Guido van Rossum64f91051997-05-22 20:41:59 +000067#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Guido van Rossumd6353e21997-05-13 17:51:13 +000068# define pthread_attr_default ((pthread_attr_t *)NULL)
69# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
70# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000071#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000072
Guido van Rossumd6353e21997-05-13 17:51:13 +000073
Guido van Rossumb98b1b31994-05-11 08:42:04 +000074/* A pthread mutex isn't sufficient to model the Python lock type
75 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
76 * following are undefined:
77 * -> a thread tries to lock a mutex it already has locked
78 * -> a thread tries to unlock a mutex locked by a different thread
79 * pthread mutexes are designed for serializing threads over short pieces
80 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000081 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000082 *
83 * The pthread_lock struct implements a Python lock as a "locked?" bit
84 * and a <condition, mutex> pair. In general, if the bit can be acquired
85 * instantly, it is, else the pair is used to block the thread until the
86 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000087 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +000088
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +000090 char locked; /* 0=unlocked, 1=locked */
91 /* a <cond, mutex> pair to handle an acquire of a locked lock */
92 pthread_cond_t lock_released;
93 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000094} pthread_lock;
95
Guido van Rossum9e46e561998-10-07 16:39:47 +000096#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +000097
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000098/*
99 * Initialization.
100 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000101
102#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000103static
104void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000105{
106}
107
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000108static void
109PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000110{
111 /* DO AN INIT BY STARTING THE THREAD */
112 static int dummy = 0;
113 pthread_t thread1;
114 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
115 pthread_join(thread1, NULL);
116}
117
118#else /* !_HAVE_BSDI */
119
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000120static void
121PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000122{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000123#if defined(_AIX) && defined(__GNUC__)
124 pthread_init();
125#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126}
127
Guido van Rossum9e46e561998-10-07 16:39:47 +0000128#endif /* !_HAVE_BSDI */
129
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000130/*
131 * Thread support.
132 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000133
134
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135int
136PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000137{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138 pthread_t th;
Guido van Rossume944da81994-05-23 12:43:41 +0000139 int success;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000140 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000141 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000142 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000143
144 success = pthread_create(&th,
145#if defined(PY_PTHREAD_D4)
146 pthread_attr_default,
147 (pthread_startroutine_t)func,
148 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000149#elif defined(PY_PTHREAD_D6)
150 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000152 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000153#elif defined(PY_PTHREAD_D7)
154 pthread_attr_default,
155 func,
156 arg
157#elif defined(PY_PTHREAD_STD)
158 (pthread_attr_t*)NULL,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000159 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000160 (void *)arg
161#endif
162 );
163
Guido van Rossum701f25e1999-03-15 20:27:53 +0000164 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000165#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000166 pthread_detach(&th);
167#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000168 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000169#endif
170 }
Guido van Rossum701f25e1999-03-15 20:27:53 +0000171 return success != 0 ? 0 : 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000172}
173
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000174long
175PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000176{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000177 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000178 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000179 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000180 /* Jump through some hoops for Alpha OSF/1 */
181 threadid = pthread_self();
182 return (long) *(long *) &threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000183}
184
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000185static void
186do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000187{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000188 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000189 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000190 if (no_cleanup)
191 _exit(0);
192 else
193 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000194 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000195}
196
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000197void
198PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000199{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000200 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000201}
202
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203void
204PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000205{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000206 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000207}
208
209#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210static void
211do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000212{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000213 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000214 if (!initialized)
215 if (no_cleanup)
216 _exit(status);
217 else
218 exit(status);
219}
220
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221void
222PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000223{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000225}
226
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227void
228PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000229{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000231}
232#endif /* NO_EXIT_PROG */
233
234/*
235 * Lock support.
236 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyThread_type_lock
238PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000239{
240 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000241 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242
Guido van Rossum65d5b571998-12-21 19:32:43 +0000243 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000244 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000246
247 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000248 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000249 if (lock) {
250 lock->locked = 0;
251
252 status = pthread_mutex_init(&lock->mut,
253 pthread_mutexattr_default);
254 CHECK_STATUS("pthread_mutex_init");
255
256 status = pthread_cond_init(&lock->lock_released,
257 pthread_condattr_default);
258 CHECK_STATUS("pthread_cond_init");
259
260 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000261 free((void *)lock);
262 lock = 0;
263 }
264 }
265
Fred Drakea44d3532000-06-30 15:01:00 +0000266 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000268}
269
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000270void
271PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000272{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000273 pthread_lock *thelock = (pthread_lock *)lock;
274 int status, error = 0;
275
Fred Drakea44d3532000-06-30 15:01:00 +0000276 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000277
278 status = pthread_mutex_destroy( &thelock->mut );
279 CHECK_STATUS("pthread_mutex_destroy");
280
281 status = pthread_cond_destroy( &thelock->lock_released );
282 CHECK_STATUS("pthread_cond_destroy");
283
284 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000285}
286
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287int
288PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000289{
290 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000291 pthread_lock *thelock = (pthread_lock *)lock;
292 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000293
Fred Drakea44d3532000-06-30 15:01:00 +0000294 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000295
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000296 status = pthread_mutex_lock( &thelock->mut );
297 CHECK_STATUS("pthread_mutex_lock[1]");
298 success = thelock->locked == 0;
299 if (success) thelock->locked = 1;
300 status = pthread_mutex_unlock( &thelock->mut );
301 CHECK_STATUS("pthread_mutex_unlock[1]");
302
303 if ( !success && waitflag ) {
304 /* continue trying until we get the lock */
305
306 /* mut must be locked by me -- part of the condition
307 * protocol */
308 status = pthread_mutex_lock( &thelock->mut );
309 CHECK_STATUS("pthread_mutex_lock[2]");
310 while ( thelock->locked ) {
311 status = pthread_cond_wait(&thelock->lock_released,
312 &thelock->mut);
313 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000314 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000315 thelock->locked = 1;
316 status = pthread_mutex_unlock( &thelock->mut );
317 CHECK_STATUS("pthread_mutex_unlock[2]");
318 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000319 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000320 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000321 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000322 return success;
323}
324
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325void
326PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000327{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000328 pthread_lock *thelock = (pthread_lock *)lock;
329 int status, error = 0;
330
Fred Drakea44d3532000-06-30 15:01:00 +0000331 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000332
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000333 status = pthread_mutex_lock( &thelock->mut );
334 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000335
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000336 thelock->locked = 0;
337
338 status = pthread_mutex_unlock( &thelock->mut );
339 CHECK_STATUS("pthread_mutex_unlock[3]");
340
341 /* wake up someone (anyone, if any) waiting on the lock */
342 status = pthread_cond_signal( &thelock->lock_released );
343 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000344}
345
346/*
347 * Semaphore support.
348 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000349
350struct semaphore {
351 pthread_mutex_t mutex;
352 pthread_cond_t cond;
353 int value;
354};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000355
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356PyThread_type_sema
357PyThread_allocate_sema(int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000358{
Guido van Rossum7af81301997-01-17 21:06:41 +0000359 struct semaphore *sema;
360 int status, error = 0;
361
Guido van Rossum65d5b571998-12-21 19:32:43 +0000362 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000363 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000364 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000365
Guido van Rossum7af81301997-01-17 21:06:41 +0000366 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
367 if (sema != NULL) {
368 sema->value = value;
369 status = pthread_mutex_init(&sema->mutex,
370 pthread_mutexattr_default);
371 CHECK_STATUS("pthread_mutex_init");
372 status = pthread_cond_init(&sema->cond,
373 pthread_condattr_default);
374 CHECK_STATUS("pthread_cond_init");
375 if (error) {
376 free((void *) sema);
377 sema = NULL;
378 }
379 }
Fred Drakea44d3532000-06-30 15:01:00 +0000380 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000381 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000382}
383
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384void
385PyThread_free_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000386{
Guido van Rossum7af81301997-01-17 21:06:41 +0000387 int status, error = 0;
388 struct semaphore *thesema = (struct semaphore *) sema;
389
Fred Drakea44d3532000-06-30 15:01:00 +0000390 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000391 status = pthread_cond_destroy(&thesema->cond);
392 CHECK_STATUS("pthread_cond_destroy");
393 status = pthread_mutex_destroy(&thesema->mutex);
394 CHECK_STATUS("pthread_mutex_destroy");
395 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000396}
397
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398int
399PyThread_down_sema(PyThread_type_sema sema, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000400{
Guido van Rossum7af81301997-01-17 21:06:41 +0000401 int status, error = 0, success;
402 struct semaphore *thesema = (struct semaphore *) sema;
403
Fred Drakea44d3532000-06-30 15:01:00 +0000404 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000405 status = pthread_mutex_lock(&thesema->mutex);
406 CHECK_STATUS("pthread_mutex_lock");
407 if (waitflag) {
408 while (!error && thesema->value <= 0) {
409 status = pthread_cond_wait(&thesema->cond,
410 &thesema->mutex);
411 CHECK_STATUS("pthread_cond_wait");
412 }
413 }
414 if (error)
415 success = 0;
416 else if (thesema->value > 0) {
417 thesema->value--;
418 success = 1;
419 }
420 else
421 success = 0;
422 status = pthread_mutex_unlock(&thesema->mutex);
423 CHECK_STATUS("pthread_mutex_unlock");
Fred Drakea44d3532000-06-30 15:01:00 +0000424 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000425 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000426}
427
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000428void
429PyThread_up_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000430{
Guido van Rossum7af81301997-01-17 21:06:41 +0000431 int status, error = 0;
432 struct semaphore *thesema = (struct semaphore *) sema;
433
Fred Drakea44d3532000-06-30 15:01:00 +0000434 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000435 status = pthread_mutex_lock(&thesema->mutex);
436 CHECK_STATUS("pthread_mutex_lock");
437 thesema->value++;
438 status = pthread_cond_signal(&thesema->cond);
439 CHECK_STATUS("pthread_cond_signal");
440 status = pthread_mutex_unlock(&thesema->mutex);
441 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000442}