blob: cb03a9ba6e8806d266a1f6c49478299b7aa49157 [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
Guido van Rossumd6353e21997-05-13 17:51:13 +000071/* set default attribute object for different versions */
72
73#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
Martin v. Löwisb0233812002-12-11 13:12:30 +000074#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000075# define pthread_attr_default pthread_attr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000076#endif
77#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000078# define pthread_mutexattr_default pthread_mutexattr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000079#endif
80#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000081# define pthread_condattr_default pthread_condattr_default
Martin v. Löwisb0233812002-12-11 13:12:30 +000082#endif
Guido van Rossum64f91051997-05-22 20:41:59 +000083#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
Martin v. Löwisb0233812002-12-11 13:12:30 +000084#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000085# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000086#endif
87#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000088# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000089#endif
90#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000091# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000092#endif
Martin v. Löwisb0233812002-12-11 13:12:30 +000093#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000094
Guido van Rossumd6353e21997-05-13 17:51:13 +000095
Martin v. Löwiscc898662002-03-17 09:53:51 +000096/* Whether or not to use semaphores directly rather than emulating them with
97 * mutexes and condition variables:
98 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000099#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +0000100# define USE_SEMAPHORES
101#else
102# undef USE_SEMAPHORES
103#endif
104
105
Guido van Rossum80230992001-10-12 21:49:17 +0000106/* On platforms that don't use standard POSIX threads pthread_sigmask()
107 * isn't present. DEC threads uses sigprocmask() instead as do most
108 * other UNIX International compliant systems that don't have the full
109 * pthread implementation.
110 */
Jason Tishlerfac083d2003-07-22 15:20:49 +0000111#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +0000112# define SET_THREAD_SIGMASK pthread_sigmask
113#else
114# define SET_THREAD_SIGMASK sigprocmask
115#endif
116
117
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000118/* A pthread mutex isn't sufficient to model the Python lock type
119 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
120 * following are undefined:
121 * -> a thread tries to lock a mutex it already has locked
122 * -> a thread tries to unlock a mutex locked by a different thread
123 * pthread mutexes are designed for serializing threads over short pieces
124 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000125 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000126 *
127 * The pthread_lock struct implements a Python lock as a "locked?" bit
128 * and a <condition, mutex> pair. In general, if the bit can be acquired
129 * instantly, it is, else the pair is used to block the thread until the
130 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000131 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000132
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000133typedef struct {
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000134 char locked; /* 0=unlocked, 1=locked */
135 /* a <cond, mutex> pair to handle an acquire of a locked lock */
136 pthread_cond_t lock_released;
137 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138} pthread_lock;
139
Guido van Rossum9e46e561998-10-07 16:39:47 +0000140#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000141
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000142/*
143 * Initialization.
144 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000145
146#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147static
148void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000149{
150}
151
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152static void
153PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000154{
155 /* DO AN INIT BY STARTING THE THREAD */
156 static int dummy = 0;
157 pthread_t thread1;
158 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
159 pthread_join(thread1, NULL);
160}
161
162#else /* !_HAVE_BSDI */
163
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000164static void
165PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000166{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000167#if defined(_AIX) && defined(__GNUC__)
168 pthread_init();
169#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000170}
171
Guido van Rossum9e46e561998-10-07 16:39:47 +0000172#endif /* !_HAVE_BSDI */
173
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000174/*
175 * Thread support.
176 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000177
178
Guido van Rossum3c288632001-10-16 21:13:49 +0000179long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000180PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000181{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000182 pthread_t th;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000183 int status;
Guido van Rossum80230992001-10-12 21:49:17 +0000184 sigset_t oldmask, newmask;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000185#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000186 pthread_attr_t attrs;
187#endif
Guido van Rossum65d5b571998-12-21 19:32:43 +0000188 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000189 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000190 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000191
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000192#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000193 pthread_attr_init(&attrs);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000194#endif
195#ifdef THREAD_STACK_SIZE
Jack Jansenc51395d2001-08-29 15:24:53 +0000196 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
197#endif
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000198#ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
199 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
200#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000201
202 /* Mask all signals in the current thread before creating the new
203 * thread. This causes the new thread to start with all signals
204 * blocked.
205 */
206 sigfillset(&newmask);
207 SET_THREAD_SIGMASK(SIG_BLOCK, &newmask, &oldmask);
208
Martin v. Löwis910ae622003-04-19 07:44:52 +0000209 status = pthread_create(&th,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000210#if defined(PY_PTHREAD_D4)
211 pthread_attr_default,
212 (pthread_startroutine_t)func,
213 (pthread_addr_t)arg
Guido van Rossum64f91051997-05-22 20:41:59 +0000214#elif defined(PY_PTHREAD_D6)
215 pthread_attr_default,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216 (void* (*)(void *))func,
Guido van Rossum64f91051997-05-22 20:41:59 +0000217 arg
Guido van Rossumd6353e21997-05-13 17:51:13 +0000218#elif defined(PY_PTHREAD_D7)
219 pthread_attr_default,
220 func,
221 arg
222#elif defined(PY_PTHREAD_STD)
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000223#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000224 &attrs,
225#else
Guido van Rossumd6353e21997-05-13 17:51:13 +0000226 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000227#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228 (void* (*)(void *))func,
Guido van Rossumd6353e21997-05-13 17:51:13 +0000229 (void *)arg
230#endif
231 );
Guido van Rossum80230992001-10-12 21:49:17 +0000232
233 /* Restore signal mask for original thread */
234 SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);
235
Fred Drake03459a52001-11-09 16:00:41 +0000236#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Jack Jansenc51395d2001-08-29 15:24:53 +0000237 pthread_attr_destroy(&attrs);
238#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000239 if (status != 0)
240 return -1;
241
Guido van Rossuma74d0e41998-09-04 13:38:32 +0000242#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
Martin v. Löwis910ae622003-04-19 07:44:52 +0000243 pthread_detach(&th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000244#elif defined(PY_PTHREAD_STD)
Martin v. Löwis910ae622003-04-19 07:44:52 +0000245 pthread_detach(th);
Guido van Rossumd6353e21997-05-13 17:51:13 +0000246#endif
Martin v. Löwis910ae622003-04-19 07:44:52 +0000247
Guido van Rossum3c288632001-10-16 21:13:49 +0000248#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
249 return (long) th;
250#else
251 return (long) *(long *) &th;
252#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000253}
254
Trent Mick635f6fb2000-08-23 21:33:05 +0000255/* XXX This implementation is considered (to quote Tim Peters) "inherently
256 hosed" because:
257 - It does not guanrantee the promise that a non-zero integer is returned.
258 - The cast to long is inherently unsafe.
259 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
260 latter return statement (for Alpha OSF/1) are any longer necessary.
261*/
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262long
263PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000264{
Guido van Rossum44ee4791998-08-27 19:21:53 +0000265 volatile pthread_t threadid;
Guido van Rossume944da81994-05-23 12:43:41 +0000266 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 PyThread_init_thread();
Guido van Rossum2565bff1995-01-09 17:50:47 +0000268 /* Jump through some hoops for Alpha OSF/1 */
269 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000270#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
271 return (long) threadid;
272#else
Guido van Rossum2565bff1995-01-09 17:50:47 +0000273 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000274#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000275}
276
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277static void
278do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000279{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000280 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum730806d1998-04-10 22:27:42 +0000281 if (!initialized) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000282 if (no_cleanup)
283 _exit(0);
284 else
285 exit(0);
Guido van Rossum730806d1998-04-10 22:27:42 +0000286 }
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(0);
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(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000299}
300
301#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302static void
303do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000304{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000305 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000306 if (!initialized)
307 if (no_cleanup)
308 _exit(status);
309 else
310 exit(status);
311}
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, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000317}
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, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000323}
324#endif /* NO_EXIT_PROG */
325
Martin v. Löwiscc898662002-03-17 09:53:51 +0000326#ifdef USE_SEMAPHORES
327
328/*
329 * Lock support.
330 */
331
332PyThread_type_lock
333PyThread_allocate_lock(void)
334{
335 sem_t *lock;
336 int status, error = 0;
337
338 dprintf(("PyThread_allocate_lock called\n"));
339 if (!initialized)
340 PyThread_init_thread();
341
342 lock = (sem_t *)malloc(sizeof(sem_t));
343
344 if (lock) {
345 status = sem_init(lock,0,1);
346 CHECK_STATUS("sem_init");
347
348 if (error) {
349 free((void *)lock);
350 lock = NULL;
351 }
352 }
353
354 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
355 return (PyThread_type_lock)lock;
356}
357
358void
359PyThread_free_lock(PyThread_type_lock lock)
360{
361 sem_t *thelock = (sem_t *)lock;
362 int status, error = 0;
363
364 dprintf(("PyThread_free_lock(%p) called\n", lock));
365
366 if (!thelock)
367 return;
368
369 status = sem_destroy(thelock);
370 CHECK_STATUS("sem_destroy");
371
372 free((void *)thelock);
373}
374
375/*
376 * As of February 2002, Cygwin thread implementations mistakenly report error
377 * codes in the return value of the sem_ calls (like the pthread_ functions).
378 * Correct implementations return -1 and put the code in errno. This supports
379 * either.
380 */
381static int
382fix_status(int status)
383{
384 return (status == -1) ? errno : status;
385}
386
387int
388PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
389{
390 int success;
391 sem_t *thelock = (sem_t *)lock;
392 int status, error = 0;
393
394 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
395
396 do {
397 if (waitflag)
398 status = fix_status(sem_wait(thelock));
399 else
400 status = fix_status(sem_trywait(thelock));
401 } while (status == EINTR); /* Retry if interrupted by a signal */
402
403 if (waitflag) {
404 CHECK_STATUS("sem_wait");
405 } else if (status != EAGAIN) {
406 CHECK_STATUS("sem_trywait");
407 }
408
409 success = (status == 0) ? 1 : 0;
410
411 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
412 return success;
413}
414
415void
416PyThread_release_lock(PyThread_type_lock lock)
417{
418 sem_t *thelock = (sem_t *)lock;
419 int status, error = 0;
420
421 dprintf(("PyThread_release_lock(%p) called\n", lock));
422
423 status = sem_post(thelock);
424 CHECK_STATUS("sem_post");
425}
426
427#else /* USE_SEMAPHORES */
428
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000429/*
430 * Lock support.
431 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000432PyThread_type_lock
433PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000434{
435 pthread_lock *lock;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000436 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000437
Guido van Rossum65d5b571998-12-21 19:32:43 +0000438 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000439 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000440 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000441
442 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
Guido van Rossum9e46e561998-10-07 16:39:47 +0000443 memset((void *)lock, '\0', sizeof(pthread_lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000444 if (lock) {
445 lock->locked = 0;
446
447 status = pthread_mutex_init(&lock->mut,
448 pthread_mutexattr_default);
449 CHECK_STATUS("pthread_mutex_init");
450
451 status = pthread_cond_init(&lock->lock_released,
452 pthread_condattr_default);
453 CHECK_STATUS("pthread_cond_init");
454
455 if (error) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000456 free((void *)lock);
457 lock = 0;
458 }
459 }
460
Fred Drakea44d3532000-06-30 15:01:00 +0000461 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000462 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000463}
464
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000465void
466PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000467{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000468 pthread_lock *thelock = (pthread_lock *)lock;
469 int status, error = 0;
470
Fred Drakea44d3532000-06-30 15:01:00 +0000471 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000472
473 status = pthread_mutex_destroy( &thelock->mut );
474 CHECK_STATUS("pthread_mutex_destroy");
475
476 status = pthread_cond_destroy( &thelock->lock_released );
477 CHECK_STATUS("pthread_cond_destroy");
478
479 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000480}
481
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000482int
483PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000484{
485 int success;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000486 pthread_lock *thelock = (pthread_lock *)lock;
487 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000488
Fred Drakea44d3532000-06-30 15:01:00 +0000489 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000490
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000491 status = pthread_mutex_lock( &thelock->mut );
492 CHECK_STATUS("pthread_mutex_lock[1]");
493 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000494
495 if ( !success && waitflag ) {
496 /* continue trying until we get the lock */
497
498 /* mut must be locked by me -- part of the condition
499 * protocol */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000500 while ( thelock->locked ) {
501 status = pthread_cond_wait(&thelock->lock_released,
502 &thelock->mut);
503 CHECK_STATUS("pthread_cond_wait");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000504 }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000505 success = 1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000506 }
Martin v. Löwis1509a152003-04-18 11:11:09 +0000507 if (success) thelock->locked = 1;
508 status = pthread_mutex_unlock( &thelock->mut );
509 CHECK_STATUS("pthread_mutex_unlock[1]");
510
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000511 if (error) success = 0;
Fred Drakea44d3532000-06-30 15:01:00 +0000512 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000513 return success;
514}
515
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000516void
517PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000518{
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000519 pthread_lock *thelock = (pthread_lock *)lock;
520 int status, error = 0;
521
Fred Drakea44d3532000-06-30 15:01:00 +0000522 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000523
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000524 status = pthread_mutex_lock( &thelock->mut );
525 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000526
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000527 thelock->locked = 0;
528
529 status = pthread_mutex_unlock( &thelock->mut );
530 CHECK_STATUS("pthread_mutex_unlock[3]");
531
532 /* wake up someone (anyone, if any) waiting on the lock */
533 status = pthread_cond_signal( &thelock->lock_released );
534 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000535}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000536
537#endif /* USE_SEMAPHORES */