blob: 2d6a4f80629e5db118abfd458ea00866bd19a139 [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
Trent Mick635f6fb2000-08-23 21:33:05 +0000174/* XXX This implementation is considered (to quote Tim Peters) "inherently
175 hosed" because:
176 - It does not guanrantee the promise that a non-zero integer is returned.
177 - The cast to long is inherently unsafe.
178 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
179 latter return statement (for Alpha OSF/1) are any longer necessary.
180*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000181long
182PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000183{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000184 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000185 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000186 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000187 /* Jump through some hoops for Alpha OSF/1 */
188 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000189#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
190 return (long) threadid;
191#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000192 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000193#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000194}
195
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196static void
197do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000198{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000199 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000200 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000201 if (no_cleanup)
202 _exit(0);
203 else
204 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000205 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000206}
207
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208void
209PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000210{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000211 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000212}
213
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000214void
215PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000216{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000217 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000218}
219
220#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221static void
222do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000223{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000225 if (!initialized)
226 if (no_cleanup)
227 _exit(status);
228 else
229 exit(status);
230}
231
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232void
233PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000234{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000236}
237
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238void
239PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000240{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242}
243#endif /* NO_EXIT_PROG */
244
245/*
246 * Lock support.
247 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyThread_type_lock
249PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000250{
251 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000252 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000253
Guido van Rossum65d5b571998-12-21 19:32:43 +0000254 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000255 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000257
258 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000259 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000260 if (lock) {
261 lock->locked = 0;
262
263 status = pthread_mutex_init(&lock->mut,
264 pthread_mutexattr_default);
265 CHECK_STATUS("pthread_mutex_init");
266
267 status = pthread_cond_init(&lock->lock_released,
268 pthread_condattr_default);
269 CHECK_STATUS("pthread_cond_init");
270
271 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000272 free((void *)lock);
273 lock = 0;
274 }
275 }
276
Fred Drakea44d3532000-06-30 15:01:00 +0000277 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000278 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000279}
280
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281void
282PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000283{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000284 pthread_lock *thelock = (pthread_lock *)lock;
285 int status, error = 0;
286
Fred Drakea44d3532000-06-30 15:01:00 +0000287 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000288
289 status = pthread_mutex_destroy( &thelock->mut );
290 CHECK_STATUS("pthread_mutex_destroy");
291
292 status = pthread_cond_destroy( &thelock->lock_released );
293 CHECK_STATUS("pthread_cond_destroy");
294
295 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000296}
297
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000298int
299PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000300{
301 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000302 pthread_lock *thelock = (pthread_lock *)lock;
303 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000304
Fred Drakea44d3532000-06-30 15:01:00 +0000305 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000306
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000307 status = pthread_mutex_lock( &thelock->mut );
308 CHECK_STATUS("pthread_mutex_lock[1]");
309 success = thelock->locked == 0;
310 if (success) thelock->locked = 1;
311 status = pthread_mutex_unlock( &thelock->mut );
312 CHECK_STATUS("pthread_mutex_unlock[1]");
313
314 if ( !success && waitflag ) {
315 /* continue trying until we get the lock */
316
317 /* mut must be locked by me -- part of the condition
318 * protocol */
319 status = pthread_mutex_lock( &thelock->mut );
320 CHECK_STATUS("pthread_mutex_lock[2]");
321 while ( thelock->locked ) {
322 status = pthread_cond_wait(&thelock->lock_released,
323 &thelock->mut);
324 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000325 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000326 thelock->locked = 1;
327 status = pthread_mutex_unlock( &thelock->mut );
328 CHECK_STATUS("pthread_mutex_unlock[2]");
329 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000330 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000331 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000332 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000333 return success;
334}
335
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336void
337PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000338{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000339 pthread_lock *thelock = (pthread_lock *)lock;
340 int status, error = 0;
341
Fred Drakea44d3532000-06-30 15:01:00 +0000342 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000343
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000344 status = pthread_mutex_lock( &thelock->mut );
345 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000346
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000347 thelock->locked = 0;
348
349 status = pthread_mutex_unlock( &thelock->mut );
350 CHECK_STATUS("pthread_mutex_unlock[3]");
351
352 /* wake up someone (anyone, if any) waiting on the lock */
353 status = pthread_cond_signal( &thelock->lock_released );
354 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000355}
356
357/*
358 * Semaphore support.
359 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000360
361struct semaphore {
362 pthread_mutex_t mutex;
363 pthread_cond_t cond;
364 int value;
365};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000366
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyThread_type_sema
368PyThread_allocate_sema(int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000369{
Guido van Rossum7af81301997-01-17 21:06:41 +0000370 struct semaphore *sema;
371 int status, error = 0;
372
Guido van Rossum65d5b571998-12-21 19:32:43 +0000373 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000374 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000375 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000376
Guido van Rossum7af81301997-01-17 21:06:41 +0000377 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
378 if (sema != NULL) {
379 sema->value = value;
380 status = pthread_mutex_init(&sema->mutex,
381 pthread_mutexattr_default);
382 CHECK_STATUS("pthread_mutex_init");
383 status = pthread_cond_init(&sema->cond,
384 pthread_condattr_default);
385 CHECK_STATUS("pthread_cond_init");
386 if (error) {
387 free((void *) sema);
388 sema = NULL;
389 }
390 }
Fred Drakea44d3532000-06-30 15:01:00 +0000391 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000392 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000393}
394
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395void
396PyThread_free_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000397{
Guido van Rossum7af81301997-01-17 21:06:41 +0000398 int status, error = 0;
399 struct semaphore *thesema = (struct semaphore *) sema;
400
Fred Drakea44d3532000-06-30 15:01:00 +0000401 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000402 status = pthread_cond_destroy(&thesema->cond);
403 CHECK_STATUS("pthread_cond_destroy");
404 status = pthread_mutex_destroy(&thesema->mutex);
405 CHECK_STATUS("pthread_mutex_destroy");
406 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000407}
408
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000409int
410PyThread_down_sema(PyThread_type_sema sema, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000411{
Guido van Rossum7af81301997-01-17 21:06:41 +0000412 int status, error = 0, success;
413 struct semaphore *thesema = (struct semaphore *) sema;
414
Fred Drakea44d3532000-06-30 15:01:00 +0000415 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000416 status = pthread_mutex_lock(&thesema->mutex);
417 CHECK_STATUS("pthread_mutex_lock");
418 if (waitflag) {
419 while (!error && thesema->value <= 0) {
420 status = pthread_cond_wait(&thesema->cond,
421 &thesema->mutex);
422 CHECK_STATUS("pthread_cond_wait");
423 }
424 }
425 if (error)
426 success = 0;
427 else if (thesema->value > 0) {
428 thesema->value--;
429 success = 1;
430 }
431 else
432 success = 0;
433 status = pthread_mutex_unlock(&thesema->mutex);
434 CHECK_STATUS("pthread_mutex_unlock");
Fred Drakea44d3532000-06-30 15:01:00 +0000435 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000436 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437}
438
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000439void
440PyThread_up_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000441{
Guido van Rossum7af81301997-01-17 21:06:41 +0000442 int status, error = 0;
443 struct semaphore *thesema = (struct semaphore *) sema;
444
Fred Drakea44d3532000-06-30 15:01:00 +0000445 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000446 status = pthread_mutex_lock(&thesema->mutex);
447 CHECK_STATUS("pthread_mutex_lock");
448 thesema->value++;
449 status = pthread_cond_signal(&thesema->cond);
450 CHECK_STATUS("pthread_cond_signal");
451 status = pthread_mutex_unlock(&thesema->mutex);
452 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000453}