blob: e788d82e55c0fc87227d09637367bcab7b6dbfe3 [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
103static void _noop()
104{
105}
106
Guido van Rossum65d5b571998-12-21 19:32:43 +0000107static void PyThread__init_thread _P0()
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
Guido van Rossum65d5b571998-12-21 19:32:43 +0000118static void PyThread__init_thread _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000119{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000120#if defined(_AIX) && defined(__GNUC__)
121 pthread_init();
122#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000123}
124
Guido van Rossum9e46e561998-10-07 16:39:47 +0000125#endif /* !_HAVE_BSDI */
126
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127/*
128 * Thread support.
129 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000130
131
Guido van Rossum65d5b571998-12-21 19:32:43 +0000132int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000133{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000134 pthread_t th;
Guido van Rossume944da81994-05-23 12:43:41 +0000135 int success;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000136 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000137 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000138 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000139
140 success = pthread_create(&th,
141#if defined(PY_PTHREAD_D4)
142 pthread_attr_default,
143 (pthread_startroutine_t)func,
144 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000145#elif defined(PY_PTHREAD_D6)
146 pthread_attr_default,
147 (void* (*)_P((void *)))func,
148 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000149#elif defined(PY_PTHREAD_D7)
150 pthread_attr_default,
151 func,
152 arg
153#elif defined(PY_PTHREAD_STD)
154 (pthread_attr_t*)NULL,
155 (void* (*)_P((void *)))func,
156 (void *)arg
157#endif
158 );
159
Guido van Rossum701f25e1999-03-15 20:27:53 +0000160 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000161#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000162 pthread_detach(&th);
163#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000164 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000165#endif
166 }
Guido van Rossum701f25e1999-03-15 20:27:53 +0000167 return success != 0 ? 0 : 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000168}
169
Guido van Rossum65d5b571998-12-21 19:32:43 +0000170long PyThread_get_thread_ident _P0()
Guido van Rossume944da81994-05-23 12:43:41 +0000171{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000172 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000173 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000174 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000175 /* Jump through some hoops for Alpha OSF/1 */
176 threadid = pthread_self();
177 return (long) *(long *) &threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000178}
179
Guido van Rossum65d5b571998-12-21 19:32:43 +0000180static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000181{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000182 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000183 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000184 if (no_cleanup)
185 _exit(0);
186 else
187 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000188 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000189}
190
Guido van Rossum65d5b571998-12-21 19:32:43 +0000191void PyThread_exit_thread _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000192{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000193 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000194}
195
Guido van Rossum65d5b571998-12-21 19:32:43 +0000196void PyThread__exit_thread _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000197{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000199}
200
201#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000202static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000203{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000204 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000205 if (!initialized)
206 if (no_cleanup)
207 _exit(status);
208 else
209 exit(status);
210}
211
Guido van Rossum65d5b571998-12-21 19:32:43 +0000212void PyThread_exit_prog _P1(status, int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000213{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000214 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000215}
216
Guido van Rossum65d5b571998-12-21 19:32:43 +0000217void PyThread__exit_prog _P1(status, int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000218{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000219 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000220}
221#endif /* NO_EXIT_PROG */
222
223/*
224 * Lock support.
225 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000226PyThread_type_lock PyThread_allocate_lock _P0()
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000227{
228 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000229 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000230
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000232 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000234
235 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000236 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000237 if (lock) {
238 lock->locked = 0;
239
240 status = pthread_mutex_init(&lock->mut,
241 pthread_mutexattr_default);
242 CHECK_STATUS("pthread_mutex_init");
243
244 status = pthread_cond_init(&lock->lock_released,
245 pthread_condattr_default);
246 CHECK_STATUS("pthread_cond_init");
247
248 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000249 free((void *)lock);
250 lock = 0;
251 }
252 }
253
Fred Drakea44d3532000-06-30 15:01:00 +0000254 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000256}
257
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000259{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000260 pthread_lock *thelock = (pthread_lock *)lock;
261 int status, error = 0;
262
Fred Drakea44d3532000-06-30 15:01:00 +0000263 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000264
265 status = pthread_mutex_destroy( &thelock->mut );
266 CHECK_STATUS("pthread_mutex_destroy");
267
268 status = pthread_cond_destroy( &thelock->lock_released );
269 CHECK_STATUS("pthread_cond_destroy");
270
271 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000272}
273
Guido van Rossum65d5b571998-12-21 19:32:43 +0000274int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000275{
276 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000277 pthread_lock *thelock = (pthread_lock *)lock;
278 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000279
Fred Drakea44d3532000-06-30 15:01:00 +0000280 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000281
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000282 status = pthread_mutex_lock( &thelock->mut );
283 CHECK_STATUS("pthread_mutex_lock[1]");
284 success = thelock->locked == 0;
285 if (success) thelock->locked = 1;
286 status = pthread_mutex_unlock( &thelock->mut );
287 CHECK_STATUS("pthread_mutex_unlock[1]");
288
289 if ( !success && waitflag ) {
290 /* continue trying until we get the lock */
291
292 /* mut must be locked by me -- part of the condition
293 * protocol */
294 status = pthread_mutex_lock( &thelock->mut );
295 CHECK_STATUS("pthread_mutex_lock[2]");
296 while ( thelock->locked ) {
297 status = pthread_cond_wait(&thelock->lock_released,
298 &thelock->mut);
299 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000300 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000301 thelock->locked = 1;
302 status = pthread_mutex_unlock( &thelock->mut );
303 CHECK_STATUS("pthread_mutex_unlock[2]");
304 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000305 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000306 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000307 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000308 return success;
309}
310
Guido van Rossum65d5b571998-12-21 19:32:43 +0000311void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000312{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000313 pthread_lock *thelock = (pthread_lock *)lock;
314 int status, error = 0;
315
Fred Drakea44d3532000-06-30 15:01:00 +0000316 dprintf(("PyThread_release_lock(%p) called\n", lock));
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[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000320
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000321 thelock->locked = 0;
322
323 status = pthread_mutex_unlock( &thelock->mut );
324 CHECK_STATUS("pthread_mutex_unlock[3]");
325
326 /* wake up someone (anyone, if any) waiting on the lock */
327 status = pthread_cond_signal( &thelock->lock_released );
328 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000329}
330
331/*
332 * Semaphore support.
333 */
Guido van Rossum7af81301997-01-17 21:06:41 +0000334
335struct semaphore {
336 pthread_mutex_t mutex;
337 pthread_cond_t cond;
338 int value;
339};
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000340
Guido van Rossum65d5b571998-12-21 19:32:43 +0000341PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000342{
Guido van Rossum7af81301997-01-17 21:06:41 +0000343 struct semaphore *sema;
344 int status, error = 0;
345
Guido van Rossum65d5b571998-12-21 19:32:43 +0000346 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000347 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000348 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000349
Guido van Rossum7af81301997-01-17 21:06:41 +0000350 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
351 if (sema != NULL) {
352 sema->value = value;
353 status = pthread_mutex_init(&sema->mutex,
354 pthread_mutexattr_default);
355 CHECK_STATUS("pthread_mutex_init");
356 status = pthread_cond_init(&sema->cond,
357 pthread_condattr_default);
358 CHECK_STATUS("pthread_cond_init");
359 if (error) {
360 free((void *) sema);
361 sema = NULL;
362 }
363 }
Fred Drakea44d3532000-06-30 15:01:00 +0000364 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000365 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000366}
367
Guido van Rossum65d5b571998-12-21 19:32:43 +0000368void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000369{
Guido van Rossum7af81301997-01-17 21:06:41 +0000370 int status, error = 0;
371 struct semaphore *thesema = (struct semaphore *) sema;
372
Fred Drakea44d3532000-06-30 15:01:00 +0000373 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000374 status = pthread_cond_destroy(&thesema->cond);
375 CHECK_STATUS("pthread_cond_destroy");
376 status = pthread_mutex_destroy(&thesema->mutex);
377 CHECK_STATUS("pthread_mutex_destroy");
378 free((void *) thesema);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000379}
380
Guido van Rossum65d5b571998-12-21 19:32:43 +0000381int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000382{
Guido van Rossum7af81301997-01-17 21:06:41 +0000383 int status, error = 0, success;
384 struct semaphore *thesema = (struct semaphore *) sema;
385
Fred Drakea44d3532000-06-30 15:01:00 +0000386 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
Guido van Rossum7af81301997-01-17 21:06:41 +0000387 status = pthread_mutex_lock(&thesema->mutex);
388 CHECK_STATUS("pthread_mutex_lock");
389 if (waitflag) {
390 while (!error && thesema->value <= 0) {
391 status = pthread_cond_wait(&thesema->cond,
392 &thesema->mutex);
393 CHECK_STATUS("pthread_cond_wait");
394 }
395 }
396 if (error)
397 success = 0;
398 else if (thesema->value > 0) {
399 thesema->value--;
400 success = 1;
401 }
402 else
403 success = 0;
404 status = pthread_mutex_unlock(&thesema->mutex);
405 CHECK_STATUS("pthread_mutex_unlock");
Fred Drakea44d3532000-06-30 15:01:00 +0000406 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000407 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000408}
409
Guido van Rossum65d5b571998-12-21 19:32:43 +0000410void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000411{
Guido van Rossum7af81301997-01-17 21:06:41 +0000412 int status, error = 0;
413 struct semaphore *thesema = (struct semaphore *) sema;
414
Fred Drakea44d3532000-06-30 15:01:00 +0000415 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum7af81301997-01-17 21:06:41 +0000416 status = pthread_mutex_lock(&thesema->mutex);
417 CHECK_STATUS("pthread_mutex_lock");
418 thesema->value++;
419 status = pthread_cond_signal(&thesema->cond);
420 CHECK_STATUS("pthread_cond_signal");
421 status = pthread_mutex_unlock(&thesema->mutex);
422 CHECK_STATUS("pthread_mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000423}