blob: 781467f84bed69c25a968426a2f3e1b658b2d5f7 [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
Martin v. Löwisb0233812002-12-11 13:12:30 +000029#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000030# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000031#endif
32#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000033# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000034#endif
35#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000036# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000037#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000038
Guido van Rossumd6353e21997-05-13 17:51:13 +000039
Martin v. Löwiscc898662002-03-17 09:53:51 +000040/* Whether or not to use semaphores directly rather than emulating them with
41 * mutexes and condition variables:
42 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000043#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000044# define USE_SEMAPHORES
45#else
46# undef USE_SEMAPHORES
47#endif
48
49
Guido van Rossum80230992001-10-12 21:49:17 +000050/* On platforms that don't use standard POSIX threads pthread_sigmask()
51 * isn't present. DEC threads uses sigprocmask() instead as do most
52 * other UNIX International compliant systems that don't have the full
53 * pthread implementation.
54 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000055#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000056# define SET_THREAD_SIGMASK pthread_sigmask
57#else
58# define SET_THREAD_SIGMASK sigprocmask
59#endif
60
61
Guido van Rossumb98b1b31994-05-11 08:42:04 +000062/* A pthread mutex isn't sufficient to model the Python lock type
63 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
64 * following are undefined:
65 * -> a thread tries to lock a mutex it already has locked
66 * -> a thread tries to unlock a mutex locked by a different thread
67 * pthread mutexes are designed for serializing threads over short pieces
68 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +000070 *
71 * The pthread_lock struct implements a Python lock as a "locked?" bit
72 * and a <condition, mutex> pair. In general, if the bit can be acquired
73 * instantly, it is, else the pair is used to block the thread until the
74 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000075 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +000076
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000077typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +000078 char locked; /* 0=unlocked, 1=locked */
79 /* a <cond, mutex> pair to handle an acquire of a locked lock */
80 pthread_cond_t lock_released;
81 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000082} pthread_lock;
83
Guido van Rossum9e46e561998-10-07 16:39:47 +000084#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +000085
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000086/*
87 * Initialization.
88 */
Guido van Rossum9e46e561998-10-07 16:39:47 +000089
90#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000091static
92void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +000093{
94}
95
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000096static void
97PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +000098{
99 /* DO AN INIT BY STARTING THE THREAD */
100 static int dummy = 0;
101 pthread_t thread1;
102 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
103 pthread_join(thread1, NULL);
104}
105
106#else /* !_HAVE_BSDI */
107
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000108static void
109PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000110{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000111#if defined(_AIX) && defined(__GNUC__)
112 pthread_init();
113#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000114}
115
Guido van Rossum9e46e561998-10-07 16:39:47 +0000116#endif /* !_HAVE_BSDI */
117
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000118/*
119 * Thread support.
120 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000121
122
Guido van Rossum3c288632001-10-16 21:13:49 +0000123long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000125{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126 pthread_t th;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000127 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000128#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000129 pthread_attr_t attrs;
130#endif
Guido van Rossum65d5b571998-12-21 19:32:43 +0000131 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000132 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000133 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000134
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000135#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000136 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000137#endif
138#ifdef THREAD_STACK_SIZE
Jack Jansenc51395d2001-08-29 15:24:53 +0000139 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
140#endif
Hye-Shik Chang30e97db2004-03-04 06:35:57 +0000141#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) && !defined(__FreeBSD__)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000142 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
143#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000144
Martin v. Löwis910ae622003-04-19 07:44:52 +0000145 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000146#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000147 &attrs,
148#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000149 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000150#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000152 (void *)arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000153 );
Guido van Rossum80230992001-10-12 21:49:17 +0000154
Fred Drake03459a52001-11-09 16:00:41 +0000155#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000156 pthread_attr_destroy(&attrs);
157#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000158 if (status != 0)
159 return -1;
160
Martin v. Löwis910ae622003-04-19 07:44:52 +0000161 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000162
Guido van Rossum3c288632001-10-16 21:13:49 +0000163#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
164 return (long) th;
165#else
166 return (long) *(long *) &th;
167#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000168}
169
Trent Mick635f6fb2000-08-23 21:33:05 +0000170/* XXX This implementation is considered (to quote Tim Peters) "inherently
171 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000172 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000173 - The cast to long is inherently unsafe.
174 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
175 latter return statement (for Alpha OSF/1) are any longer necessary.
176*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000177long
178PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000179{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000180 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000181 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000182 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000183 /* Jump through some hoops for Alpha OSF/1 */
184 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000185#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
186 return (long) threadid;
187#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000188 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000189#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000190}
191
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192static void
193do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000194{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000195 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000196 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000197 if (no_cleanup)
198 _exit(0);
199 else
200 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000201 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000202}
203
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000204void
205PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000206{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000207 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000208}
209
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210void
211PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000212{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000213 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000214}
215
216#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000217static void
218do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000219{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000221 if (!initialized)
222 if (no_cleanup)
223 _exit(status);
224 else
225 exit(status);
226}
227
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228void
229PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000232}
233
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234void
235PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000238}
239#endif /* NO_EXIT_PROG */
240
Martin v. Löwiscc898662002-03-17 09:53:51 +0000241#ifdef USE_SEMAPHORES
242
243/*
244 * Lock support.
245 */
246
247PyThread_type_lock
248PyThread_allocate_lock(void)
249{
250 sem_t *lock;
251 int status, error = 0;
252
253 dprintf(("PyThread_allocate_lock called\n"));
254 if (!initialized)
255 PyThread_init_thread();
256
257 lock = (sem_t *)malloc(sizeof(sem_t));
258
259 if (lock) {
260 status = sem_init(lock,0,1);
261 CHECK_STATUS("sem_init");
262
263 if (error) {
264 free((void *)lock);
265 lock = NULL;
266 }
267 }
268
269 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
270 return (PyThread_type_lock)lock;
271}
272
273void
274PyThread_free_lock(PyThread_type_lock lock)
275{
276 sem_t *thelock = (sem_t *)lock;
277 int status, error = 0;
278
279 dprintf(("PyThread_free_lock(%p) called\n", lock));
280
281 if (!thelock)
282 return;
283
284 status = sem_destroy(thelock);
285 CHECK_STATUS("sem_destroy");
286
287 free((void *)thelock);
288}
289
290/*
291 * As of February 2002, Cygwin thread implementations mistakenly report error
292 * codes in the return value of the sem_ calls (like the pthread_ functions).
293 * Correct implementations return -1 and put the code in errno. This supports
294 * either.
295 */
296static int
297fix_status(int status)
298{
299 return (status == -1) ? errno : status;
300}
301
302int
303PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
304{
305 int success;
306 sem_t *thelock = (sem_t *)lock;
307 int status, error = 0;
308
309 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
310
311 do {
312 if (waitflag)
313 status = fix_status(sem_wait(thelock));
314 else
315 status = fix_status(sem_trywait(thelock));
316 } while (status == EINTR); /* Retry if interrupted by a signal */
317
318 if (waitflag) {
319 CHECK_STATUS("sem_wait");
320 } else if (status != EAGAIN) {
321 CHECK_STATUS("sem_trywait");
322 }
323
324 success = (status == 0) ? 1 : 0;
325
326 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
327 return success;
328}
329
330void
331PyThread_release_lock(PyThread_type_lock lock)
332{
333 sem_t *thelock = (sem_t *)lock;
334 int status, error = 0;
335
336 dprintf(("PyThread_release_lock(%p) called\n", lock));
337
338 status = sem_post(thelock);
339 CHECK_STATUS("sem_post");
340}
341
342#else /* USE_SEMAPHORES */
343
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000344/*
345 * Lock support.
346 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347PyThread_type_lock
348PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000349{
350 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000351 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000352
Guido van Rossum65d5b571998-12-21 19:32:43 +0000353 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000354 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000355 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000356
357 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000358 if (lock) {
Armin Rigoa6eb56c2005-09-20 18:07:47 +0000359 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000360 lock->locked = 0;
361
362 status = pthread_mutex_init(&lock->mut,
363 pthread_mutexattr_default);
364 CHECK_STATUS("pthread_mutex_init");
365
366 status = pthread_cond_init(&lock->lock_released,
367 pthread_condattr_default);
368 CHECK_STATUS("pthread_cond_init");
369
370 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000371 free((void *)lock);
372 lock = 0;
373 }
374 }
375
Fred Drakea44d3532000-06-30 15:01:00 +0000376 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000377 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000378}
379
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380void
381PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000382{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000383 pthread_lock *thelock = (pthread_lock *)lock;
384 int status, error = 0;
385
Fred Drakea44d3532000-06-30 15:01:00 +0000386 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000387
388 status = pthread_mutex_destroy( &thelock->mut );
389 CHECK_STATUS("pthread_mutex_destroy");
390
391 status = pthread_cond_destroy( &thelock->lock_released );
392 CHECK_STATUS("pthread_cond_destroy");
393
394 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000395}
396
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397int
398PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000399{
400 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000401 pthread_lock *thelock = (pthread_lock *)lock;
402 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000403
Fred Drakea44d3532000-06-30 15:01:00 +0000404 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000405
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000406 status = pthread_mutex_lock( &thelock->mut );
407 CHECK_STATUS("pthread_mutex_lock[1]");
408 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000409
410 if ( !success && waitflag ) {
411 /* continue trying until we get the lock */
412
413 /* mut must be locked by me -- part of the condition
414 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000415 while ( thelock->locked ) {
416 status = pthread_cond_wait(&thelock->lock_released,
417 &thelock->mut);
418 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000419 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000420 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000421 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000422 if (success) thelock->locked = 1;
423 status = pthread_mutex_unlock( &thelock->mut );
424 CHECK_STATUS("pthread_mutex_unlock[1]");
425
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000426 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000427 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000428 return success;
429}
430
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000431void
432PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000433{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000434 pthread_lock *thelock = (pthread_lock *)lock;
435 int status, error = 0;
436
Fred Drakea44d3532000-06-30 15:01:00 +0000437 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000438
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000439 status = pthread_mutex_lock( &thelock->mut );
440 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000441
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000442 thelock->locked = 0;
443
444 status = pthread_mutex_unlock( &thelock->mut );
445 CHECK_STATUS("pthread_mutex_unlock[3]");
446
447 /* wake up someone (anyone, if any) waiting on the lock */
448 status = pthread_cond_signal( &thelock->lock_released );
449 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000450}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000451
452#endif /* USE_SEMAPHORES */