blob: 2e594fe922e16a679a74f217418105fd4feb76c8 [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)
Martin v. Löwisb0233812002-12-11 13:12:30 +000082#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000083# define pthread_attr_default pthread_attr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000084#endif
85#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000086# define pthread_mutexattr_default pthread_mutexattr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000087#endif
88#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000089# define pthread_condattr_default pthread_condattr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000090#endif
Guido van Rossum64f91051997-05-22 20:41:59 +000091#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Martin v. Löwisb0233812002-12-11 13:12:30 +000092#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000093# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000094#endif
95#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000096# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000097#endif
98#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000099# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +0000100#endif
Martin v. Löwisb0233812002-12-11 13:12:30 +0000101#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000102
Guido van Rossumd6353e21997-05-13 17:51:13 +0000103
Martin v. Löwiscc898662002-03-17 09:53:51 +0000104/* Whether or not to use semaphores directly rather than emulating them with
105 * mutexes and condition variables:
106 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +0000107#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +0000108# define USE_SEMAPHORES
109#else
110# undef USE_SEMAPHORES
111#endif
112
113
Guido van Rossum80230992001-10-12 21:49:17 +0000114/* On platforms that don't use standard POSIX threads pthread_sigmask()
115 * isn't present. DEC threads uses sigprocmask() instead as do most
116 * other UNIX International compliant systems that don't have the full
117 * pthread implementation.
118 */
Martin v. Löwis69c0ff32001-10-15 14:34:42 +0000119#ifdef HAVE_PTHREAD_SIGMASK
Guido van Rossum80230992001-10-12 21:49:17 +0000120# define SET_THREAD_SIGMASK pthread_sigmask
121#else
122# define SET_THREAD_SIGMASK sigprocmask
123#endif
124
125
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000126/* A pthread mutex isn't sufficient to model the Python lock type
127 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
128 * following are undefined:
129 * -> a thread tries to lock a mutex it already has locked
130 * -> a thread tries to unlock a mutex locked by a different thread
131 * pthread mutexes are designed for serializing threads over short pieces
132 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000133 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000134 *
135 * The pthread_lock struct implements a Python lock as a "locked?" bit
136 * and a <condition, mutex> pair. In general, if the bit can be acquired
137 * instantly, it is, else the pair is used to block the thread until the
138 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000139 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000140
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000141typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000142 char locked; /* 0=unlocked, 1=locked */
143 /* a <cond, mutex> pair to handle an acquire of a locked lock */
144 pthread_cond_t lock_released;
145 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146} pthread_lock;
147
Guido van Rossum9e46e561998-10-07 16:39:47 +0000148#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000149
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000150/*
151 * Initialization.
152 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000153
154#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155static
156void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000157{
158}
159
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160static void
161PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000162{
163 /* DO AN INIT BY STARTING THE THREAD */
164 static int dummy = 0;
165 pthread_t thread1;
166 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
167 pthread_join(thread1, NULL);
168}
169
170#else /* !_HAVE_BSDI */
171
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000172static void
173PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000174{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000175#if defined(_AIX) && defined(__GNUC__)
176 pthread_init();
177#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000178}
179
Guido van Rossum9e46e561998-10-07 16:39:47 +0000180#endif /* !_HAVE_BSDI */
181
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000182/*
183 * Thread support.
184 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000185
186
Guido van Rossum3c288632001-10-16 21:13:49 +0000187long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000188PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000189{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000190 pthread_t th;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000191 int status;
Guido van Rossum80230992001-10-12 21:49:17 +0000192 sigset_t oldmask, newmask;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000193#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000194 pthread_attr_t attrs;
195#endif
Guido van Rossum65d5b571998-12-21 19:32:43 +0000196 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000197 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000199
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000200#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000201 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000202#endif
203#ifdef THREAD_STACK_SIZE
Jack Jansenc51395d2001-08-29 15:24:53 +0000204 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
205#endif
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000206#ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
207 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
208#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000209
210 /* Mask all signals in the current thread before creating the new
211 * thread. This causes the new thread to start with all signals
212 * blocked.
213 */
214 sigfillset(&newmask);
215 SET_THREAD_SIGMASK(SIG_BLOCK, &newmask, &oldmask);
216
Martin v. Löwis910ae622003-04-19 07:44:52 +0000217 status = pthread_create(&th,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000218#if defined(PY_PTHREAD_D4)
219 pthread_attr_default,
220 (pthread_startroutine_t)func,
221 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000222#elif defined(PY_PTHREAD_D6)
223 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000225 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000226#elif defined(PY_PTHREAD_D7)
227 pthread_attr_default,
228 func,
229 arg
230#elif defined(PY_PTHREAD_STD)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000231#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000232 &attrs,
233#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000234 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000235#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000237 (void *)arg
238#endif
239 );
Guido van Rossum80230992001-10-12 21:49:17 +0000240
241 /* Restore signal mask for original thread */
242 SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);
243
Fred Drake03459a52001-11-09 16:00:41 +0000244#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000245 pthread_attr_destroy(&attrs);
246#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000247 if (status != 0)
248 return -1;
249
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000250#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Martin v. Löwis910ae622003-04-19 07:44:52 +0000251 pthread_detach(&th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000252#elif defined(PY_PTHREAD_STD)
Martin v. Löwis910ae622003-04-19 07:44:52 +0000253 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000254#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000255
Guido van Rossum3c288632001-10-16 21:13:49 +0000256#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
257 return (long) th;
258#else
259 return (long) *(long *) &th;
260#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000261}
262
Trent Mick635f6fb2000-08-23 21:33:05 +0000263/* XXX This implementation is considered (to quote Tim Peters) "inherently
264 hosed" because:
265 - It does not guanrantee the promise that a non-zero integer is returned.
266 - The cast to long is inherently unsafe.
267 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
268 latter return statement (for Alpha OSF/1) are any longer necessary.
269*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000270long
271PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000272{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000273 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000274 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000276 /* Jump through some hoops for Alpha OSF/1 */
277 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000278#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
279 return (long) threadid;
280#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000281 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000282#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000283}
284
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000285static void
286do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000287{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000288 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000289 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000290 if (no_cleanup)
291 _exit(0);
292 else
293 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000294 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000295}
296
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297void
298PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000299{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000300 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000301}
302
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303void
304PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000305{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000306 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000307}
308
309#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000310static void
311do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000312{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000313 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000314 if (!initialized)
315 if (no_cleanup)
316 _exit(status);
317 else
318 exit(status);
319}
320
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321void
322PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000323{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000324 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000325}
326
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327void
328PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000329{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000330 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000331}
332#endif /* NO_EXIT_PROG */
333
Martin v. Löwiscc898662002-03-17 09:53:51 +0000334#ifdef USE_SEMAPHORES
335
336/*
337 * Lock support.
338 */
339
340PyThread_type_lock
341PyThread_allocate_lock(void)
342{
343 sem_t *lock;
344 int status, error = 0;
345
346 dprintf(("PyThread_allocate_lock called\n"));
347 if (!initialized)
348 PyThread_init_thread();
349
350 lock = (sem_t *)malloc(sizeof(sem_t));
351
352 if (lock) {
353 status = sem_init(lock,0,1);
354 CHECK_STATUS("sem_init");
355
356 if (error) {
357 free((void *)lock);
358 lock = NULL;
359 }
360 }
361
362 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
363 return (PyThread_type_lock)lock;
364}
365
366void
367PyThread_free_lock(PyThread_type_lock lock)
368{
369 sem_t *thelock = (sem_t *)lock;
370 int status, error = 0;
371
372 dprintf(("PyThread_free_lock(%p) called\n", lock));
373
374 if (!thelock)
375 return;
376
377 status = sem_destroy(thelock);
378 CHECK_STATUS("sem_destroy");
379
380 free((void *)thelock);
381}
382
383/*
384 * As of February 2002, Cygwin thread implementations mistakenly report error
385 * codes in the return value of the sem_ calls (like the pthread_ functions).
386 * Correct implementations return -1 and put the code in errno. This supports
387 * either.
388 */
389static int
390fix_status(int status)
391{
392 return (status == -1) ? errno : status;
393}
394
395int
396PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
397{
398 int success;
399 sem_t *thelock = (sem_t *)lock;
400 int status, error = 0;
401
402 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
403
404 do {
405 if (waitflag)
406 status = fix_status(sem_wait(thelock));
407 else
408 status = fix_status(sem_trywait(thelock));
409 } while (status == EINTR); /* Retry if interrupted by a signal */
410
411 if (waitflag) {
412 CHECK_STATUS("sem_wait");
413 } else if (status != EAGAIN) {
414 CHECK_STATUS("sem_trywait");
415 }
416
417 success = (status == 0) ? 1 : 0;
418
419 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
420 return success;
421}
422
423void
424PyThread_release_lock(PyThread_type_lock lock)
425{
426 sem_t *thelock = (sem_t *)lock;
427 int status, error = 0;
428
429 dprintf(("PyThread_release_lock(%p) called\n", lock));
430
431 status = sem_post(thelock);
432 CHECK_STATUS("sem_post");
433}
434
435#else /* USE_SEMAPHORES */
436
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437/*
438 * Lock support.
439 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440PyThread_type_lock
441PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000442{
443 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000444 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000445
Guido van Rossum65d5b571998-12-21 19:32:43 +0000446 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000447 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000448 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000449
450 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000451 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000452 if (lock) {
453 lock->locked = 0;
454
455 status = pthread_mutex_init(&lock->mut,
456 pthread_mutexattr_default);
457 CHECK_STATUS("pthread_mutex_init");
458
459 status = pthread_cond_init(&lock->lock_released,
460 pthread_condattr_default);
461 CHECK_STATUS("pthread_cond_init");
462
463 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000464 free((void *)lock);
465 lock = 0;
466 }
467 }
468
Fred Drakea44d3532000-06-30 15:01:00 +0000469 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000470 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000471}
472
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000473void
474PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000475{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000476 pthread_lock *thelock = (pthread_lock *)lock;
477 int status, error = 0;
478
Fred Drakea44d3532000-06-30 15:01:00 +0000479 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000480
481 status = pthread_mutex_destroy( &thelock->mut );
482 CHECK_STATUS("pthread_mutex_destroy");
483
484 status = pthread_cond_destroy( &thelock->lock_released );
485 CHECK_STATUS("pthread_cond_destroy");
486
487 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000488}
489
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000490int
491PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000492{
493 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000494 pthread_lock *thelock = (pthread_lock *)lock;
495 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000496
Fred Drakea44d3532000-06-30 15:01:00 +0000497 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000498
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000499 status = pthread_mutex_lock( &thelock->mut );
500 CHECK_STATUS("pthread_mutex_lock[1]");
501 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000502
503 if ( !success && waitflag ) {
504 /* continue trying until we get the lock */
505
506 /* mut must be locked by me -- part of the condition
507 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000508 while ( thelock->locked ) {
509 status = pthread_cond_wait(&thelock->lock_released,
510 &thelock->mut);
511 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000512 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000513 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000514 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000515 if (success) thelock->locked = 1;
516 status = pthread_mutex_unlock( &thelock->mut );
517 CHECK_STATUS("pthread_mutex_unlock[1]");
518
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000519 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000520 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000521 return success;
522}
523
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000524void
525PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000526{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000527 pthread_lock *thelock = (pthread_lock *)lock;
528 int status, error = 0;
529
Fred Drakea44d3532000-06-30 15:01:00 +0000530 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000531
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000532 status = pthread_mutex_lock( &thelock->mut );
533 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000534
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000535 thelock->locked = 0;
536
537 status = pthread_mutex_unlock( &thelock->mut );
538 CHECK_STATUS("pthread_mutex_unlock[3]");
539
540 /* wake up someone (anyone, if any) waiting on the lock */
541 status = pthread_cond_signal( &thelock->lock_released );
542 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000543}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000544
545#endif /* USE_SEMAPHORES */