blob: e30982fc4f8be5c8977ed2841ac84bbc498f1940 [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;
Guido van Rossume944da81994-05-23 12:43:41 +0000191 int success;
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
Guido van Rossumd6353e21997-05-13 17:51:13 +0000217 success = pthread_create(&th,
218#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
Guido van Rossum701f25e1999-03-15 20:27:53 +0000247 if (success == 0) {
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000248#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Guido van Rossumd6353e21997-05-13 17:51:13 +0000249 pthread_detach(&th);
250#elif defined(PY_PTHREAD_STD)
Guido van Rossumf4806c21997-04-30 19:59:22 +0000251 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000252#endif
253 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000254#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
255 return (long) th;
256#else
257 return (long) *(long *) &th;
258#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000259}
260
Trent Mick635f6fb2000-08-23 21:33:05 +0000261/* XXX This implementation is considered (to quote Tim Peters) "inherently
262 hosed" because:
263 - It does not guanrantee the promise that a non-zero integer is returned.
264 - The cast to long is inherently unsafe.
265 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
266 latter return statement (for Alpha OSF/1) are any longer necessary.
267*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000268long
269PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000270{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000271 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000272 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000273 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000274 /* Jump through some hoops for Alpha OSF/1 */
275 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000276#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
277 return (long) threadid;
278#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000279 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000280#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000281}
282
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000283static void
284do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000285{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000286 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000287 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000288 if (no_cleanup)
289 _exit(0);
290 else
291 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000292 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000293}
294
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000295void
296PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000297{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000298 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000299}
300
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301void
302PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000303{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000304 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000305}
306
307#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308static void
309do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000310{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000311 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000312 if (!initialized)
313 if (no_cleanup)
314 _exit(status);
315 else
316 exit(status);
317}
318
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000319void
320PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000321{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000322 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000323}
324
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325void
326PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000327{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000328 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000329}
330#endif /* NO_EXIT_PROG */
331
Martin v. Löwiscc898662002-03-17 09:53:51 +0000332#ifdef USE_SEMAPHORES
333
334/*
335 * Lock support.
336 */
337
338PyThread_type_lock
339PyThread_allocate_lock(void)
340{
341 sem_t *lock;
342 int status, error = 0;
343
344 dprintf(("PyThread_allocate_lock called\n"));
345 if (!initialized)
346 PyThread_init_thread();
347
348 lock = (sem_t *)malloc(sizeof(sem_t));
349
350 if (lock) {
351 status = sem_init(lock,0,1);
352 CHECK_STATUS("sem_init");
353
354 if (error) {
355 free((void *)lock);
356 lock = NULL;
357 }
358 }
359
360 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
361 return (PyThread_type_lock)lock;
362}
363
364void
365PyThread_free_lock(PyThread_type_lock lock)
366{
367 sem_t *thelock = (sem_t *)lock;
368 int status, error = 0;
369
370 dprintf(("PyThread_free_lock(%p) called\n", lock));
371
372 if (!thelock)
373 return;
374
375 status = sem_destroy(thelock);
376 CHECK_STATUS("sem_destroy");
377
378 free((void *)thelock);
379}
380
381/*
382 * As of February 2002, Cygwin thread implementations mistakenly report error
383 * codes in the return value of the sem_ calls (like the pthread_ functions).
384 * Correct implementations return -1 and put the code in errno. This supports
385 * either.
386 */
387static int
388fix_status(int status)
389{
390 return (status == -1) ? errno : status;
391}
392
393int
394PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
395{
396 int success;
397 sem_t *thelock = (sem_t *)lock;
398 int status, error = 0;
399
400 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
401
402 do {
403 if (waitflag)
404 status = fix_status(sem_wait(thelock));
405 else
406 status = fix_status(sem_trywait(thelock));
407 } while (status == EINTR); /* Retry if interrupted by a signal */
408
409 if (waitflag) {
410 CHECK_STATUS("sem_wait");
411 } else if (status != EAGAIN) {
412 CHECK_STATUS("sem_trywait");
413 }
414
415 success = (status == 0) ? 1 : 0;
416
417 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
418 return success;
419}
420
421void
422PyThread_release_lock(PyThread_type_lock lock)
423{
424 sem_t *thelock = (sem_t *)lock;
425 int status, error = 0;
426
427 dprintf(("PyThread_release_lock(%p) called\n", lock));
428
429 status = sem_post(thelock);
430 CHECK_STATUS("sem_post");
431}
432
433#else /* USE_SEMAPHORES */
434
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000435/*
436 * Lock support.
437 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000438PyThread_type_lock
439PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000440{
441 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000442 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000443
Guido van Rossum65d5b571998-12-21 19:32:43 +0000444 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000445 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000446 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000447
448 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000449 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000450 if (lock) {
451 lock->locked = 0;
452
453 status = pthread_mutex_init(&lock->mut,
454 pthread_mutexattr_default);
455 CHECK_STATUS("pthread_mutex_init");
456
457 status = pthread_cond_init(&lock->lock_released,
458 pthread_condattr_default);
459 CHECK_STATUS("pthread_cond_init");
460
461 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000462 free((void *)lock);
463 lock = 0;
464 }
465 }
466
Fred Drakea44d3532000-06-30 15:01:00 +0000467 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000468 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000469}
470
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000471void
472PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000473{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000474 pthread_lock *thelock = (pthread_lock *)lock;
475 int status, error = 0;
476
Fred Drakea44d3532000-06-30 15:01:00 +0000477 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000478
479 status = pthread_mutex_destroy( &thelock->mut );
480 CHECK_STATUS("pthread_mutex_destroy");
481
482 status = pthread_cond_destroy( &thelock->lock_released );
483 CHECK_STATUS("pthread_cond_destroy");
484
485 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000486}
487
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000488int
489PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000490{
491 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000492 pthread_lock *thelock = (pthread_lock *)lock;
493 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000494
Fred Drakea44d3532000-06-30 15:01:00 +0000495 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000496
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000497 status = pthread_mutex_lock( &thelock->mut );
498 CHECK_STATUS("pthread_mutex_lock[1]");
499 success = thelock->locked == 0;
500 if (success) thelock->locked = 1;
501 status = pthread_mutex_unlock( &thelock->mut );
502 CHECK_STATUS("pthread_mutex_unlock[1]");
503
504 if ( !success && waitflag ) {
505 /* continue trying until we get the lock */
506
507 /* mut must be locked by me -- part of the condition
508 * protocol */
509 status = pthread_mutex_lock( &thelock->mut );
510 CHECK_STATUS("pthread_mutex_lock[2]");
511 while ( thelock->locked ) {
512 status = pthread_cond_wait(&thelock->lock_released,
513 &thelock->mut);
514 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000515 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000516 thelock->locked = 1;
517 status = pthread_mutex_unlock( &thelock->mut );
518 CHECK_STATUS("pthread_mutex_unlock[2]");
519 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000520 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000521 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000522 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000523 return success;
524}
525
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526void
527PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000528{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000529 pthread_lock *thelock = (pthread_lock *)lock;
530 int status, error = 0;
531
Fred Drakea44d3532000-06-30 15:01:00 +0000532 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000533
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000534 status = pthread_mutex_lock( &thelock->mut );
535 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000536
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000537 thelock->locked = 0;
538
539 status = pthread_mutex_unlock( &thelock->mut );
540 CHECK_STATUS("pthread_mutex_unlock[3]");
541
542 /* wake up someone (anyone, if any) waiting on the lock */
543 status = pthread_cond_signal( &thelock->lock_released );
544 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000545}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000546
547#endif /* USE_SEMAPHORES */