blob: 6910ccba1eb2692e2c28700612e6934b5bad35d5 [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
Jack Jansenc51395d2001-08-29 15:24:53 +000051#ifdef USE_GUSI
52/* The Macintosh GUSI I/O library sets the stackspace to
53** 20KB, much too low. We up it to 64K.
54*/
55#define THREAD_STACK_SIZE 0x10000
56#endif
57
Guido van Rossumd6353e21997-05-13 17:51:13 +000058
59/* set default attribute object for different versions */
60
61#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
62# define pthread_attr_default pthread_attr_default
63# define pthread_mutexattr_default pthread_mutexattr_default
64# define pthread_condattr_default pthread_condattr_default
Guido van Rossum64f91051997-05-22 20:41:59 +000065#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Guido van Rossumd6353e21997-05-13 17:51:13 +000066# define pthread_attr_default ((pthread_attr_t *)NULL)
67# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
68# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000069#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000070
Guido van Rossumd6353e21997-05-13 17:51:13 +000071
Guido van Rossumb98b1b31994-05-11 08:42:04 +000072/* A pthread mutex isn't sufficient to model the Python lock type
73 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
74 * following are undefined:
75 * -> a thread tries to lock a mutex it already has locked
76 * -> a thread tries to unlock a mutex locked by a different thread
77 * pthread mutexes are designed for serializing threads over short pieces
78 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000079 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000080 *
81 * The pthread_lock struct implements a Python lock as a "locked?" bit
82 * and a <condition, mutex> pair. In general, if the bit can be acquired
83 * instantly, it is, else the pair is used to block the thread until the
84 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000085 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +000086
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000087typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +000088 char locked; /* 0=unlocked, 1=locked */
89 /* a <cond, mutex> pair to handle an acquire of a locked lock */
90 pthread_cond_t lock_released;
91 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000092} pthread_lock;
93
Guido van Rossum9e46e561998-10-07 16:39:47 +000094#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +000095
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000096/*
97 * Initialization.
98 */
Guido van Rossum9e46e561998-10-07 16:39:47 +000099
100#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000101static
102void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000103{
104}
105
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106static void
107PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000108{
109 /* DO AN INIT BY STARTING THE THREAD */
110 static int dummy = 0;
111 pthread_t thread1;
112 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
113 pthread_join(thread1, NULL);
114}
115
116#else /* !_HAVE_BSDI */
117
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000118static void
119PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000120{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000121#if defined(_AIX) && defined(__GNUC__)
122 pthread_init();
123#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000124}
125
Guido van Rossum9e46e561998-10-07 16:39:47 +0000126#endif /* !_HAVE_BSDI */
127
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000128/*
129 * Thread support.
130 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000131
132
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133int
134PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000135{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000136 pthread_t th;
Guido van Rossume944da81994-05-23 12:43:41 +0000137 int success;
Jack Jansenc51395d2001-08-29 15:24:53 +0000138#ifdef THREAD_STACK_SIZE
139 pthread_attr_t attrs;
140#endif
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000142 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000143 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000144
Jack Jansenc51395d2001-08-29 15:24:53 +0000145#ifdef THREAD_STACK_SIZE
146 pthread_attr_init(&attrs);
147 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
148#endif
Guido van Rossumd6353e21997-05-13 17:51:13 +0000149 success = pthread_create(&th,
150#if defined(PY_PTHREAD_D4)
151 pthread_attr_default,
152 (pthread_startroutine_t)func,
153 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000154#elif defined(PY_PTHREAD_D6)
155 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000156 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000157 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000158#elif defined(PY_PTHREAD_D7)
159 pthread_attr_default,
160 func,
161 arg
162#elif defined(PY_PTHREAD_STD)
Jack Jansenc51395d2001-08-29 15:24:53 +0000163#ifdef THREAD_STACK_SIZE
164 &attrs,
165#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000166 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000167#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000168 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000169 (void *)arg
170#endif
171 );
Jack Jansenc51395d2001-08-29 15:24:53 +0000172#ifdef THREAD_STACK_SIZE
173 pthread_attr_destroy(&attrs);
174#endif
Guido van Rossum701f25e1999-03-15 20:27:53 +0000175 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000176#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000177 pthread_detach(&th);
178#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000179 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000180#endif
181 }
Guido van Rossum701f25e1999-03-15 20:27:53 +0000182 return success != 0 ? 0 : 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000183}
184
Trent Mick635f6fb2000-08-23 21:33:05 +0000185/* XXX This implementation is considered (to quote Tim Peters) "inherently
186 hosed" because:
187 - It does not guanrantee the promise that a non-zero integer is returned.
188 - The cast to long is inherently unsafe.
189 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
190 latter return statement (for Alpha OSF/1) are any longer necessary.
191*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192long
193PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000194{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000195 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000196 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000197 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000198 /* Jump through some hoops for Alpha OSF/1 */
199 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000200#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
201 return (long) threadid;
202#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000203 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000204#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000205}
206
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207static void
208do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000209{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000210 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000211 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000212 if (no_cleanup)
213 _exit(0);
214 else
215 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000216 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000217}
218
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219void
220PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000221{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000222 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000223}
224
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000225void
226PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000227{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000228 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000229}
230
231#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232static void
233do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000234{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000236 if (!initialized)
237 if (no_cleanup)
238 _exit(status);
239 else
240 exit(status);
241}
242
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243void
244PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000245{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000247}
248
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249void
250PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000251{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000252 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000253}
254#endif /* NO_EXIT_PROG */
255
256/*
257 * Lock support.
258 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyThread_type_lock
260PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000261{
262 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000263 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000264
Guido van Rossum65d5b571998-12-21 19:32:43 +0000265 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000266 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000268
269 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000270 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000271 if (lock) {
272 lock->locked = 0;
273
274 status = pthread_mutex_init(&lock->mut,
275 pthread_mutexattr_default);
276 CHECK_STATUS("pthread_mutex_init");
277
278 status = pthread_cond_init(&lock->lock_released,
279 pthread_condattr_default);
280 CHECK_STATUS("pthread_cond_init");
281
282 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000283 free((void *)lock);
284 lock = 0;
285 }
286 }
287
Fred Drakea44d3532000-06-30 15:01:00 +0000288 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000289 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000290}
291
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000292void
293PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000294{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000295 pthread_lock *thelock = (pthread_lock *)lock;
296 int status, error = 0;
297
Fred Drakea44d3532000-06-30 15:01:00 +0000298 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000299
300 status = pthread_mutex_destroy( &thelock->mut );
301 CHECK_STATUS("pthread_mutex_destroy");
302
303 status = pthread_cond_destroy( &thelock->lock_released );
304 CHECK_STATUS("pthread_cond_destroy");
305
306 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000307}
308
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000309int
310PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000311{
312 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000313 pthread_lock *thelock = (pthread_lock *)lock;
314 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000315
Fred Drakea44d3532000-06-30 15:01:00 +0000316 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000317
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000318 status = pthread_mutex_lock( &thelock->mut );
319 CHECK_STATUS("pthread_mutex_lock[1]");
320 success = thelock->locked == 0;
321 if (success) thelock->locked = 1;
322 status = pthread_mutex_unlock( &thelock->mut );
323 CHECK_STATUS("pthread_mutex_unlock[1]");
324
325 if ( !success && waitflag ) {
326 /* continue trying until we get the lock */
327
328 /* mut must be locked by me -- part of the condition
329 * protocol */
330 status = pthread_mutex_lock( &thelock->mut );
331 CHECK_STATUS("pthread_mutex_lock[2]");
332 while ( thelock->locked ) {
333 status = pthread_cond_wait(&thelock->lock_released,
334 &thelock->mut);
335 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000336 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000337 thelock->locked = 1;
338 status = pthread_mutex_unlock( &thelock->mut );
339 CHECK_STATUS("pthread_mutex_unlock[2]");
340 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000341 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000342 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000343 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000344 return success;
345}
346
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347void
348PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000349{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000350 pthread_lock *thelock = (pthread_lock *)lock;
351 int status, error = 0;
352
Fred Drakea44d3532000-06-30 15:01:00 +0000353 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000354
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000355 status = pthread_mutex_lock( &thelock->mut );
356 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000357
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000358 thelock->locked = 0;
359
360 status = pthread_mutex_unlock( &thelock->mut );
361 CHECK_STATUS("pthread_mutex_unlock[3]");
362
363 /* wake up someone (anyone, if any) waiting on the lock */
364 status = pthread_cond_signal( &thelock->lock_released );
365 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000366}
367
368/*
369 * Semaphore support.
370 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000371
372struct semaphore {
373 pthread_mutex_t mutex;
374 pthread_cond_t cond;
375 int value;
376};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000377
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378PyThread_type_sema
379PyThread_allocate_sema(int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000380{
Guido van Rossum7af81301997-01-17 21:06:41 +0000381 struct semaphore *sema;
382 int status, error = 0;
383
Guido van Rossum65d5b571998-12-21 19:32:43 +0000384 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000385 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000386 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000387
Guido van Rossum7af81301997-01-17 21:06:41 +0000388 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
389 if (sema != NULL) {
390 sema->value = value;
391 status = pthread_mutex_init(&sema->mutex,
392 pthread_mutexattr_default);
393 CHECK_STATUS("pthread_mutex_init");
394 status = pthread_cond_init(&sema->cond,
395 pthread_condattr_default);
396 CHECK_STATUS("pthread_cond_init");
397 if (error) {
398 free((void *) sema);
399 sema = NULL;
400 }
401 }
Fred Drakea44d3532000-06-30 15:01:00 +0000402 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000403 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000404}
405
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000406void
407PyThread_free_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000408{
Guido van Rossum7af81301997-01-17 21:06:41 +0000409 int status, error = 0;
410 struct semaphore *thesema = (struct semaphore *) sema;
411
Fred Drakea44d3532000-06-30 15:01:00 +0000412 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000413 status = pthread_cond_destroy(&thesema->cond);
414 CHECK_STATUS("pthread_cond_destroy");
415 status = pthread_mutex_destroy(&thesema->mutex);
416 CHECK_STATUS("pthread_mutex_destroy");
417 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000418}
419
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420int
421PyThread_down_sema(PyThread_type_sema sema, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000422{
Guido van Rossum7af81301997-01-17 21:06:41 +0000423 int status, error = 0, success;
424 struct semaphore *thesema = (struct semaphore *) sema;
425
Fred Drakea44d3532000-06-30 15:01:00 +0000426 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000427 status = pthread_mutex_lock(&thesema->mutex);
428 CHECK_STATUS("pthread_mutex_lock");
429 if (waitflag) {
430 while (!error && thesema->value <= 0) {
431 status = pthread_cond_wait(&thesema->cond,
432 &thesema->mutex);
433 CHECK_STATUS("pthread_cond_wait");
434 }
435 }
436 if (error)
437 success = 0;
438 else if (thesema->value > 0) {
439 thesema->value--;
440 success = 1;
441 }
442 else
443 success = 0;
444 status = pthread_mutex_unlock(&thesema->mutex);
445 CHECK_STATUS("pthread_mutex_unlock");
Fred Drakea44d3532000-06-30 15:01:00 +0000446 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000447 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000448}
449
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000450void
451PyThread_up_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000452{
Guido van Rossum7af81301997-01-17 21:06:41 +0000453 int status, error = 0;
454 struct semaphore *thesema = (struct semaphore *) sema;
455
Fred Drakea44d3532000-06-30 15:01:00 +0000456 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000457 status = pthread_mutex_lock(&thesema->mutex);
458 CHECK_STATUS("pthread_mutex_lock");
459 thesema->value++;
460 status = pthread_cond_signal(&thesema->cond);
461 CHECK_STATUS("pthread_cond_signal");
462 status = pthread_mutex_unlock(&thesema->mutex);
463 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000464}