blob: 9e82ed054344188a467f484e26b1a15be0606053 [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 Rossum532246e1998-05-14 21:01:27 +000058#elif defined(__hpux) && defined(_DECTHREADS_)
Guido van Rossum89df70b1998-05-07 13:28:23 +000059# define PY_PTHREAD_D4
Martin v. Löwis779ffc02002-12-02 22:17:01 +000060# error Systems with PY_PTHREAD_D4 are unsupported. See README.
Guido van Rossum89df70b1998-05-07 13:28:23 +000061
Guido van Rossum46ff1901997-06-02 22:25:45 +000062#else /* Default case */
63# define PY_PTHREAD_STD
64
Guido van Rossum1a623111996-08-08 18:53:41 +000065#endif
66
Guido van Rossumd6353e21997-05-13 17:51:13 +000067/* set default attribute object for different versions */
68
69#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
Martin v. Löwisb0233812002-12-11 13:12:30 +000070#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000071# define pthread_attr_default pthread_attr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000072#endif
73#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000074# define pthread_mutexattr_default pthread_mutexattr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000075#endif
76#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000077# define pthread_condattr_default pthread_condattr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000078#endif
Skip Montanaro4d474be2004-01-17 00:29:32 +000079#elif defined(PY_PTHREAD_STD)
Martin v. Löwisb0233812002-12-11 13:12:30 +000080#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000081# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000082#endif
83#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000084# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000085#endif
86#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000087# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000088#endif
Martin v. Löwisb0233812002-12-11 13:12:30 +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 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000095#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000096# 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 */
Jason Tishlerfac083d2003-07-22 15:20:49 +0000107#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_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;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000179 int status;
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
Martin v. Löwis910ae622003-04-19 07:44:52 +0000205 status = pthread_create(&th,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000206#if defined(PY_PTHREAD_D4)
207 pthread_attr_default,
208 (pthread_startroutine_t)func,
209 (pthread_addr_t)arg
210#elif defined(PY_PTHREAD_D7)
211 pthread_attr_default,
212 func,
213 arg
214#elif defined(PY_PTHREAD_STD)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000215#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000216 &attrs,
217#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000218 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000219#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000221 (void *)arg
222#endif
223 );
Guido van Rossum80230992001-10-12 21:49:17 +0000224
225 /* Restore signal mask for original thread */
226 SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);
227
Fred Drake03459a52001-11-09 16:00:41 +0000228#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000229 pthread_attr_destroy(&attrs);
230#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000231 if (status != 0)
232 return -1;
233
Skip Montanaro4d474be2004-01-17 00:29:32 +0000234#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
Martin v. Löwis910ae622003-04-19 07:44:52 +0000235 pthread_detach(&th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000236#elif defined(PY_PTHREAD_STD)
Martin v. Löwis910ae622003-04-19 07:44:52 +0000237 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000238#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000239
Guido van Rossum3c288632001-10-16 21:13:49 +0000240#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
241 return (long) th;
242#else
243 return (long) *(long *) &th;
244#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000245}
246
Trent Mick635f6fb2000-08-23 21:33:05 +0000247/* XXX This implementation is considered (to quote Tim Peters) "inherently
248 hosed" because:
249 - It does not guanrantee the promise that a non-zero integer is returned.
250 - The cast to long is inherently unsafe.
251 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
252 latter return statement (for Alpha OSF/1) are any longer necessary.
253*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254long
255PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000256{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000257 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000258 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000260 /* Jump through some hoops for Alpha OSF/1 */
261 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000262#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
263 return (long) threadid;
264#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000265 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000266#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000267}
268
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269static void
270do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000271{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000273 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000274 if (no_cleanup)
275 _exit(0);
276 else
277 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000278 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000279}
280
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281void
282PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000283{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000284 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000285}
286
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287void
288PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000289{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000290 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000291}
292
293#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294static void
295do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000296{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000298 if (!initialized)
299 if (no_cleanup)
300 _exit(status);
301 else
302 exit(status);
303}
304
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305void
306PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000307{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000308 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000309}
310
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000311void
312PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000313{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000314 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000315}
316#endif /* NO_EXIT_PROG */
317
Martin v. Löwiscc898662002-03-17 09:53:51 +0000318#ifdef USE_SEMAPHORES
319
320/*
321 * Lock support.
322 */
323
324PyThread_type_lock
325PyThread_allocate_lock(void)
326{
327 sem_t *lock;
328 int status, error = 0;
329
330 dprintf(("PyThread_allocate_lock called\n"));
331 if (!initialized)
332 PyThread_init_thread();
333
334 lock = (sem_t *)malloc(sizeof(sem_t));
335
336 if (lock) {
337 status = sem_init(lock,0,1);
338 CHECK_STATUS("sem_init");
339
340 if (error) {
341 free((void *)lock);
342 lock = NULL;
343 }
344 }
345
346 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
347 return (PyThread_type_lock)lock;
348}
349
350void
351PyThread_free_lock(PyThread_type_lock lock)
352{
353 sem_t *thelock = (sem_t *)lock;
354 int status, error = 0;
355
356 dprintf(("PyThread_free_lock(%p) called\n", lock));
357
358 if (!thelock)
359 return;
360
361 status = sem_destroy(thelock);
362 CHECK_STATUS("sem_destroy");
363
364 free((void *)thelock);
365}
366
367/*
368 * As of February 2002, Cygwin thread implementations mistakenly report error
369 * codes in the return value of the sem_ calls (like the pthread_ functions).
370 * Correct implementations return -1 and put the code in errno. This supports
371 * either.
372 */
373static int
374fix_status(int status)
375{
376 return (status == -1) ? errno : status;
377}
378
379int
380PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
381{
382 int success;
383 sem_t *thelock = (sem_t *)lock;
384 int status, error = 0;
385
386 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
387
388 do {
389 if (waitflag)
390 status = fix_status(sem_wait(thelock));
391 else
392 status = fix_status(sem_trywait(thelock));
393 } while (status == EINTR); /* Retry if interrupted by a signal */
394
395 if (waitflag) {
396 CHECK_STATUS("sem_wait");
397 } else if (status != EAGAIN) {
398 CHECK_STATUS("sem_trywait");
399 }
400
401 success = (status == 0) ? 1 : 0;
402
403 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
404 return success;
405}
406
407void
408PyThread_release_lock(PyThread_type_lock lock)
409{
410 sem_t *thelock = (sem_t *)lock;
411 int status, error = 0;
412
413 dprintf(("PyThread_release_lock(%p) called\n", lock));
414
415 status = sem_post(thelock);
416 CHECK_STATUS("sem_post");
417}
418
419#else /* USE_SEMAPHORES */
420
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000421/*
422 * Lock support.
423 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000424PyThread_type_lock
425PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000426{
427 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000428 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000429
Guido van Rossum65d5b571998-12-21 19:32:43 +0000430 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000431 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000432 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000433
434 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000435 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000436 if (lock) {
437 lock->locked = 0;
438
439 status = pthread_mutex_init(&lock->mut,
440 pthread_mutexattr_default);
441 CHECK_STATUS("pthread_mutex_init");
442
443 status = pthread_cond_init(&lock->lock_released,
444 pthread_condattr_default);
445 CHECK_STATUS("pthread_cond_init");
446
447 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000448 free((void *)lock);
449 lock = 0;
450 }
451 }
452
Fred Drakea44d3532000-06-30 15:01:00 +0000453 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000454 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000455}
456
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000457void
458PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000459{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000460 pthread_lock *thelock = (pthread_lock *)lock;
461 int status, error = 0;
462
Fred Drakea44d3532000-06-30 15:01:00 +0000463 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000464
465 status = pthread_mutex_destroy( &thelock->mut );
466 CHECK_STATUS("pthread_mutex_destroy");
467
468 status = pthread_cond_destroy( &thelock->lock_released );
469 CHECK_STATUS("pthread_cond_destroy");
470
471 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000472}
473
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000474int
475PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000476{
477 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000478 pthread_lock *thelock = (pthread_lock *)lock;
479 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000480
Fred Drakea44d3532000-06-30 15:01:00 +0000481 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000482
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000483 status = pthread_mutex_lock( &thelock->mut );
484 CHECK_STATUS("pthread_mutex_lock[1]");
485 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000486
487 if ( !success && waitflag ) {
488 /* continue trying until we get the lock */
489
490 /* mut must be locked by me -- part of the condition
491 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000492 while ( thelock->locked ) {
493 status = pthread_cond_wait(&thelock->lock_released,
494 &thelock->mut);
495 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000496 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000497 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000498 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000499 if (success) thelock->locked = 1;
500 status = pthread_mutex_unlock( &thelock->mut );
501 CHECK_STATUS("pthread_mutex_unlock[1]");
502
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000503 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000504 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000505 return success;
506}
507
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000508void
509PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000510{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000511 pthread_lock *thelock = (pthread_lock *)lock;
512 int status, error = 0;
513
Fred Drakea44d3532000-06-30 15:01:00 +0000514 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000515
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000516 status = pthread_mutex_lock( &thelock->mut );
517 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000518
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000519 thelock->locked = 0;
520
521 status = pthread_mutex_unlock( &thelock->mut );
522 CHECK_STATUS("pthread_mutex_unlock[3]");
523
524 /* wake up someone (anyone, if any) waiting on the lock */
525 status = pthread_cond_signal( &thelock->lock_released );
526 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000527}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000528
529#endif /* USE_SEMAPHORES */