blob: c1c92d1a15eb5175f24d73ad3af834adde759a7c [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
Andrew MacIntyre92913322006-06-13 15:04:24 +000015/* The POSIX spec requires that use of pthread_attr_setstacksize
16 be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
17#ifdef _POSIX_THREAD_ATTR_STACKSIZE
18#ifndef THREAD_STACK_SIZE
Antoine Pitrouc83ea132010-05-09 14:46:46 +000019#define THREAD_STACK_SIZE 0 /* use default stack size */
Andrew MacIntyre92913322006-06-13 15:04:24 +000020#endif
Ned Deily482f9082011-05-28 00:11:54 -070021
22#if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
23 /* The default stack size for new threads on OSX is small enough that
24 * we'll get hard crashes instead of 'maximum recursion depth exceeded'
25 * exceptions.
26 *
27 * The default stack size below is the minimal stack size where a
28 * simple recursive function doesn't cause a hard crash.
29 */
30#undef THREAD_STACK_SIZE
31#define THREAD_STACK_SIZE 0x400000
32#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +000033/* for safety, ensure a viable minimum stacksize */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000034#define THREAD_STACK_MIN 0x8000 /* 32kB */
Andrew MacIntyre92913322006-06-13 15:04:24 +000035#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
36#ifdef THREAD_STACK_SIZE
37#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
38#endif
39#endif
40
Martin v. Löwis42ab61e2002-03-17 17:19:00 +000041/* The POSIX spec says that implementations supporting the sem_*
42 family of functions must indicate this by defining
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 _POSIX_SEMAPHORES. */
Martin v. Löwiscc898662002-03-17 09:53:51 +000044#ifdef _POSIX_SEMAPHORES
Antoine Pitrouc83ea132010-05-09 14:46:46 +000045/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
Martin v. Löwis8b8fb3d2005-03-28 12:34:20 +000046 we need to add 0 to make it work there as well. */
47#if (_POSIX_SEMAPHORES+0) == -1
Anthony Baxter19b23692005-03-16 04:15:07 +000048#define HAVE_BROKEN_POSIX_SEMAPHORES
49#else
Martin v. Löwiscc898662002-03-17 09:53:51 +000050#include <semaphore.h>
51#include <errno.h>
52#endif
Anthony Baxter19b23692005-03-16 04:15:07 +000053#endif
Guido van Rossum66020991996-06-11 18:32:18 +000054
Hye-Shik Changd478f342006-03-23 12:32:36 +000055/* Before FreeBSD 5.4, system scope threads was very limited resource
56 in default setting. So the process scope is preferred to get
57 enough number of threads to work. */
58#ifdef __FreeBSD__
59#include <osreldate.h>
60#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
61#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
62#endif
63#endif
64
Martin v. Löwisb0233812002-12-11 13:12:30 +000065#if !defined(pthread_attr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000066# define pthread_attr_default ((pthread_attr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000067#endif
68#if !defined(pthread_mutexattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000069# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
Martin v. Löwisb0233812002-12-11 13:12:30 +000070#endif
71#if !defined(pthread_condattr_default)
Guido van Rossumd6353e21997-05-13 17:51:13 +000072# define pthread_condattr_default ((pthread_condattr_t *)NULL)
Guido van Rossum1a623111996-08-08 18:53:41 +000073#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000074
Guido van Rossumd6353e21997-05-13 17:51:13 +000075
Martin v. Löwiscc898662002-03-17 09:53:51 +000076/* Whether or not to use semaphores directly rather than emulating them with
77 * mutexes and condition variables:
78 */
Martin v. Löwisdfc33fd2003-01-21 10:14:41 +000079#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
Martin v. Löwiscc898662002-03-17 09:53:51 +000080# define USE_SEMAPHORES
81#else
82# undef USE_SEMAPHORES
83#endif
84
85
Guido van Rossum80230992001-10-12 21:49:17 +000086/* On platforms that don't use standard POSIX threads pthread_sigmask()
87 * isn't present. DEC threads uses sigprocmask() instead as do most
88 * other UNIX International compliant systems that don't have the full
89 * pthread implementation.
90 */
Jason Tishlerfac083d2003-07-22 15:20:49 +000091#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Guido van Rossum80230992001-10-12 21:49:17 +000092# define SET_THREAD_SIGMASK pthread_sigmask
93#else
94# define SET_THREAD_SIGMASK sigprocmask
95#endif
96
97
Guido van Rossumb98b1b31994-05-11 08:42:04 +000098/* A pthread mutex isn't sufficient to model the Python lock type
99 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
100 * following are undefined:
101 * -> a thread tries to lock a mutex it already has locked
102 * -> a thread tries to unlock a mutex locked by a different thread
103 * pthread mutexes are designed for serializing threads over short pieces
104 * of code anyway, so wouldn't be an appropriate implementation of
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000105 * Python's locks regardless.
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000106 *
107 * The pthread_lock struct implements a Python lock as a "locked?" bit
108 * and a <condition, mutex> pair. In general, if the bit can be acquired
109 * instantly, it is, else the pair is used to block the thread until the
110 * bit is cleared. 9 May 1994 tim@ksr.com
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000111 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000112
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 char locked; /* 0=unlocked, 1=locked */
115 /* a <cond, mutex> pair to handle an acquire of a locked lock */
116 pthread_cond_t lock_released;
117 pthread_mutex_t mut;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000118} pthread_lock;
119
Guido van Rossum9e46e561998-10-07 16:39:47 +0000120#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000121
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000122/*
123 * Initialization.
124 */
Guido van Rossum9e46e561998-10-07 16:39:47 +0000125
126#ifdef _HAVE_BSDI
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127static
128void _noop(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000129{
130}
131
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000132static void
133PyThread__init_thread(void)
Guido van Rossum9e46e561998-10-07 16:39:47 +0000134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000135 /* DO AN INIT BY STARTING THE THREAD */
136 static int dummy = 0;
137 pthread_t thread1;
138 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
139 pthread_join(thread1, NULL);
Guido van Rossum9e46e561998-10-07 16:39:47 +0000140}
141
142#else /* !_HAVE_BSDI */
143
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144static void
145PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146{
Guido van Rossumd21744a1998-09-10 03:04:40 +0000147#if defined(_AIX) && defined(__GNUC__)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000148 pthread_init();
Guido van Rossumd21744a1998-09-10 03:04:40 +0000149#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000150}
151
Guido van Rossum9e46e561998-10-07 16:39:47 +0000152#endif /* !_HAVE_BSDI */
153
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154/*
155 * Thread support.
156 */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000157
158
Guido van Rossum3c288632001-10-16 21:13:49 +0000159long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000161{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 pthread_t th;
163 int status;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000164#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000165 pthread_attr_t attrs;
Jack Jansenc51395d2001-08-29 15:24:53 +0000166#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000167#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 size_t tss;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000169#endif
170
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000171 dprintf(("PyThread_start_new_thread called\n"));
172 if (!initialized)
173 PyThread_init_thread();
Guido van Rossumd6353e21997-05-13 17:51:13 +0000174
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000175#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176 if (pthread_attr_init(&attrs) != 0)
177 return -1;
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000178#endif
Andrew MacIntyre92913322006-06-13 15:04:24 +0000179#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000180 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
181 : THREAD_STACK_SIZE;
182 if (tss != 0) {
183 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
184 pthread_attr_destroy(&attrs);
185 return -1;
186 }
187 }
Jack Jansenc51395d2001-08-29 15:24:53 +0000188#endif
Hye-Shik Changd478f342006-03-23 12:32:36 +0000189#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000191#endif
Guido van Rossum80230992001-10-12 21:49:17 +0000192
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 status = pthread_create(&th,
Guido van Rossumd0b69ec2001-09-10 14:10:54 +0000194#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 &attrs,
Jack Jansenc51395d2001-08-29 15:24:53 +0000196#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 (pthread_attr_t*)NULL,
Jack Jansenc51395d2001-08-29 15:24:53 +0000198#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 (void* (*)(void *))func,
200 (void *)arg
201 );
Guido van Rossum80230992001-10-12 21:49:17 +0000202
Fred Drake03459a52001-11-09 16:00:41 +0000203#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 pthread_attr_destroy(&attrs);
Jack Jansenc51395d2001-08-29 15:24:53 +0000205#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000206 if (status != 0)
207 return -1;
Martin v. Löwis910ae622003-04-19 07:44:52 +0000208
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000209 pthread_detach(th);
Martin v. Löwis910ae622003-04-19 07:44:52 +0000210
Guido van Rossum3c288632001-10-16 21:13:49 +0000211#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 return (long) th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000213#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 return (long) *(long *) &th;
Guido van Rossum3c288632001-10-16 21:13:49 +0000215#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000216}
217
Trent Mick635f6fb2000-08-23 21:33:05 +0000218/* XXX This implementation is considered (to quote Tim Peters) "inherently
219 hosed" because:
Skip Montanaro6babcc22004-03-03 08:42:23 +0000220 - It does not guarantee the promise that a non-zero integer is returned.
Trent Mick635f6fb2000-08-23 21:33:05 +0000221 - The cast to long is inherently unsafe.
222 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
223 latter return statement (for Alpha OSF/1) are any longer necessary.
224*/
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000225long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +0000227{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000228 volatile pthread_t threadid;
229 if (!initialized)
230 PyThread_init_thread();
231 /* Jump through some hoops for Alpha OSF/1 */
232 threadid = pthread_self();
Trent Mick635f6fb2000-08-23 21:33:05 +0000233#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 return (long) threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000235#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 return (long) *(long *) &threadid;
Trent Mick635f6fb2000-08-23 21:33:05 +0000237#endif
Guido van Rossume944da81994-05-23 12:43:41 +0000238}
239
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 dprintf(("PyThread_exit_thread called\n"));
244 if (!initialized) {
245 exit(0);
246 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000247}
248
Martin v. Löwiscc898662002-03-17 09:53:51 +0000249#ifdef USE_SEMAPHORES
250
251/*
252 * Lock support.
253 */
254
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000255PyThread_type_lock
Martin v. Löwiscc898662002-03-17 09:53:51 +0000256PyThread_allocate_lock(void)
257{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 sem_t *lock;
259 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000260
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 dprintf(("PyThread_allocate_lock called\n"));
262 if (!initialized)
263 PyThread_init_thread();
Martin v. Löwiscc898662002-03-17 09:53:51 +0000264
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 lock = (sem_t *)malloc(sizeof(sem_t));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000266
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 if (lock) {
268 status = sem_init(lock,0,1);
269 CHECK_STATUS("sem_init");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000270
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 if (error) {
272 free((void *)lock);
273 lock = NULL;
274 }
275 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000276
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000277 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
278 return (PyThread_type_lock)lock;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000279}
280
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000282PyThread_free_lock(PyThread_type_lock lock)
283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 sem_t *thelock = (sem_t *)lock;
285 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000286
Jesus Cea7ddd9c22012-12-05 14:41:11 +0100287 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000288 dprintf(("PyThread_free_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000289
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 if (!thelock)
291 return;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000292
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000293 status = sem_destroy(thelock);
294 CHECK_STATUS("sem_destroy");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000295
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 free((void *)thelock);
Martin v. Löwiscc898662002-03-17 09:53:51 +0000297}
298
299/*
300 * As of February 2002, Cygwin thread implementations mistakenly report error
301 * codes in the return value of the sem_ calls (like the pthread_ functions).
302 * Correct implementations return -1 and put the code in errno. This supports
303 * either.
304 */
305static int
306fix_status(int status)
307{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000308 return (status == -1) ? errno : status;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000309}
310
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000311int
Martin v. Löwiscc898662002-03-17 09:53:51 +0000312PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
313{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000314 int success;
315 sem_t *thelock = (sem_t *)lock;
316 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000317
Jesus Cea7ddd9c22012-12-05 14:41:11 +0100318 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000319 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000320
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 do {
322 if (waitflag)
323 status = fix_status(sem_wait(thelock));
324 else
325 status = fix_status(sem_trywait(thelock));
326 } while (status == EINTR); /* Retry if interrupted by a signal */
Martin v. Löwiscc898662002-03-17 09:53:51 +0000327
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 if (waitflag) {
329 CHECK_STATUS("sem_wait");
330 } else if (status != EAGAIN) {
331 CHECK_STATUS("sem_trywait");
332 }
Martin v. Löwiscc898662002-03-17 09:53:51 +0000333
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000334 success = (status == 0) ? 1 : 0;
335
336 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
337 return success;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000338}
339
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000340void
Martin v. Löwiscc898662002-03-17 09:53:51 +0000341PyThread_release_lock(PyThread_type_lock lock)
342{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000343 sem_t *thelock = (sem_t *)lock;
344 int status, error = 0;
Martin v. Löwiscc898662002-03-17 09:53:51 +0000345
Jesus Cea7ddd9c22012-12-05 14:41:11 +0100346 (void) error; /* silence unused-but-set-variable warning */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 dprintf(("PyThread_release_lock(%p) called\n", lock));
Martin v. Löwiscc898662002-03-17 09:53:51 +0000348
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 status = sem_post(thelock);
350 CHECK_STATUS("sem_post");
Martin v. Löwiscc898662002-03-17 09:53:51 +0000351}
352
353#else /* USE_SEMAPHORES */
354
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000355/*
356 * Lock support.
357 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000360{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000361 pthread_lock *lock;
362 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000363
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 dprintf(("PyThread_allocate_lock called\n"));
365 if (!initialized)
366 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000367
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000368 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
369 if (lock) {
370 memset((void *)lock, '\0', sizeof(pthread_lock));
371 lock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000372
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000373 status = pthread_mutex_init(&lock->mut,
374 pthread_mutexattr_default);
375 CHECK_STATUS("pthread_mutex_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000376
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 status = pthread_cond_init(&lock->lock_released,
378 pthread_condattr_default);
379 CHECK_STATUS("pthread_cond_init");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000380
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000381 if (error) {
382 free((void *)lock);
383 lock = 0;
384 }
385 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000386
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000387 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
388 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000389}
390
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000391void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000392PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 pthread_lock *thelock = (pthread_lock *)lock;
395 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000396
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000398
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 status = pthread_mutex_destroy( &thelock->mut );
400 CHECK_STATUS("pthread_mutex_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000401
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000402 status = pthread_cond_destroy( &thelock->lock_released );
403 CHECK_STATUS("pthread_cond_destroy");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000404
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405 free((void *)thelock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000406}
407
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000409PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000410{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 int success;
412 pthread_lock *thelock = (pthread_lock *)lock;
413 int status, error = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000414
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000416
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000417 status = pthread_mutex_lock( &thelock->mut );
418 CHECK_STATUS("pthread_mutex_lock[1]");
419 success = thelock->locked == 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000420
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000421 if ( !success && waitflag ) {
422 /* continue trying until we get the lock */
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000423
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 /* mut must be locked by me -- part of the condition
425 * protocol */
426 while ( thelock->locked ) {
427 status = pthread_cond_wait(&thelock->lock_released,
428 &thelock->mut);
429 CHECK_STATUS("pthread_cond_wait");
430 }
431 success = 1;
432 }
433 if (success) thelock->locked = 1;
434 status = pthread_mutex_unlock( &thelock->mut );
435 CHECK_STATUS("pthread_mutex_unlock[1]");
Martin v. Löwis1509a152003-04-18 11:11:09 +0000436
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000437 if (error) success = 0;
438 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
439 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000440}
441
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000442void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000444{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 pthread_lock *thelock = (pthread_lock *)lock;
446 int status, error = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000447
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000448 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000449
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000450 status = pthread_mutex_lock( &thelock->mut );
451 CHECK_STATUS("pthread_mutex_lock[3]");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000452
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 thelock->locked = 0;
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000454
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000455 status = pthread_mutex_unlock( &thelock->mut );
456 CHECK_STATUS("pthread_mutex_unlock[3]");
Guido van Rossumb98b1b31994-05-11 08:42:04 +0000457
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 /* wake up someone (anyone, if any) waiting on the lock */
459 status = pthread_cond_signal( &thelock->lock_released );
460 CHECK_STATUS("pthread_cond_signal");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000461}
Martin v. Löwiscc898662002-03-17 09:53:51 +0000462
463#endif /* USE_SEMAPHORES */
Andrew MacIntyre92913322006-06-13 15:04:24 +0000464
465/* set the thread stack size.
466 * Return 0 if size is valid, -1 if size is invalid,
467 * -2 if setting stack size is not supported.
468 */
469static int
470_pythread_pthread_set_stacksize(size_t size)
471{
472#if defined(THREAD_STACK_SIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 pthread_attr_t attrs;
474 size_t tss_min;
475 int rc = 0;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000476#endif
477
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000478 /* set to default */
479 if (size == 0) {
480 _pythread_stacksize = 0;
481 return 0;
482 }
Andrew MacIntyre92913322006-06-13 15:04:24 +0000483
484#if defined(THREAD_STACK_SIZE)
485#if defined(PTHREAD_STACK_MIN)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000486 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
487 : THREAD_STACK_MIN;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000488#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000489 tss_min = THREAD_STACK_MIN;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000490#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 if (size >= tss_min) {
492 /* validate stack size by setting thread attribute */
493 if (pthread_attr_init(&attrs) == 0) {
494 rc = pthread_attr_setstacksize(&attrs, size);
495 pthread_attr_destroy(&attrs);
496 if (rc == 0) {
497 _pythread_stacksize = size;
498 return 0;
499 }
500 }
501 }
502 return -1;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000503#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000504 return -2;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000505#endif
506}
507
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)