blob: 6f6fde496b6b98f2e9448137c4b23b0fbf6bed10 [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
Guido van Rossum1a623111996-08-08 18:53:41 +000023
Guido van Rossumd6353e21997-05-13 17:51:13 +000024/* try to determine what version of the Pthread Standard is installed.
25 * this is important, since all sorts of parameter types changed from
26 * draft to draft and there are several (incompatible) drafts in
27 * common use. these macros are a start, at least.
28 * 12 May 1997 -- david arnold <davida@pobox.com>
29 */
30
31#if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
32/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
33# define PY_PTHREAD_D4
Martin v. Löwis779ffc02002-12-02 22:17:01 +000034# error Systems with PY_PTHREAD_D4 are unsupported. See README.
Guido van Rossumd6353e21997-05-13 17:51:13 +000035
36#elif defined(__osf__) && defined (__alpha)
37/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
38# if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
39# define PY_PTHREAD_D4
Martin v. Löwis779ffc02002-12-02 22:17:01 +000040# error Systems with PY_PTHREAD_D4 are unsupported. See README.
Guido van Rossumd6353e21997-05-13 17:51:13 +000041# else
42# define PY_PTHREAD_STD
43# endif
44
45#elif defined(_AIX)
Guido van Rossum1a623111996-08-08 18:53:41 +000046/* SCHED_BG_NP is defined if using AIX DCE pthreads
47 * but it is unsupported by AIX 4 pthreads. Default
48 * attributes for AIX 4 pthreads equal to NULL. For
49 * AIX DCE pthreads they should be left unchanged.
50 */
Guido van Rossumd6353e21997-05-13 17:51:13 +000051# if !defined(SCHED_BG_NP)
52# define PY_PTHREAD_STD
53# else
54# define PY_PTHREAD_D7
Martin v. Löwis779ffc02002-12-02 22:17:01 +000055# error Systems with PY_PTHREAD_D7 are unsupported. See README.
Guido van Rossumd6353e21997-05-13 17:51:13 +000056# endif
57
Guido van Rossum64f91051997-05-22 20:41:59 +000058#elif defined(__DGUX)
59# define PY_PTHREAD_D6
Martin v. Löwis779ffc02002-12-02 22:17:01 +000060# error Systems with PY_PTHREAD_D6 are unsupported. See README.
Guido van Rossum46ff1901997-06-02 22:25:45 +000061
Guido van Rossum532246e1998-05-14 21:01:27 +000062#elif defined(__hpux) && defined(_DECTHREADS_)
Guido van Rossum89df70b1998-05-07 13:28:23 +000063# define PY_PTHREAD_D4
Martin v. Löwis779ffc02002-12-02 22:17:01 +000064# error Systems with PY_PTHREAD_D4 are unsupported. See README.
Guido van Rossum89df70b1998-05-07 13:28:23 +000065
Guido van Rossum46ff1901997-06-02 22:25:45 +000066#else /* Default case */
67# define PY_PTHREAD_STD
68
Guido van Rossum1a623111996-08-08 18:53:41 +000069#endif
70
Jack Jansenc51395d2001-08-29 15:24:53 +000071#ifdef USE_GUSI
72/* The Macintosh GUSI I/O library sets the stackspace to
73** 20KB, much too low. We up it to 64K.
74*/
75#define THREAD_STACK_SIZE 0x10000
76#endif
77
Guido van Rossumd6353e21997-05-13 17:51:13 +000078
79/* set default attribute object for different versions */
80
81#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
82# define pthread_attr_default pthread_attr_default
83# define pthread_mutexattr_default pthread_mutexattr_default
84# define pthread_condattr_default pthread_condattr_default
Guido van Rossum64f91051997-05-22 20:41:59 +000085#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Guido van Rossumd6353e21997-05-13 17:51:13 +000086# define pthread_attr_default ((pthread_attr_t *)NULL)
87# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
88# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000089#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000090
Guido van Rossumd6353e21997-05-13 17:51:13 +000091
Martin v. Löwiscc898662002-03-17 09:53:51 +000092/* Whether or not to use semaphores directly rather than emulating them with
93 * mutexes and condition variables:
94 */
95#ifdef _POSIX_SEMAPHORES
96# define USE_SEMAPHORES
97#else
98# undef USE_SEMAPHORES
99#endif
100
101
Guido van Rossum80230992001-10-12 21:49:17 +0000102/* On platforms that don't use standard POSIX threads pthread_sigmask()
103 * isn't present. DEC threads uses sigprocmask() instead as do most
104 * other UNIX International compliant systems that don't have the full
105 * pthread implementation.
106 */
Martin v. Löwis69c0ff32001-10-15 14:34:42 +0000107#ifdef HAVE_PTHREAD_SIGMASK
Guido van Rossum80230992001-10-12 21:49:17 +0000108# define SET_THREAD_SIGMASK pthread_sigmask
109#else
110# define SET_THREAD_SIGMASK sigprocmask
111#endif
112
113
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000114/* A pthread mutex isn't sufficient to model the Python lock type
115 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
116 * following are undefined:
117 * -> a thread tries to lock a mutex it already has locked
118 * -> a thread tries to unlock a mutex locked by a different thread
119 * pthread mutexes are designed for serializing threads over short pieces
120 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000121 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000122 *
123 * The pthread_lock struct implements a Python lock as a "locked?" bit
124 * and a <condition, mutex> pair. In general, if the bit can be acquired
125 * instantly, it is, else the pair is used to block the thread until the
126 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000128
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000129typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000130 char locked; /* 0=unlocked, 1=locked */
131 /* a <cond, mutex> pair to handle an acquire of a locked lock */
132 pthread_cond_t lock_released;
133 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000134} pthread_lock;
135
Guido van Rossum9e46e561998-10-07 16:39:47 +0000136#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000137
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138/*
139 * Initialization.
140 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000141
142#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000143static
144void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000145{
146}
147
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000148static void
149PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000150{
151 /* DO AN INIT BY STARTING THE THREAD */
152 static int dummy = 0;
153 pthread_t thread1;
154 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
155 pthread_join(thread1, NULL);
156}
157
158#else /* !_HAVE_BSDI */
159
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160static void
161PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000162{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000163#if defined(_AIX) && defined(__GNUC__)
164 pthread_init();
165#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000166}
167
Guido van Rossum9e46e561998-10-07 16:39:47 +0000168#endif /* !_HAVE_BSDI */
169
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000170/*
171 * Thread support.
172 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000173
174
Guido van Rossum3c288632001-10-16 21:13:49 +0000175long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000176PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000177{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000178 pthread_t th;
Guido van Rossume944da81994-05-23 12:43:41 +0000179 int success;
Guido van Rossum80230992001-10-12 21:49:17 +0000180 sigset_t oldmask, newmask;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000181#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000182 pthread_attr_t attrs;
183#endif
Guido van Rossum65d5b571998-12-21 19:32:43 +0000184 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000185 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000186 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000187
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000188#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000189 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000190#endif
191#ifdef THREAD_STACK_SIZE
Jack Jansenc51395d2001-08-29 15:24:53 +0000192 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
193#endif
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000194#ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
195 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
196#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000197
198 /* Mask all signals in the current thread before creating the new
199 * thread. This causes the new thread to start with all signals
200 * blocked.
201 */
202 sigfillset(&newmask);
203 SET_THREAD_SIGMASK(SIG_BLOCK, &newmask, &oldmask);
204
Guido van Rossumd6353e21997-05-13 17:51:13 +0000205 success = pthread_create(&th,
206#if defined(PY_PTHREAD_D4)
207 pthread_attr_default,
208 (pthread_startroutine_t)func,
209 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000210#elif defined(PY_PTHREAD_D6)
211 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000212 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000213 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000214#elif defined(PY_PTHREAD_D7)
215 pthread_attr_default,
216 func,
217 arg
218#elif defined(PY_PTHREAD_STD)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000219#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000220 &attrs,
221#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000222 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000223#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000225 (void *)arg
226#endif
227 );
Guido van Rossum80230992001-10-12 21:49:17 +0000228
229 /* Restore signal mask for original thread */
230 SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);
231
Fred Drake03459a52001-11-09 16:00:41 +0000232#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000233 pthread_attr_destroy(&attrs);
234#endif
Guido van Rossum701f25e1999-03-15 20:27:53 +0000235 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000236#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000237 pthread_detach(&th);
238#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000239 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000240#endif
241 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000242#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
243 return (long) th;
244#else
245 return (long) *(long *) &th;
246#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000247}
248
Trent Mick635f6fb2000-08-23 21:33:05 +0000249/* XXX This implementation is considered (to quote Tim Peters) "inherently
250 hosed" because:
251 - It does not guanrantee the promise that a non-zero integer is returned.
252 - The cast to long is inherently unsafe.
253 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
254 latter return statement (for Alpha OSF/1) are any longer necessary.
255*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256long
257PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000258{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000259 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000260 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000262 /* Jump through some hoops for Alpha OSF/1 */
263 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000264#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
265 return (long) threadid;
266#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000267 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000268#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000269}
270
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271static void
272do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000273{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000274 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000275 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000276 if (no_cleanup)
277 _exit(0);
278 else
279 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000280 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000281}
282
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000283void
284PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000285{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000286 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000287}
288
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289void
290PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000291{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000292 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000293}
294
295#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000296static void
297do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000298{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000299 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000300 if (!initialized)
301 if (no_cleanup)
302 _exit(status);
303 else
304 exit(status);
305}
306
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307void
308PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000309{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000311}
312
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000313void
314PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000315{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000316 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000317}
318#endif /* NO_EXIT_PROG */
319
Martin v. Löwiscc898662002-03-17 09:53:51 +0000320#ifdef USE_SEMAPHORES
321
322/*
323 * Lock support.
324 */
325
326PyThread_type_lock
327PyThread_allocate_lock(void)
328{
329 sem_t *lock;
330 int status, error = 0;
331
332 dprintf(("PyThread_allocate_lock called\n"));
333 if (!initialized)
334 PyThread_init_thread();
335
336 lock = (sem_t *)malloc(sizeof(sem_t));
337
338 if (lock) {
339 status = sem_init(lock,0,1);
340 CHECK_STATUS("sem_init");
341
342 if (error) {
343 free((void *)lock);
344 lock = NULL;
345 }
346 }
347
348 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
349 return (PyThread_type_lock)lock;
350}
351
352void
353PyThread_free_lock(PyThread_type_lock lock)
354{
355 sem_t *thelock = (sem_t *)lock;
356 int status, error = 0;
357
358 dprintf(("PyThread_free_lock(%p) called\n", lock));
359
360 if (!thelock)
361 return;
362
363 status = sem_destroy(thelock);
364 CHECK_STATUS("sem_destroy");
365
366 free((void *)thelock);
367}
368
369/*
370 * As of February 2002, Cygwin thread implementations mistakenly report error
371 * codes in the return value of the sem_ calls (like the pthread_ functions).
372 * Correct implementations return -1 and put the code in errno. This supports
373 * either.
374 */
375static int
376fix_status(int status)
377{
378 return (status == -1) ? errno : status;
379}
380
381int
382PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
383{
384 int success;
385 sem_t *thelock = (sem_t *)lock;
386 int status, error = 0;
387
388 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
389
390 do {
391 if (waitflag)
392 status = fix_status(sem_wait(thelock));
393 else
394 status = fix_status(sem_trywait(thelock));
395 } while (status == EINTR); /* Retry if interrupted by a signal */
396
397 if (waitflag) {
398 CHECK_STATUS("sem_wait");
399 } else if (status != EAGAIN) {
400 CHECK_STATUS("sem_trywait");
401 }
402
403 success = (status == 0) ? 1 : 0;
404
405 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
406 return success;
407}
408
409void
410PyThread_release_lock(PyThread_type_lock lock)
411{
412 sem_t *thelock = (sem_t *)lock;
413 int status, error = 0;
414
415 dprintf(("PyThread_release_lock(%p) called\n", lock));
416
417 status = sem_post(thelock);
418 CHECK_STATUS("sem_post");
419}
420
421#else /* USE_SEMAPHORES */
422
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000423/*
424 * Lock support.
425 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000426PyThread_type_lock
427PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000428{
429 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000430 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000431
Guido van Rossum65d5b571998-12-21 19:32:43 +0000432 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000433 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000434 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000435
436 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000437 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000438 if (lock) {
439 lock->locked = 0;
440
441 status = pthread_mutex_init(&lock->mut,
442 pthread_mutexattr_default);
443 CHECK_STATUS("pthread_mutex_init");
444
445 status = pthread_cond_init(&lock->lock_released,
446 pthread_condattr_default);
447 CHECK_STATUS("pthread_cond_init");
448
449 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000450 free((void *)lock);
451 lock = 0;
452 }
453 }
454
Fred Drakea44d3532000-06-30 15:01:00 +0000455 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000456 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000457}
458
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000459void
460PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000461{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000462 pthread_lock *thelock = (pthread_lock *)lock;
463 int status, error = 0;
464
Fred Drakea44d3532000-06-30 15:01:00 +0000465 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000466
467 status = pthread_mutex_destroy( &thelock->mut );
468 CHECK_STATUS("pthread_mutex_destroy");
469
470 status = pthread_cond_destroy( &thelock->lock_released );
471 CHECK_STATUS("pthread_cond_destroy");
472
473 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000474}
475
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000476int
477PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000478{
479 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000480 pthread_lock *thelock = (pthread_lock *)lock;
481 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000482
Fred Drakea44d3532000-06-30 15:01:00 +0000483 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000484
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000485 status = pthread_mutex_lock( &thelock->mut );
486 CHECK_STATUS("pthread_mutex_lock[1]");
487 success = thelock->locked == 0;
488 if (success) thelock->locked = 1;
489 status = pthread_mutex_unlock( &thelock->mut );
490 CHECK_STATUS("pthread_mutex_unlock[1]");
491
492 if ( !success && waitflag ) {
493 /* continue trying until we get the lock */
494
495 /* mut must be locked by me -- part of the condition
496 * protocol */
497 status = pthread_mutex_lock( &thelock->mut );
498 CHECK_STATUS("pthread_mutex_lock[2]");
499 while ( thelock->locked ) {
500 status = pthread_cond_wait(&thelock->lock_released,
501 &thelock->mut);
502 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000503 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000504 thelock->locked = 1;
505 status = pthread_mutex_unlock( &thelock->mut );
506 CHECK_STATUS("pthread_mutex_unlock[2]");
507 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000508 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000509 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000510 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000511 return success;
512}
513
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000514void
515PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000516{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000517 pthread_lock *thelock = (pthread_lock *)lock;
518 int status, error = 0;
519
Fred Drakea44d3532000-06-30 15:01:00 +0000520 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000521
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000522 status = pthread_mutex_lock( &thelock->mut );
523 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000524
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000525 thelock->locked = 0;
526
527 status = pthread_mutex_unlock( &thelock->mut );
528 CHECK_STATUS("pthread_mutex_unlock[3]");
529
530 /* wake up someone (anyone, if any) waiting on the lock */
531 status = pthread_cond_signal( &thelock->lock_released );
532 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000533}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000534
535#endif /* USE_SEMAPHORES */