blob: d18d2de7253a12d6beabd0d1c8576d8627971461 [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
19#include <semaphore.h>
20#include <errno.h>
21#endif
Guido van Rossum66020991996-06-11 18:32:18 +000022
Martin v. Löwisb0233812002-12-11 13:12:30 +000023#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000024# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000025#endif
26#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000027# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000028#endif
29#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000030# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000031#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000032
Guido van Rossumd6353e21997-05-13 17:51:13 +000033
Martin v. Löwiscc898662002-03-17 09:53:51 +000034/* Whether or not to use semaphores directly rather than emulating them with
35 * mutexes and condition variables:
36 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000037#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000038# define USE_SEMAPHORES
39#else
40# undef USE_SEMAPHORES
41#endif
42
43
Guido van Rossum80230992001-10-12 21:49:17 +000044/* On platforms that don't use standard POSIX threads pthread_sigmask()
45 * isn't present. DEC threads uses sigprocmask() instead as do most
46 * other UNIX International compliant systems that don't have the full
47 * pthread implementation.
48 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000049#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000050# define SET_THREAD_SIGMASK pthread_sigmask
51#else
52# define SET_THREAD_SIGMASK sigprocmask
53#endif
54
55
Guido van Rossumb98b1b31994-05-11 08:42:04 +000056/* A pthread mutex isn't sufficient to model the Python lock type
57 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
58 * following are undefined:
59 * -> a thread tries to lock a mutex it already has locked
60 * -> a thread tries to unlock a mutex locked by a different thread
61 * pthread mutexes are designed for serializing threads over short pieces
62 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000063 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000064 *
65 * The pthread_lock struct implements a Python lock as a "locked?" bit
66 * and a <condition, mutex> pair. In general, if the bit can be acquired
67 * instantly, it is, else the pair is used to block the thread until the
68 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +000070
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000071typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +000072 char locked; /* 0=unlocked, 1=locked */
73 /* a <cond, mutex> pair to handle an acquire of a locked lock */
74 pthread_cond_t lock_released;
75 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000076} pthread_lock;
77
Guido van Rossum9e46e561998-10-07 16:39:47 +000078#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +000079
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000080/*
81 * Initialization.
82 */
Guido van Rossum9e46e561998-10-07 16:39:47 +000083
84#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085static
86void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +000087{
88}
89
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000090static void
91PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +000092{
93 /* DO AN INIT BY STARTING THE THREAD */
94 static int dummy = 0;
95 pthread_t thread1;
96 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
97 pthread_join(thread1, NULL);
98}
99
100#else /* !_HAVE_BSDI */
101
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102static void
103PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000104{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000105#if defined(_AIX) && defined(__GNUC__)
106 pthread_init();
107#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000108}
109
Guido van Rossum9e46e561998-10-07 16:39:47 +0000110#endif /* !_HAVE_BSDI */
111
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000112/*
113 * Thread support.
114 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000115
116
Guido van Rossum3c288632001-10-16 21:13:49 +0000117long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000118PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000119{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000120 pthread_t th;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000121 int status;
Guido van Rossum80230992001-10-12 21:49:17 +0000122 sigset_t oldmask, newmask;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000123#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000124 pthread_attr_t attrs;
125#endif
Guido van Rossum65d5b571998-12-21 19:32:43 +0000126 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000128 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000129
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000130#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000131 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000132#endif
133#ifdef THREAD_STACK_SIZE
Jack Jansenc51395d2001-08-29 15:24:53 +0000134 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
135#endif
Hye-Shik Chang30e97db2004-03-04 06:35:57 +0000136#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) && !defined(__FreeBSD__)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000137 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
138#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000139
140 /* Mask all signals in the current thread before creating the new
141 * thread. This causes the new thread to start with all signals
142 * blocked.
143 */
144 sigfillset(&newmask);
145 SET_THREAD_SIGMASK(SIG_BLOCK, &newmask, &oldmask);
146
Martin v. Löwis910ae622003-04-19 07:44:52 +0000147 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000148#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000149 &attrs,
150#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000151 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000152#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000154 (void *)arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000155 );
Guido van Rossum80230992001-10-12 21:49:17 +0000156
157 /* Restore signal mask for original thread */
158 SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);
159
Fred Drake03459a52001-11-09 16:00:41 +0000160#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000161 pthread_attr_destroy(&attrs);
162#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000163 if (status != 0)
164 return -1;
165
Martin v. Löwis910ae622003-04-19 07:44:52 +0000166 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000167
Guido van Rossum3c288632001-10-16 21:13:49 +0000168#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
169 return (long) th;
170#else
171 return (long) *(long *) &th;
172#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000173}
174
Trent Mick635f6fb2000-08-23 21:33:05 +0000175/* XXX This implementation is considered (to quote Tim Peters) "inherently
176 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000177 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000178 - The cast to long is inherently unsafe.
179 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
180 latter return statement (for Alpha OSF/1) are any longer necessary.
181*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000182long
183PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000184{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000185 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000186 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000187 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000188 /* Jump through some hoops for Alpha OSF/1 */
189 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000190#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
191 return (long) threadid;
192#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000193 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000194#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000195}
196
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000197static void
198do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000199{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000200 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000201 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000202 if (no_cleanup)
203 _exit(0);
204 else
205 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000206 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000207}
208
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209void
210PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000211{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000212 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000213}
214
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000215void
216PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000217{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000218 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000219}
220
221#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000222static void
223do_PyThread_exit_prog(int status, 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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233void
234PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000235{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000237}
238
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239void
240PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000241{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000242 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000243}
244#endif /* NO_EXIT_PROG */
245
Martin v. Löwiscc898662002-03-17 09:53:51 +0000246#ifdef USE_SEMAPHORES
247
248/*
249 * Lock support.
250 */
251
252PyThread_type_lock
253PyThread_allocate_lock(void)
254{
255 sem_t *lock;
256 int status, error = 0;
257
258 dprintf(("PyThread_allocate_lock called\n"));
259 if (!initialized)
260 PyThread_init_thread();
261
262 lock = (sem_t *)malloc(sizeof(sem_t));
263
264 if (lock) {
265 status = sem_init(lock,0,1);
266 CHECK_STATUS("sem_init");
267
268 if (error) {
269 free((void *)lock);
270 lock = NULL;
271 }
272 }
273
274 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
275 return (PyThread_type_lock)lock;
276}
277
278void
279PyThread_free_lock(PyThread_type_lock lock)
280{
281 sem_t *thelock = (sem_t *)lock;
282 int status, error = 0;
283
284 dprintf(("PyThread_free_lock(%p) called\n", lock));
285
286 if (!thelock)
287 return;
288
289 status = sem_destroy(thelock);
290 CHECK_STATUS("sem_destroy");
291
292 free((void *)thelock);
293}
294
295/*
296 * As of February 2002, Cygwin thread implementations mistakenly report error
297 * codes in the return value of the sem_ calls (like the pthread_ functions).
298 * Correct implementations return -1 and put the code in errno. This supports
299 * either.
300 */
301static int
302fix_status(int status)
303{
304 return (status == -1) ? errno : status;
305}
306
307int
308PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
309{
310 int success;
311 sem_t *thelock = (sem_t *)lock;
312 int status, error = 0;
313
314 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
315
316 do {
317 if (waitflag)
318 status = fix_status(sem_wait(thelock));
319 else
320 status = fix_status(sem_trywait(thelock));
321 } while (status == EINTR); /* Retry if interrupted by a signal */
322
323 if (waitflag) {
324 CHECK_STATUS("sem_wait");
325 } else if (status != EAGAIN) {
326 CHECK_STATUS("sem_trywait");
327 }
328
329 success = (status == 0) ? 1 : 0;
330
331 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
332 return success;
333}
334
335void
336PyThread_release_lock(PyThread_type_lock lock)
337{
338 sem_t *thelock = (sem_t *)lock;
339 int status, error = 0;
340
341 dprintf(("PyThread_release_lock(%p) called\n", lock));
342
343 status = sem_post(thelock);
344 CHECK_STATUS("sem_post");
345}
346
347#else /* USE_SEMAPHORES */
348
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000349/*
350 * Lock support.
351 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352PyThread_type_lock
353PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000354{
355 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000356 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000357
Guido van Rossum65d5b571998-12-21 19:32:43 +0000358 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000359 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000360 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000361
362 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000363 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000364 if (lock) {
365 lock->locked = 0;
366
367 status = pthread_mutex_init(&lock->mut,
368 pthread_mutexattr_default);
369 CHECK_STATUS("pthread_mutex_init");
370
371 status = pthread_cond_init(&lock->lock_released,
372 pthread_condattr_default);
373 CHECK_STATUS("pthread_cond_init");
374
375 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000376 free((void *)lock);
377 lock = 0;
378 }
379 }
380
Fred Drakea44d3532000-06-30 15:01:00 +0000381 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000382 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000383}
384
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385void
386PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000387{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000388 pthread_lock *thelock = (pthread_lock *)lock;
389 int status, error = 0;
390
Fred Drakea44d3532000-06-30 15:01:00 +0000391 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000392
393 status = pthread_mutex_destroy( &thelock->mut );
394 CHECK_STATUS("pthread_mutex_destroy");
395
396 status = pthread_cond_destroy( &thelock->lock_released );
397 CHECK_STATUS("pthread_cond_destroy");
398
399 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000400}
401
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000402int
403PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000404{
405 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000406 pthread_lock *thelock = (pthread_lock *)lock;
407 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000408
Fred Drakea44d3532000-06-30 15:01:00 +0000409 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000410
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000411 status = pthread_mutex_lock( &thelock->mut );
412 CHECK_STATUS("pthread_mutex_lock[1]");
413 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000414
415 if ( !success && waitflag ) {
416 /* continue trying until we get the lock */
417
418 /* mut must be locked by me -- part of the condition
419 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000420 while ( thelock->locked ) {
421 status = pthread_cond_wait(&thelock->lock_released,
422 &thelock->mut);
423 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000424 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000425 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000426 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000427 if (success) thelock->locked = 1;
428 status = pthread_mutex_unlock( &thelock->mut );
429 CHECK_STATUS("pthread_mutex_unlock[1]");
430
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000431 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000432 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000433 return success;
434}
435
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436void
437PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000438{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000439 pthread_lock *thelock = (pthread_lock *)lock;
440 int status, error = 0;
441
Fred Drakea44d3532000-06-30 15:01:00 +0000442 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000443
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000444 status = pthread_mutex_lock( &thelock->mut );
445 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000446
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000447 thelock->locked = 0;
448
449 status = pthread_mutex_unlock( &thelock->mut );
450 CHECK_STATUS("pthread_mutex_unlock[3]");
451
452 /* wake up someone (anyone, if any) waiting on the lock */
453 status = pthread_cond_signal( &thelock->lock_released );
454 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000455}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000456
457#endif /* USE_SEMAPHORES */