blob: c29a61c809a2a2ca110ee290b90e663f12f51d02 [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>
Martin v. Löwisa7a76d32002-10-04 07:21:24 +00006#if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
Jack Jansen76689572002-01-15 20:36:14 +00007#define destructor xxdestructor
8#endif
Guido van Rossum66020991996-06-11 18:32:18 +00009#include <pthread.h>
Martin v. Löwisa7a76d32002-10-04 07:21:24 +000010#if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
Jack Jansen76689572002-01-15 20:36:14 +000011#undef destructor
12#endif
Guido van Rossum80230992001-10-12 21:49:17 +000013#include <signal.h>
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000014
15/* The POSIX spec says that implementations supporting the sem_*
16 family of functions must indicate this by defining
17 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000018#ifdef _POSIX_SEMAPHORES
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000019/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
20 we need to add 0 to make it work there as well. */
21#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000022#define HAVE_BROKEN_POSIX_SEMAPHORES
23#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000024#include <semaphore.h>
25#include <errno.h>
26#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000027#endif
Guido van Rossum66020991996-06-11 18:32:18 +000028
Hye-Shik Changd478f342006-03-23 12:32:36 +000029/* Before FreeBSD 5.4, system scope threads was very limited resource
30 in default setting. So the process scope is preferred to get
31 enough number of threads to work. */
32#ifdef __FreeBSD__
33#include <osreldate.h>
34#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
35#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
36#endif
37#endif
38
Martin v. Löwisb0233812002-12-11 13:12:30 +000039#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000040# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000041#endif
42#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000043# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000044#endif
45#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000046# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000047#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000048
Guido van Rossumd6353e21997-05-13 17:51:13 +000049
Martin v. Löwiscc898662002-03-17 09:53:51 +000050/* Whether or not to use semaphores directly rather than emulating them with
51 * mutexes and condition variables:
52 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000053#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000054# define USE_SEMAPHORES
55#else
56# undef USE_SEMAPHORES
57#endif
58
59
Guido van Rossum80230992001-10-12 21:49:17 +000060/* On platforms that don't use standard POSIX threads pthread_sigmask()
61 * isn't present. DEC threads uses sigprocmask() instead as do most
62 * other UNIX International compliant systems that don't have the full
63 * pthread implementation.
64 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000065#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000066# define SET_THREAD_SIGMASK pthread_sigmask
67#else
68# define SET_THREAD_SIGMASK sigprocmask
69#endif
70
71
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
Guido van Rossum3c288632001-10-16 21:13:49 +0000133long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000134PyThread_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;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000137 int status;
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
Hye-Shik Changd478f342006-03-23 12:32:36 +0000151#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000152 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
153#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000154
Martin v. Löwis910ae622003-04-19 07:44:52 +0000155 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000156#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000157 &attrs,
158#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000159 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000160#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000161 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000162 (void *)arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000163 );
Guido van Rossum80230992001-10-12 21:49:17 +0000164
Fred Drake03459a52001-11-09 16:00:41 +0000165#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000166 pthread_attr_destroy(&attrs);
167#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000168 if (status != 0)
169 return -1;
170
Martin v. Löwis910ae622003-04-19 07:44:52 +0000171 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000172
Guido van Rossum3c288632001-10-16 21:13:49 +0000173#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
174 return (long) th;
175#else
176 return (long) *(long *) &th;
177#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000178}
179
Trent Mick635f6fb2000-08-23 21:33:05 +0000180/* XXX This implementation is considered (to quote Tim Peters) "inherently
181 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000182 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000183 - The cast to long is inherently unsafe.
184 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
185 latter return statement (for Alpha OSF/1) are any longer necessary.
186*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187long
188PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000189{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000190 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000191 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000192 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000193 /* Jump through some hoops for Alpha OSF/1 */
194 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000195#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
196 return (long) threadid;
197#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000198 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000199#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000200}
201
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202static void
203do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000204{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000205 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000206 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000207 if (no_cleanup)
208 _exit(0);
209 else
210 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000211 }
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(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000218}
219
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220void
221PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000222{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000224}
225
226#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227static void
228do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000229{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000231 if (!initialized)
232 if (no_cleanup)
233 _exit(status);
234 else
235 exit(status);
236}
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, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242}
243
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244void
245PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000246{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000248}
249#endif /* NO_EXIT_PROG */
250
Martin v. Löwiscc898662002-03-17 09:53:51 +0000251#ifdef USE_SEMAPHORES
252
253/*
254 * Lock support.
255 */
256
257PyThread_type_lock
258PyThread_allocate_lock(void)
259{
260 sem_t *lock;
261 int status, error = 0;
262
263 dprintf(("PyThread_allocate_lock called\n"));
264 if (!initialized)
265 PyThread_init_thread();
266
267 lock = (sem_t *)malloc(sizeof(sem_t));
268
269 if (lock) {
270 status = sem_init(lock,0,1);
271 CHECK_STATUS("sem_init");
272
273 if (error) {
274 free((void *)lock);
275 lock = NULL;
276 }
277 }
278
279 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
280 return (PyThread_type_lock)lock;
281}
282
283void
284PyThread_free_lock(PyThread_type_lock lock)
285{
286 sem_t *thelock = (sem_t *)lock;
287 int status, error = 0;
288
289 dprintf(("PyThread_free_lock(%p) called\n", lock));
290
291 if (!thelock)
292 return;
293
294 status = sem_destroy(thelock);
295 CHECK_STATUS("sem_destroy");
296
297 free((void *)thelock);
298}
299
300/*
301 * As of February 2002, Cygwin thread implementations mistakenly report error
302 * codes in the return value of the sem_ calls (like the pthread_ functions).
303 * Correct implementations return -1 and put the code in errno. This supports
304 * either.
305 */
306static int
307fix_status(int status)
308{
309 return (status == -1) ? errno : status;
310}
311
312int
313PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
314{
315 int success;
316 sem_t *thelock = (sem_t *)lock;
317 int status, error = 0;
318
319 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
320
321 do {
322 if (waitflag)
323 status = fix_status(sem_wait(thelock));
324 else
325 status = fix_status(sem_trywait(thelock));
326 } while (status == EINTR); /* Retry if interrupted by a signal */
327
328 if (waitflag) {
329 CHECK_STATUS("sem_wait");
330 } else if (status != EAGAIN) {
331 CHECK_STATUS("sem_trywait");
332 }
333
334 success = (status == 0) ? 1 : 0;
335
336 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
337 return success;
338}
339
340void
341PyThread_release_lock(PyThread_type_lock lock)
342{
343 sem_t *thelock = (sem_t *)lock;
344 int status, error = 0;
345
346 dprintf(("PyThread_release_lock(%p) called\n", lock));
347
348 status = sem_post(thelock);
349 CHECK_STATUS("sem_post");
350}
351
352#else /* USE_SEMAPHORES */
353
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000354/*
355 * Lock support.
356 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyThread_type_lock
358PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000359{
360 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000361 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000362
Guido van Rossum65d5b571998-12-21 19:32:43 +0000363 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000364 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000365 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000366
367 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000368 if (lock) {
Armin Rigoa6eb56c2005-09-20 18:07:47 +0000369 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000370 lock->locked = 0;
371
372 status = pthread_mutex_init(&lock->mut,
373 pthread_mutexattr_default);
374 CHECK_STATUS("pthread_mutex_init");
375
376 status = pthread_cond_init(&lock->lock_released,
377 pthread_condattr_default);
378 CHECK_STATUS("pthread_cond_init");
379
380 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000381 free((void *)lock);
382 lock = 0;
383 }
384 }
385
Fred Drakea44d3532000-06-30 15:01:00 +0000386 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000387 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000388}
389
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390void
391PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000392{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000393 pthread_lock *thelock = (pthread_lock *)lock;
394 int status, error = 0;
395
Fred Drakea44d3532000-06-30 15:01:00 +0000396 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000397
398 status = pthread_mutex_destroy( &thelock->mut );
399 CHECK_STATUS("pthread_mutex_destroy");
400
401 status = pthread_cond_destroy( &thelock->lock_released );
402 CHECK_STATUS("pthread_cond_destroy");
403
404 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000405}
406
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407int
408PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000409{
410 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000411 pthread_lock *thelock = (pthread_lock *)lock;
412 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000413
Fred Drakea44d3532000-06-30 15:01:00 +0000414 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000415
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000416 status = pthread_mutex_lock( &thelock->mut );
417 CHECK_STATUS("pthread_mutex_lock[1]");
418 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000419
420 if ( !success && waitflag ) {
421 /* continue trying until we get the lock */
422
423 /* mut must be locked by me -- part of the condition
424 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000425 while ( thelock->locked ) {
426 status = pthread_cond_wait(&thelock->lock_released,
427 &thelock->mut);
428 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000429 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000430 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000431 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000432 if (success) thelock->locked = 1;
433 status = pthread_mutex_unlock( &thelock->mut );
434 CHECK_STATUS("pthread_mutex_unlock[1]");
435
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000436 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000437 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000438 return success;
439}
440
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000441void
442PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000443{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000444 pthread_lock *thelock = (pthread_lock *)lock;
445 int status, error = 0;
446
Fred Drakea44d3532000-06-30 15:01:00 +0000447 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000448
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000449 status = pthread_mutex_lock( &thelock->mut );
450 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000451
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000452 thelock->locked = 0;
453
454 status = pthread_mutex_unlock( &thelock->mut );
455 CHECK_STATUS("pthread_mutex_unlock[3]");
456
457 /* wake up someone (anyone, if any) waiting on the lock */
458 status = pthread_cond_signal( &thelock->lock_released );
459 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000460}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000461
462#endif /* USE_SEMAPHORES */