2 /* Posix threads interface */
6 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
7 #define destructor xxdestructor
10 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
15 /* 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
19 #define THREAD_STACK_SIZE 0 /* use default stack size */
21 /* for safety, ensure a viable minimum stacksize */
22 #define THREAD_STACK_MIN 0x8000 /* 32kB */
23 #else /* !_POSIX_THREAD_ATTR_STACKSIZE */
24 #ifdef THREAD_STACK_SIZE
25 #error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
29 /* The POSIX spec says that implementations supporting the sem_*
30 family of functions must indicate this by defining
32 #ifdef _POSIX_SEMAPHORES
33 /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
34 we need to add 0 to make it work there as well. */
35 #if (_POSIX_SEMAPHORES+0) == -1
36 #define HAVE_BROKEN_POSIX_SEMAPHORES
38 #include <semaphore.h>
43 /* Before FreeBSD 5.4, system scope threads was very limited resource
44 in default setting. So the process scope is preferred to get
45 enough number of threads to work. */
47 #include <osreldate.h>
48 #if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
49 #undef PTHREAD_SYSTEM_SCHED_SUPPORTED
53 #if !defined(pthread_attr_default)
54 # define pthread_attr_default ((pthread_attr_t *)NULL)
56 #if !defined(pthread_mutexattr_default)
57 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
59 #if !defined(pthread_condattr_default)
60 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
64 /* Whether or not to use semaphores directly rather than emulating them with
65 * mutexes and condition variables:
67 #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
68 # define USE_SEMAPHORES
70 # undef USE_SEMAPHORES
74 /* On platforms that don't use standard POSIX threads pthread_sigmask()
75 * isn't present. DEC threads uses sigprocmask() instead as do most
76 * other UNIX International compliant systems that don't have the full
77 * pthread implementation.
79 #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
80 # define SET_THREAD_SIGMASK pthread_sigmask
82 # define SET_THREAD_SIGMASK sigprocmask
86 /* A pthread mutex isn't sufficient to model the Python lock type
87 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
88 * following are undefined:
89 * -> a thread tries to lock a mutex it already has locked
90 * -> a thread tries to unlock a mutex locked by a different thread
91 * pthread mutexes are designed for serializing threads over short pieces
92 * of code anyway, so wouldn't be an appropriate implementation of
93 * Python's locks regardless.
95 * The pthread_lock struct implements a Python lock as a "locked?" bit
96 * and a <condition, mutex> pair. In general, if the bit can be acquired
97 * instantly, it is, else the pair is used to block the thread until the
98 * bit is cleared. 9 May 1994 tim@ksr.com
102 char locked; /* 0=unlocked, 1=locked */
103 /* a <cond, mutex> pair to handle an acquire of a locked lock */
104 pthread_cond_t lock_released;
108 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
121 PyThread__init_thread(void)
123 /* DO AN INIT BY STARTING THE THREAD */
124 static int dummy = 0;
126 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
127 pthread_join(thread1, NULL);
130 #else /* !_HAVE_BSDI */
133 PyThread__init_thread(void)
135 #if defined(_AIX) && defined(__GNUC__)
140 #endif /* !_HAVE_BSDI */
148 PyThread_start_new_thread(void (*func)(void *), void *arg)
152 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
153 pthread_attr_t attrs;
155 #if defined(THREAD_STACK_SIZE)
159 dprintf(("PyThread_start_new_thread called\n"));
161 PyThread_init_thread();
163 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
164 if (pthread_attr_init(&attrs) != 0)
167 #if defined(THREAD_STACK_SIZE)
168 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
171 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
172 pthread_attr_destroy(&attrs);
177 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
178 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
181 status = pthread_create(&th,
182 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
185 (pthread_attr_t*)NULL,
187 (void* (*)(void *))func,
191 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
192 pthread_attr_destroy(&attrs);
199 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
202 return (long) *(long *) &th;
206 /* XXX This implementation is considered (to quote Tim Peters) "inherently
208 - It does not guarantee the promise that a non-zero integer is returned.
209 - The cast to long is inherently unsafe.
210 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
211 latter return statement (for Alpha OSF/1) are any longer necessary.
214 PyThread_get_thread_ident(void)
216 volatile pthread_t threadid;
218 PyThread_init_thread();
219 /* Jump through some hoops for Alpha OSF/1 */
220 threadid = pthread_self();
221 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
222 return (long) threadid;
224 return (long) *(long *) &threadid;
229 PyThread_exit_thread(void)
231 dprintf(("PyThread_exit_thread called\n"));
237 #ifdef USE_SEMAPHORES
244 PyThread_allocate_lock(void)
247 int status, error = 0;
249 dprintf(("PyThread_allocate_lock called\n"));
251 PyThread_init_thread();
253 lock = (sem_t *)malloc(sizeof(sem_t));
256 status = sem_init(lock,0,1);
257 CHECK_STATUS("sem_init");
265 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
266 return (PyThread_type_lock)lock;
270 PyThread_free_lock(PyThread_type_lock lock)
272 sem_t *thelock = (sem_t *)lock;
273 int status, error = 0;
275 dprintf(("PyThread_free_lock(%p) called\n", lock));
280 status = sem_destroy(thelock);
281 CHECK_STATUS("sem_destroy");
283 free((void *)thelock);
287 * As of February 2002, Cygwin thread implementations mistakenly report error
288 * codes in the return value of the sem_ calls (like the pthread_ functions).
289 * Correct implementations return -1 and put the code in errno. This supports
293 fix_status(int status)
295 return (status == -1) ? errno : status;
299 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
302 sem_t *thelock = (sem_t *)lock;
303 int status, error = 0;
305 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
309 status = fix_status(sem_wait(thelock));
311 status = fix_status(sem_trywait(thelock));
312 } while (status == EINTR); /* Retry if interrupted by a signal */
315 CHECK_STATUS("sem_wait");
316 } else if (status != EAGAIN) {
317 CHECK_STATUS("sem_trywait");
320 success = (status == 0) ? 1 : 0;
322 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
327 PyThread_release_lock(PyThread_type_lock lock)
329 sem_t *thelock = (sem_t *)lock;
330 int status, error = 0;
332 dprintf(("PyThread_release_lock(%p) called\n", lock));
334 status = sem_post(thelock);
335 CHECK_STATUS("sem_post");
338 #else /* USE_SEMAPHORES */
344 PyThread_allocate_lock(void)
347 int status, error = 0;
349 dprintf(("PyThread_allocate_lock called\n"));
351 PyThread_init_thread();
353 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
355 memset((void *)lock, '\0', sizeof(pthread_lock));
358 status = pthread_mutex_init(&lock->mut,
359 pthread_mutexattr_default);
360 CHECK_STATUS("pthread_mutex_init");
362 status = pthread_cond_init(&lock->lock_released,
363 pthread_condattr_default);
364 CHECK_STATUS("pthread_cond_init");
372 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
373 return (PyThread_type_lock) lock;
377 PyThread_free_lock(PyThread_type_lock lock)
379 pthread_lock *thelock = (pthread_lock *)lock;
380 int status, error = 0;
382 dprintf(("PyThread_free_lock(%p) called\n", lock));
384 status = pthread_mutex_destroy( &thelock->mut );
385 CHECK_STATUS("pthread_mutex_destroy");
387 status = pthread_cond_destroy( &thelock->lock_released );
388 CHECK_STATUS("pthread_cond_destroy");
390 free((void *)thelock);
394 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
397 pthread_lock *thelock = (pthread_lock *)lock;
398 int status, error = 0;
400 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
402 status = pthread_mutex_lock( &thelock->mut );
403 CHECK_STATUS("pthread_mutex_lock[1]");
404 success = thelock->locked == 0;
406 if ( !success && waitflag ) {
407 /* continue trying until we get the lock */
409 /* mut must be locked by me -- part of the condition
411 while ( thelock->locked ) {
412 status = pthread_cond_wait(&thelock->lock_released,
414 CHECK_STATUS("pthread_cond_wait");
418 if (success) thelock->locked = 1;
419 status = pthread_mutex_unlock( &thelock->mut );
420 CHECK_STATUS("pthread_mutex_unlock[1]");
422 if (error) success = 0;
423 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
428 PyThread_release_lock(PyThread_type_lock lock)
430 pthread_lock *thelock = (pthread_lock *)lock;
431 int status, error = 0;
433 dprintf(("PyThread_release_lock(%p) called\n", lock));
435 status = pthread_mutex_lock( &thelock->mut );
436 CHECK_STATUS("pthread_mutex_lock[3]");
440 status = pthread_mutex_unlock( &thelock->mut );
441 CHECK_STATUS("pthread_mutex_unlock[3]");
443 /* wake up someone (anyone, if any) waiting on the lock */
444 status = pthread_cond_signal( &thelock->lock_released );
445 CHECK_STATUS("pthread_cond_signal");
448 #endif /* USE_SEMAPHORES */
450 /* set the thread stack size.
451 * Return 0 if size is valid, -1 if size is invalid,
452 * -2 if setting stack size is not supported.
455 _pythread_pthread_set_stacksize(size_t size)
457 #if defined(THREAD_STACK_SIZE)
458 pthread_attr_t attrs;
465 _pythread_stacksize = 0;
469 #if defined(THREAD_STACK_SIZE)
470 #if defined(PTHREAD_STACK_MIN)
471 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
474 tss_min = THREAD_STACK_MIN;
476 if (size >= tss_min) {
477 /* validate stack size by setting thread attribute */
478 if (pthread_attr_init(&attrs) == 0) {
479 rc = pthread_attr_setstacksize(&attrs, size);
480 pthread_attr_destroy(&attrs);
482 _pythread_stacksize = size;
493 #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)