blob: 26120d6886dac785964ade34c7a8f8b7560c0ca3 [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;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000138#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000139 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
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000145#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000146 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000147#endif
148#ifdef THREAD_STACK_SIZE
Jack Jansenc51395d2001-08-29 15:24:53 +0000149 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
150#endif
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000151#ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
152 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
153#endif
Guido van Rossumd6353e21997-05-13 17:51:13 +0000154 success = pthread_create(&th,
155#if defined(PY_PTHREAD_D4)
156 pthread_attr_default,
157 (pthread_startroutine_t)func,
158 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000159#elif defined(PY_PTHREAD_D6)
160 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000161 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000162 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000163#elif defined(PY_PTHREAD_D7)
164 pthread_attr_default,
165 func,
166 arg
167#elif defined(PY_PTHREAD_STD)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000168#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000169 &attrs,
170#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000171 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000172#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000173 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000174 (void *)arg
175#endif
176 );
Jack Jansenc51395d2001-08-29 15:24:53 +0000177#ifdef THREAD_STACK_SIZE
178 pthread_attr_destroy(&attrs);
179#endif
Guido van Rossum701f25e1999-03-15 20:27:53 +0000180 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000181#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000182 pthread_detach(&th);
183#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000184 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000185#endif
186 }
Guido van Rossum701f25e1999-03-15 20:27:53 +0000187 return success != 0 ? 0 : 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000188}
189
Trent Mick635f6fb2000-08-23 21:33:05 +0000190/* XXX This implementation is considered (to quote Tim Peters) "inherently
191 hosed" because:
192 - It does not guanrantee the promise that a non-zero integer is returned.
193 - The cast to long is inherently unsafe.
194 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
195 latter return statement (for Alpha OSF/1) are any longer necessary.
196*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000197long
198PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000199{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000200 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000201 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000202 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000203 /* Jump through some hoops for Alpha OSF/1 */
204 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000205#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
206 return (long) threadid;
207#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000208 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000209#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000210}
211
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000212static void
213do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000214{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000215 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000216 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000217 if (no_cleanup)
218 _exit(0);
219 else
220 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000221 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000222}
223
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224void
225PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000226{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000227 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000228}
229
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230void
231PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000232{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000234}
235
236#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237static void
238do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000239{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000241 if (!initialized)
242 if (no_cleanup)
243 _exit(status);
244 else
245 exit(status);
246}
247
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248void
249PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000250{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000252}
253
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254void
255PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000256{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000258}
259#endif /* NO_EXIT_PROG */
260
261/*
262 * Lock support.
263 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyThread_type_lock
265PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000266{
267 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000268 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000269
Guido van Rossum65d5b571998-12-21 19:32:43 +0000270 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000271 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000273
274 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000275 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000276 if (lock) {
277 lock->locked = 0;
278
279 status = pthread_mutex_init(&lock->mut,
280 pthread_mutexattr_default);
281 CHECK_STATUS("pthread_mutex_init");
282
283 status = pthread_cond_init(&lock->lock_released,
284 pthread_condattr_default);
285 CHECK_STATUS("pthread_cond_init");
286
287 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000288 free((void *)lock);
289 lock = 0;
290 }
291 }
292
Fred Drakea44d3532000-06-30 15:01:00 +0000293 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000294 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000295}
296
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297void
298PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000299{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000300 pthread_lock *thelock = (pthread_lock *)lock;
301 int status, error = 0;
302
Fred Drakea44d3532000-06-30 15:01:00 +0000303 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000304
305 status = pthread_mutex_destroy( &thelock->mut );
306 CHECK_STATUS("pthread_mutex_destroy");
307
308 status = pthread_cond_destroy( &thelock->lock_released );
309 CHECK_STATUS("pthread_cond_destroy");
310
311 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000312}
313
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314int
315PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000316{
317 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000318 pthread_lock *thelock = (pthread_lock *)lock;
319 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000320
Fred Drakea44d3532000-06-30 15:01:00 +0000321 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000322
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000323 status = pthread_mutex_lock( &thelock->mut );
324 CHECK_STATUS("pthread_mutex_lock[1]");
325 success = thelock->locked == 0;
326 if (success) thelock->locked = 1;
327 status = pthread_mutex_unlock( &thelock->mut );
328 CHECK_STATUS("pthread_mutex_unlock[1]");
329
330 if ( !success && waitflag ) {
331 /* continue trying until we get the lock */
332
333 /* mut must be locked by me -- part of the condition
334 * protocol */
335 status = pthread_mutex_lock( &thelock->mut );
336 CHECK_STATUS("pthread_mutex_lock[2]");
337 while ( thelock->locked ) {
338 status = pthread_cond_wait(&thelock->lock_released,
339 &thelock->mut);
340 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000341 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000342 thelock->locked = 1;
343 status = pthread_mutex_unlock( &thelock->mut );
344 CHECK_STATUS("pthread_mutex_unlock[2]");
345 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000346 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000347 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000348 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000349 return success;
350}
351
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352void
353PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000354{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000355 pthread_lock *thelock = (pthread_lock *)lock;
356 int status, error = 0;
357
Fred Drakea44d3532000-06-30 15:01:00 +0000358 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000359
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000360 status = pthread_mutex_lock( &thelock->mut );
361 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000362
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000363 thelock->locked = 0;
364
365 status = pthread_mutex_unlock( &thelock->mut );
366 CHECK_STATUS("pthread_mutex_unlock[3]");
367
368 /* wake up someone (anyone, if any) waiting on the lock */
369 status = pthread_cond_signal( &thelock->lock_released );
370 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000371}
372
373/*
374 * Semaphore support.
375 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000376
377struct semaphore {
378 pthread_mutex_t mutex;
379 pthread_cond_t cond;
380 int value;
381};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000382
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383PyThread_type_sema
384PyThread_allocate_sema(int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000385{
Guido van Rossum7af81301997-01-17 21:06:41 +0000386 struct semaphore *sema;
387 int status, error = 0;
388
Guido van Rossum65d5b571998-12-21 19:32:43 +0000389 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000390 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000391 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000392
Guido van Rossum7af81301997-01-17 21:06:41 +0000393 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
394 if (sema != NULL) {
395 sema->value = value;
396 status = pthread_mutex_init(&sema->mutex,
397 pthread_mutexattr_default);
398 CHECK_STATUS("pthread_mutex_init");
399 status = pthread_cond_init(&sema->cond,
400 pthread_condattr_default);
401 CHECK_STATUS("pthread_cond_init");
402 if (error) {
403 free((void *) sema);
404 sema = NULL;
405 }
406 }
Fred Drakea44d3532000-06-30 15:01:00 +0000407 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000408 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000409}
410
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000411void
412PyThread_free_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000413{
Guido van Rossum7af81301997-01-17 21:06:41 +0000414 int status, error = 0;
415 struct semaphore *thesema = (struct semaphore *) sema;
416
Fred Drakea44d3532000-06-30 15:01:00 +0000417 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000418 status = pthread_cond_destroy(&thesema->cond);
419 CHECK_STATUS("pthread_cond_destroy");
420 status = pthread_mutex_destroy(&thesema->mutex);
421 CHECK_STATUS("pthread_mutex_destroy");
422 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000423}
424
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000425int
426PyThread_down_sema(PyThread_type_sema sema, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000427{
Guido van Rossum7af81301997-01-17 21:06:41 +0000428 int status, error = 0, success;
429 struct semaphore *thesema = (struct semaphore *) sema;
430
Fred Drakea44d3532000-06-30 15:01:00 +0000431 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000432 status = pthread_mutex_lock(&thesema->mutex);
433 CHECK_STATUS("pthread_mutex_lock");
434 if (waitflag) {
435 while (!error && thesema->value <= 0) {
436 status = pthread_cond_wait(&thesema->cond,
437 &thesema->mutex);
438 CHECK_STATUS("pthread_cond_wait");
439 }
440 }
441 if (error)
442 success = 0;
443 else if (thesema->value > 0) {
444 thesema->value--;
445 success = 1;
446 }
447 else
448 success = 0;
449 status = pthread_mutex_unlock(&thesema->mutex);
450 CHECK_STATUS("pthread_mutex_unlock");
Fred Drakea44d3532000-06-30 15:01:00 +0000451 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000452 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000453}
454
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455void
456PyThread_up_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000457{
Guido van Rossum7af81301997-01-17 21:06:41 +0000458 int status, error = 0;
459 struct semaphore *thesema = (struct semaphore *) sema;
460
Fred Drakea44d3532000-06-30 15:01:00 +0000461 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000462 status = pthread_mutex_lock(&thesema->mutex);
463 CHECK_STATUS("pthread_mutex_lock");
464 thesema->value++;
465 status = pthread_cond_signal(&thesema->cond);
466 CHECK_STATUS("pthread_cond_signal");
467 status = pthread_mutex_unlock(&thesema->mutex);
468 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000469}