blob: 0b4041a3a986d54f10439015b2ac611caf5f6217 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000029
30******************************************************************/
31
Guido van Rossum66020991996-06-11 18:32:18 +000032/* Posix threads interface */
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000033
Guido van Rossum66020991996-06-11 18:32:18 +000034#include <stdlib.h>
Guido van Rossum9e46e561998-10-07 16:39:47 +000035#include <string.h>
Guido van Rossum66020991996-06-11 18:32:18 +000036#include <pthread.h>
37
Guido van Rossum1a623111996-08-08 18:53:41 +000038
Guido van Rossumd6353e21997-05-13 17:51:13 +000039/* try to determine what version of the Pthread Standard is installed.
40 * this is important, since all sorts of parameter types changed from
41 * draft to draft and there are several (incompatible) drafts in
42 * common use. these macros are a start, at least.
43 * 12 May 1997 -- david arnold <davida@pobox.com>
44 */
45
46#if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
47/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
48# define PY_PTHREAD_D4
49
50#elif defined(__osf__) && defined (__alpha)
51/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
52# if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
53# define PY_PTHREAD_D4
54# else
55# define PY_PTHREAD_STD
56# endif
57
58#elif defined(_AIX)
Guido van Rossum1a623111996-08-08 18:53:41 +000059/* SCHED_BG_NP is defined if using AIX DCE pthreads
60 * but it is unsupported by AIX 4 pthreads. Default
61 * attributes for AIX 4 pthreads equal to NULL. For
62 * AIX DCE pthreads they should be left unchanged.
63 */
Guido van Rossumd6353e21997-05-13 17:51:13 +000064# if !defined(SCHED_BG_NP)
65# define PY_PTHREAD_STD
66# else
67# define PY_PTHREAD_D7
68# endif
69
Guido van Rossum64f91051997-05-22 20:41:59 +000070#elif defined(__DGUX)
71# define PY_PTHREAD_D6
Guido van Rossum46ff1901997-06-02 22:25:45 +000072
Guido van Rossum532246e1998-05-14 21:01:27 +000073#elif defined(__hpux) && defined(_DECTHREADS_)
Guido van Rossum89df70b1998-05-07 13:28:23 +000074# define PY_PTHREAD_D4
75
Guido van Rossum46ff1901997-06-02 22:25:45 +000076#else /* Default case */
77# define PY_PTHREAD_STD
78
Guido van Rossum1a623111996-08-08 18:53:41 +000079#endif
80
Guido van Rossumd6353e21997-05-13 17:51:13 +000081
82/* set default attribute object for different versions */
83
84#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
85# define pthread_attr_default pthread_attr_default
86# define pthread_mutexattr_default pthread_mutexattr_default
87# define pthread_condattr_default pthread_condattr_default
Guido van Rossum64f91051997-05-22 20:41:59 +000088#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Guido van Rossumd6353e21997-05-13 17:51:13 +000089# define pthread_attr_default ((pthread_attr_t *)NULL)
90# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
91# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000092#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000093
Guido van Rossumd6353e21997-05-13 17:51:13 +000094
Guido van Rossumb98b1b31994-05-11 08:42:04 +000095/* A pthread mutex isn't sufficient to model the Python lock type
96 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
97 * following are undefined:
98 * -> a thread tries to lock a mutex it already has locked
99 * -> a thread tries to unlock a mutex locked by a different thread
100 * pthread mutexes are designed for serializing threads over short pieces
101 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000102 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000103 *
104 * The pthread_lock struct implements a Python lock as a "locked?" bit
105 * and a <condition, mutex> pair. In general, if the bit can be acquired
106 * instantly, it is, else the pair is used to block the thread until the
107 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000108 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000109
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000110typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000111 char locked; /* 0=unlocked, 1=locked */
112 /* a <cond, mutex> pair to handle an acquire of a locked lock */
113 pthread_cond_t lock_released;
114 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000115} pthread_lock;
116
Guido van Rossum9e46e561998-10-07 16:39:47 +0000117#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000118
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000119/*
120 * Initialization.
121 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000122
123#ifdef _HAVE_BSDI
124static void _noop()
125{
126}
127
Guido van Rossum65d5b571998-12-21 19:32:43 +0000128static void PyThread__init_thread _P0()
Guido van Rossum9e46e561998-10-07 16:39:47 +0000129{
130 /* DO AN INIT BY STARTING THE THREAD */
131 static int dummy = 0;
132 pthread_t thread1;
133 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
134 pthread_join(thread1, NULL);
135}
136
137#else /* !_HAVE_BSDI */
138
Guido van Rossum65d5b571998-12-21 19:32:43 +0000139static void PyThread__init_thread _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000140{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000141#if defined(_AIX) && defined(__GNUC__)
142 pthread_init();
143#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000144}
145
Guido van Rossum9e46e561998-10-07 16:39:47 +0000146#endif /* !_HAVE_BSDI */
147
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000148/*
149 * Thread support.
150 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000151
152
Guido van Rossum65d5b571998-12-21 19:32:43 +0000153int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000155 pthread_t th;
Guido van Rossume944da81994-05-23 12:43:41 +0000156 int success;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000157 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000158 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000159 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000160
161 success = pthread_create(&th,
162#if defined(PY_PTHREAD_D4)
163 pthread_attr_default,
164 (pthread_startroutine_t)func,
165 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000166#elif defined(PY_PTHREAD_D6)
167 pthread_attr_default,
168 (void* (*)_P((void *)))func,
169 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000170#elif defined(PY_PTHREAD_D7)
171 pthread_attr_default,
172 func,
173 arg
174#elif defined(PY_PTHREAD_STD)
175 (pthread_attr_t*)NULL,
176 (void* (*)_P((void *)))func,
177 (void *)arg
178#endif
179 );
180
181 if (success >= 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000182#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000183 pthread_detach(&th);
184#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000185 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000186#endif
187 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000188 return success < 0 ? 0 : 1;
189}
190
Guido van Rossum65d5b571998-12-21 19:32:43 +0000191long PyThread_get_thread_ident _P0()
Guido van Rossume944da81994-05-23 12:43:41 +0000192{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000193 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000194 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000195 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000196 /* Jump through some hoops for Alpha OSF/1 */
197 threadid = pthread_self();
198 return (long) *(long *) &threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000199}
200
Guido van Rossum65d5b571998-12-21 19:32:43 +0000201static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000202{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000203 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000204 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000205 if (no_cleanup)
206 _exit(0);
207 else
208 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000209 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000210}
211
Guido van Rossum65d5b571998-12-21 19:32:43 +0000212void PyThread_exit_thread _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000213{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000214 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000215}
216
Guido van Rossum65d5b571998-12-21 19:32:43 +0000217void PyThread__exit_thread _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000218{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000219 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000220}
221
222#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000224{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000225 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000226 if (!initialized)
227 if (no_cleanup)
228 _exit(status);
229 else
230 exit(status);
231}
232
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233void PyThread_exit_prog _P1(status, 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
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238void PyThread__exit_prog _P1(status, int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000239{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000241}
242#endif /* NO_EXIT_PROG */
243
244/*
245 * Lock support.
246 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247PyThread_type_lock PyThread_allocate_lock _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000248{
249 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000250 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000251
Guido van Rossum65d5b571998-12-21 19:32:43 +0000252 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000253 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000254 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000255
256 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000257 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000258 if (lock) {
259 lock->locked = 0;
260
261 status = pthread_mutex_init(&lock->mut,
262 pthread_mutexattr_default);
263 CHECK_STATUS("pthread_mutex_init");
264
265 status = pthread_cond_init(&lock->lock_released,
266 pthread_condattr_default);
267 CHECK_STATUS("pthread_cond_init");
268
269 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000270 free((void *)lock);
271 lock = 0;
272 }
273 }
274
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275 dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock));
276 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000277}
278
Guido van Rossum65d5b571998-12-21 19:32:43 +0000279void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000280{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000281 pthread_lock *thelock = (pthread_lock *)lock;
282 int status, error = 0;
283
Guido van Rossum65d5b571998-12-21 19:32:43 +0000284 dprintf(("PyThread_free_lock(%lx) called\n", (long)lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000285
286 status = pthread_mutex_destroy( &thelock->mut );
287 CHECK_STATUS("pthread_mutex_destroy");
288
289 status = pthread_cond_destroy( &thelock->lock_released );
290 CHECK_STATUS("pthread_cond_destroy");
291
292 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000293}
294
Guido van Rossum65d5b571998-12-21 19:32:43 +0000295int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000296{
297 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000298 pthread_lock *thelock = (pthread_lock *)lock;
299 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000300
Guido van Rossum65d5b571998-12-21 19:32:43 +0000301 dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000302
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000303 status = pthread_mutex_lock( &thelock->mut );
304 CHECK_STATUS("pthread_mutex_lock[1]");
305 success = thelock->locked == 0;
306 if (success) thelock->locked = 1;
307 status = pthread_mutex_unlock( &thelock->mut );
308 CHECK_STATUS("pthread_mutex_unlock[1]");
309
310 if ( !success && waitflag ) {
311 /* continue trying until we get the lock */
312
313 /* mut must be locked by me -- part of the condition
314 * protocol */
315 status = pthread_mutex_lock( &thelock->mut );
316 CHECK_STATUS("pthread_mutex_lock[2]");
317 while ( thelock->locked ) {
318 status = pthread_cond_wait(&thelock->lock_released,
319 &thelock->mut);
320 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000321 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000322 thelock->locked = 1;
323 status = pthread_mutex_unlock( &thelock->mut );
324 CHECK_STATUS("pthread_mutex_unlock[2]");
325 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000326 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000327 if (error) success = 0;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000328 dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000329 return success;
330}
331
Guido van Rossum65d5b571998-12-21 19:32:43 +0000332void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000333{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000334 pthread_lock *thelock = (pthread_lock *)lock;
335 int status, error = 0;
336
Guido van Rossum65d5b571998-12-21 19:32:43 +0000337 dprintf(("PyThread_release_lock(%lx) called\n", (long)lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000338
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000339 status = pthread_mutex_lock( &thelock->mut );
340 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000341
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000342 thelock->locked = 0;
343
344 status = pthread_mutex_unlock( &thelock->mut );
345 CHECK_STATUS("pthread_mutex_unlock[3]");
346
347 /* wake up someone (anyone, if any) waiting on the lock */
348 status = pthread_cond_signal( &thelock->lock_released );
349 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000350}
351
352/*
353 * Semaphore support.
354 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000355
356struct semaphore {
357 pthread_mutex_t mutex;
358 pthread_cond_t cond;
359 int value;
360};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000361
Guido van Rossum65d5b571998-12-21 19:32:43 +0000362PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000363{
Guido van Rossum7af81301997-01-17 21:06:41 +0000364 struct semaphore *sema;
365 int status, error = 0;
366
Guido van Rossum65d5b571998-12-21 19:32:43 +0000367 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000368 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000369 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000370
Guido van Rossum7af81301997-01-17 21:06:41 +0000371 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
372 if (sema != NULL) {
373 sema->value = value;
374 status = pthread_mutex_init(&sema->mutex,
375 pthread_mutexattr_default);
376 CHECK_STATUS("pthread_mutex_init");
377 status = pthread_cond_init(&sema->cond,
378 pthread_condattr_default);
379 CHECK_STATUS("pthread_cond_init");
380 if (error) {
381 free((void *) sema);
382 sema = NULL;
383 }
384 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000385 dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema));
386 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000387}
388
Guido van Rossum65d5b571998-12-21 19:32:43 +0000389void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000390{
Guido van Rossum7af81301997-01-17 21:06:41 +0000391 int status, error = 0;
392 struct semaphore *thesema = (struct semaphore *) sema;
393
Guido van Rossum65d5b571998-12-21 19:32:43 +0000394 dprintf(("PyThread_free_sema(%lx) called\n", (long) sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000395 status = pthread_cond_destroy(&thesema->cond);
396 CHECK_STATUS("pthread_cond_destroy");
397 status = pthread_mutex_destroy(&thesema->mutex);
398 CHECK_STATUS("pthread_mutex_destroy");
399 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000400}
401
Guido van Rossum65d5b571998-12-21 19:32:43 +0000402int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000403{
Guido van Rossum7af81301997-01-17 21:06:41 +0000404 int status, error = 0, success;
405 struct semaphore *thesema = (struct semaphore *) sema;
406
Guido van Rossum65d5b571998-12-21 19:32:43 +0000407 dprintf(("PyThread_down_sema(%lx, %d) called\n", (long) sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000408 status = pthread_mutex_lock(&thesema->mutex);
409 CHECK_STATUS("pthread_mutex_lock");
410 if (waitflag) {
411 while (!error && thesema->value <= 0) {
412 status = pthread_cond_wait(&thesema->cond,
413 &thesema->mutex);
414 CHECK_STATUS("pthread_cond_wait");
415 }
416 }
417 if (error)
418 success = 0;
419 else if (thesema->value > 0) {
420 thesema->value--;
421 success = 1;
422 }
423 else
424 success = 0;
425 status = pthread_mutex_unlock(&thesema->mutex);
426 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000427 dprintf(("PyThread_down_sema(%lx) return\n", (long) sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000428 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000429}
430
Guido van Rossum65d5b571998-12-21 19:32:43 +0000431void PyThread_up_sema _P1(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
Guido van Rossum65d5b571998-12-21 19:32:43 +0000436 dprintf(("PyThread_up_sema(%lx)\n", (long) 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}