1994-05-09 12:12:46 -03:00
|
|
|
|
1996-06-11 15:32:18 -03:00
|
|
|
/* Posix threads interface */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
1998-10-07 13:39:47 -03:00
|
|
|
#include <string.h>
|
2002-10-04 04:21:24 -03:00
|
|
|
#if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
|
2002-01-15 16:36:14 -04:00
|
|
|
#define destructor xxdestructor
|
|
|
|
#endif
|
1996-06-11 15:32:18 -03:00
|
|
|
#include <pthread.h>
|
2002-10-04 04:21:24 -03:00
|
|
|
#if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
|
2002-01-15 16:36:14 -04:00
|
|
|
#undef destructor
|
|
|
|
#endif
|
2001-10-12 18:49:17 -03:00
|
|
|
#include <signal.h>
|
2002-03-17 13:19:00 -04:00
|
|
|
|
2006-06-13 12:04:24 -03:00
|
|
|
/* The POSIX spec requires that use of pthread_attr_setstacksize
|
|
|
|
be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
|
|
|
|
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
|
|
|
|
#ifndef THREAD_STACK_SIZE
|
|
|
|
#define THREAD_STACK_SIZE 0 /* use default stack size */
|
|
|
|
#endif
|
|
|
|
/* for safety, ensure a viable minimum stacksize */
|
|
|
|
#define THREAD_STACK_MIN 0x8000 /* 32kB */
|
|
|
|
#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
|
|
|
|
#ifdef THREAD_STACK_SIZE
|
|
|
|
#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-03-17 13:19:00 -04:00
|
|
|
/* The POSIX spec says that implementations supporting the sem_*
|
|
|
|
family of functions must indicate this by defining
|
|
|
|
_POSIX_SEMAPHORES. */
|
2002-03-17 05:53:51 -04:00
|
|
|
#ifdef _POSIX_SEMAPHORES
|
2005-03-28 08:34:20 -04:00
|
|
|
/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
|
|
|
|
we need to add 0 to make it work there as well. */
|
|
|
|
#if (_POSIX_SEMAPHORES+0) == -1
|
2005-03-16 00:15:07 -04:00
|
|
|
#define HAVE_BROKEN_POSIX_SEMAPHORES
|
|
|
|
#else
|
2002-03-17 05:53:51 -04:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2005-03-16 00:15:07 -04:00
|
|
|
#endif
|
1994-05-09 12:12:46 -03:00
|
|
|
|
2006-03-23 08:32:36 -04:00
|
|
|
/* Before FreeBSD 5.4, system scope threads was very limited resource
|
|
|
|
in default setting. So the process scope is preferred to get
|
|
|
|
enough number of threads to work. */
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <osreldate.h>
|
|
|
|
#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
|
|
|
|
#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-12-11 09:12:30 -04:00
|
|
|
#if !defined(pthread_attr_default)
|
1997-05-13 14:51:13 -03:00
|
|
|
# define pthread_attr_default ((pthread_attr_t *)NULL)
|
2002-12-11 09:12:30 -04:00
|
|
|
#endif
|
|
|
|
#if !defined(pthread_mutexattr_default)
|
1997-05-13 14:51:13 -03:00
|
|
|
# define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
|
2002-12-11 09:12:30 -04:00
|
|
|
#endif
|
|
|
|
#if !defined(pthread_condattr_default)
|
1997-05-13 14:51:13 -03:00
|
|
|
# define pthread_condattr_default ((pthread_condattr_t *)NULL)
|
1996-08-08 15:53:41 -03:00
|
|
|
#endif
|
1994-05-09 12:12:46 -03:00
|
|
|
|
1997-05-13 14:51:13 -03:00
|
|
|
|
2002-03-17 05:53:51 -04:00
|
|
|
/* Whether or not to use semaphores directly rather than emulating them with
|
|
|
|
* mutexes and condition variables:
|
|
|
|
*/
|
2003-01-21 06:14:41 -04:00
|
|
|
#if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
|
2002-03-17 05:53:51 -04:00
|
|
|
# define USE_SEMAPHORES
|
|
|
|
#else
|
|
|
|
# undef USE_SEMAPHORES
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-10-12 18:49:17 -03:00
|
|
|
/* On platforms that don't use standard POSIX threads pthread_sigmask()
|
|
|
|
* isn't present. DEC threads uses sigprocmask() instead as do most
|
|
|
|
* other UNIX International compliant systems that don't have the full
|
|
|
|
* pthread implementation.
|
|
|
|
*/
|
2003-07-22 12:20:49 -03:00
|
|
|
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
|
2001-10-12 18:49:17 -03:00
|
|
|
# define SET_THREAD_SIGMASK pthread_sigmask
|
|
|
|
#else
|
|
|
|
# define SET_THREAD_SIGMASK sigprocmask
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1994-05-11 05:42:04 -03:00
|
|
|
/* A pthread mutex isn't sufficient to model the Python lock type
|
|
|
|
* because, according to Draft 5 of the docs (P1003.4a/D5), both of the
|
|
|
|
* following are undefined:
|
|
|
|
* -> a thread tries to lock a mutex it already has locked
|
|
|
|
* -> a thread tries to unlock a mutex locked by a different thread
|
|
|
|
* pthread mutexes are designed for serializing threads over short pieces
|
|
|
|
* of code anyway, so wouldn't be an appropriate implementation of
|
1994-05-09 12:12:46 -03:00
|
|
|
* Python's locks regardless.
|
1994-05-11 05:42:04 -03:00
|
|
|
*
|
|
|
|
* The pthread_lock struct implements a Python lock as a "locked?" bit
|
|
|
|
* and a <condition, mutex> pair. In general, if the bit can be acquired
|
|
|
|
* instantly, it is, else the pair is used to block the thread until the
|
|
|
|
* bit is cleared. 9 May 1994 tim@ksr.com
|
1994-05-09 12:12:46 -03:00
|
|
|
*/
|
1994-05-11 05:42:04 -03:00
|
|
|
|
1994-05-09 12:12:46 -03:00
|
|
|
typedef struct {
|
1994-05-11 05:42:04 -03:00
|
|
|
char locked; /* 0=unlocked, 1=locked */
|
|
|
|
/* a <cond, mutex> pair to handle an acquire of a locked lock */
|
|
|
|
pthread_cond_t lock_released;
|
|
|
|
pthread_mutex_t mut;
|
1994-05-09 12:12:46 -03:00
|
|
|
} pthread_lock;
|
|
|
|
|
1998-10-07 13:39:47 -03:00
|
|
|
#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
|
1994-05-11 05:42:04 -03:00
|
|
|
|
1994-05-09 12:12:46 -03:00
|
|
|
/*
|
|
|
|
* Initialization.
|
|
|
|
*/
|
1998-10-07 13:39:47 -03:00
|
|
|
|
|
|
|
#ifdef _HAVE_BSDI
|
2000-07-22 15:47:25 -03:00
|
|
|
static
|
|
|
|
void _noop(void)
|
1998-10-07 13:39:47 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
static void
|
|
|
|
PyThread__init_thread(void)
|
1998-10-07 13:39:47 -03:00
|
|
|
{
|
|
|
|
/* DO AN INIT BY STARTING THE THREAD */
|
|
|
|
static int dummy = 0;
|
|
|
|
pthread_t thread1;
|
|
|
|
pthread_create(&thread1, NULL, (void *) _noop, &dummy);
|
|
|
|
pthread_join(thread1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !_HAVE_BSDI */
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
static void
|
|
|
|
PyThread__init_thread(void)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
1998-09-10 00:04:40 -03:00
|
|
|
#if defined(_AIX) && defined(__GNUC__)
|
|
|
|
pthread_init();
|
|
|
|
#endif
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
1998-10-07 13:39:47 -03:00
|
|
|
#endif /* !_HAVE_BSDI */
|
|
|
|
|
1994-05-09 12:12:46 -03:00
|
|
|
/*
|
|
|
|
* Thread support.
|
|
|
|
*/
|
1994-05-11 05:42:04 -03:00
|
|
|
|
|
|
|
|
2001-10-16 18:13:49 -03:00
|
|
|
long
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_start_new_thread(void (*func)(void *), void *arg)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
|
|
|
pthread_t th;
|
2003-04-19 04:44:52 -03:00
|
|
|
int status;
|
2001-09-10 11:10:54 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2001-08-29 12:24:53 -03:00
|
|
|
pthread_attr_t attrs;
|
|
|
|
#endif
|
2006-06-13 12:04:24 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE)
|
|
|
|
size_t tss;
|
|
|
|
#endif
|
|
|
|
|
1998-12-21 15:32:43 -04:00
|
|
|
dprintf(("PyThread_start_new_thread called\n"));
|
1994-05-09 12:12:46 -03:00
|
|
|
if (!initialized)
|
1998-12-21 15:32:43 -04:00
|
|
|
PyThread_init_thread();
|
1997-05-13 14:51:13 -03:00
|
|
|
|
2001-09-10 11:10:54 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2006-06-13 12:04:24 -03:00
|
|
|
if (pthread_attr_init(&attrs) != 0)
|
|
|
|
return -1;
|
2001-09-10 11:10:54 -03:00
|
|
|
#endif
|
2006-06-13 12:04:24 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE)
|
|
|
|
tss = (_pythread_stacksize != 0) ? _pythread_stacksize
|
|
|
|
: THREAD_STACK_SIZE;
|
|
|
|
if (tss != 0) {
|
|
|
|
if (pthread_attr_setstacksize(&attrs, tss) != 0) {
|
|
|
|
pthread_attr_destroy(&attrs);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-09-10 11:10:54 -03:00
|
|
|
#endif
|
2006-03-23 08:32:36 -04:00
|
|
|
#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2001-09-10 11:10:54 -03:00
|
|
|
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
|
2001-08-29 12:24:53 -03:00
|
|
|
#endif
|
2001-10-12 18:49:17 -03:00
|
|
|
|
2003-04-19 04:44:52 -03:00
|
|
|
status = pthread_create(&th,
|
2001-09-10 11:10:54 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2001-08-29 12:24:53 -03:00
|
|
|
&attrs,
|
|
|
|
#else
|
1997-05-13 14:51:13 -03:00
|
|
|
(pthread_attr_t*)NULL,
|
2001-08-29 12:24:53 -03:00
|
|
|
#endif
|
2000-07-22 15:47:25 -03:00
|
|
|
(void* (*)(void *))func,
|
1997-05-13 14:51:13 -03:00
|
|
|
(void *)arg
|
|
|
|
);
|
2001-10-12 18:49:17 -03:00
|
|
|
|
2001-11-09 12:00:41 -04:00
|
|
|
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2001-08-29 12:24:53 -03:00
|
|
|
pthread_attr_destroy(&attrs);
|
|
|
|
#endif
|
2003-04-19 04:44:52 -03:00
|
|
|
if (status != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pthread_detach(th);
|
|
|
|
|
2001-10-16 18:13:49 -03:00
|
|
|
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
|
|
|
|
return (long) th;
|
|
|
|
#else
|
|
|
|
return (long) *(long *) &th;
|
|
|
|
#endif
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2000-08-23 18:33:05 -03:00
|
|
|
/* XXX This implementation is considered (to quote Tim Peters) "inherently
|
|
|
|
hosed" because:
|
2004-03-03 04:42:23 -04:00
|
|
|
- It does not guarantee the promise that a non-zero integer is returned.
|
2000-08-23 18:33:05 -03:00
|
|
|
- The cast to long is inherently unsafe.
|
|
|
|
- It is not clear that the 'volatile' (for AIX?) and ugly casting in the
|
|
|
|
latter return statement (for Alpha OSF/1) are any longer necessary.
|
|
|
|
*/
|
2000-07-22 15:47:25 -03:00
|
|
|
long
|
|
|
|
PyThread_get_thread_ident(void)
|
1994-05-23 09:43:41 -03:00
|
|
|
{
|
1998-08-27 16:21:53 -03:00
|
|
|
volatile pthread_t threadid;
|
1994-05-23 09:43:41 -03:00
|
|
|
if (!initialized)
|
1998-12-21 15:32:43 -04:00
|
|
|
PyThread_init_thread();
|
1995-01-09 13:50:47 -04:00
|
|
|
/* Jump through some hoops for Alpha OSF/1 */
|
|
|
|
threadid = pthread_self();
|
2000-08-23 18:33:05 -03:00
|
|
|
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
|
|
|
|
return (long) threadid;
|
|
|
|
#else
|
1995-01-09 13:50:47 -04:00
|
|
|
return (long) *(long *) &threadid;
|
2000-08-23 18:33:05 -03:00
|
|
|
#endif
|
1994-05-23 09:43:41 -03:00
|
|
|
}
|
|
|
|
|
2010-02-23 19:19:39 -04:00
|
|
|
void
|
|
|
|
PyThread_exit_thread(void)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
1998-12-21 15:32:43 -04:00
|
|
|
dprintf(("PyThread_exit_thread called\n"));
|
1998-04-10 19:27:42 -03:00
|
|
|
if (!initialized) {
|
2010-02-23 19:19:39 -04:00
|
|
|
exit(0);
|
1998-04-10 19:27:42 -03:00
|
|
|
}
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2002-03-17 05:53:51 -04:00
|
|
|
#ifdef USE_SEMAPHORES
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyThread_type_lock
|
|
|
|
PyThread_allocate_lock(void)
|
|
|
|
{
|
|
|
|
sem_t *lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_allocate_lock called\n"));
|
|
|
|
if (!initialized)
|
|
|
|
PyThread_init_thread();
|
|
|
|
|
|
|
|
lock = (sem_t *)malloc(sizeof(sem_t));
|
|
|
|
|
|
|
|
if (lock) {
|
|
|
|
status = sem_init(lock,0,1);
|
|
|
|
CHECK_STATUS("sem_init");
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
free((void *)lock);
|
|
|
|
lock = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
|
|
|
|
return (PyThread_type_lock)lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PyThread_free_lock(PyThread_type_lock lock)
|
|
|
|
{
|
|
|
|
sem_t *thelock = (sem_t *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_free_lock(%p) called\n", lock));
|
|
|
|
|
|
|
|
if (!thelock)
|
|
|
|
return;
|
|
|
|
|
|
|
|
status = sem_destroy(thelock);
|
|
|
|
CHECK_STATUS("sem_destroy");
|
|
|
|
|
|
|
|
free((void *)thelock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As of February 2002, Cygwin thread implementations mistakenly report error
|
|
|
|
* codes in the return value of the sem_ calls (like the pthread_ functions).
|
|
|
|
* Correct implementations return -1 and put the code in errno. This supports
|
|
|
|
* either.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fix_status(int status)
|
|
|
|
{
|
|
|
|
return (status == -1) ? errno : status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
|
|
|
|
{
|
|
|
|
int success;
|
|
|
|
sem_t *thelock = (sem_t *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (waitflag)
|
|
|
|
status = fix_status(sem_wait(thelock));
|
|
|
|
else
|
|
|
|
status = fix_status(sem_trywait(thelock));
|
|
|
|
} while (status == EINTR); /* Retry if interrupted by a signal */
|
|
|
|
|
|
|
|
if (waitflag) {
|
|
|
|
CHECK_STATUS("sem_wait");
|
|
|
|
} else if (status != EAGAIN) {
|
|
|
|
CHECK_STATUS("sem_trywait");
|
|
|
|
}
|
|
|
|
|
|
|
|
success = (status == 0) ? 1 : 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PyThread_release_lock(PyThread_type_lock lock)
|
|
|
|
{
|
|
|
|
sem_t *thelock = (sem_t *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_release_lock(%p) called\n", lock));
|
|
|
|
|
|
|
|
status = sem_post(thelock);
|
|
|
|
CHECK_STATUS("sem_post");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* USE_SEMAPHORES */
|
|
|
|
|
1994-05-09 12:12:46 -03:00
|
|
|
/*
|
|
|
|
* Lock support.
|
|
|
|
*/
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_type_lock
|
|
|
|
PyThread_allocate_lock(void)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
|
|
|
pthread_lock *lock;
|
1994-05-11 05:42:04 -03:00
|
|
|
int status, error = 0;
|
1994-05-09 12:12:46 -03:00
|
|
|
|
1998-12-21 15:32:43 -04:00
|
|
|
dprintf(("PyThread_allocate_lock called\n"));
|
1994-05-09 12:12:46 -03:00
|
|
|
if (!initialized)
|
1998-12-21 15:32:43 -04:00
|
|
|
PyThread_init_thread();
|
1994-05-09 12:12:46 -03:00
|
|
|
|
|
|
|
lock = (pthread_lock *) malloc(sizeof(pthread_lock));
|
1994-05-11 05:42:04 -03:00
|
|
|
if (lock) {
|
2005-09-20 15:07:47 -03:00
|
|
|
memset((void *)lock, '\0', sizeof(pthread_lock));
|
1994-05-11 05:42:04 -03:00
|
|
|
lock->locked = 0;
|
|
|
|
|
|
|
|
status = pthread_mutex_init(&lock->mut,
|
|
|
|
pthread_mutexattr_default);
|
|
|
|
CHECK_STATUS("pthread_mutex_init");
|
|
|
|
|
|
|
|
status = pthread_cond_init(&lock->lock_released,
|
|
|
|
pthread_condattr_default);
|
|
|
|
CHECK_STATUS("pthread_cond_init");
|
|
|
|
|
|
|
|
if (error) {
|
1994-05-09 12:12:46 -03:00
|
|
|
free((void *)lock);
|
|
|
|
lock = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-30 12:01:00 -03:00
|
|
|
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
|
1998-12-21 15:32:43 -04:00
|
|
|
return (PyThread_type_lock) lock;
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
void
|
|
|
|
PyThread_free_lock(PyThread_type_lock lock)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
1994-05-11 05:42:04 -03:00
|
|
|
pthread_lock *thelock = (pthread_lock *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
2000-06-30 12:01:00 -03:00
|
|
|
dprintf(("PyThread_free_lock(%p) called\n", lock));
|
1994-05-11 05:42:04 -03:00
|
|
|
|
|
|
|
status = pthread_mutex_destroy( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_destroy");
|
|
|
|
|
|
|
|
status = pthread_cond_destroy( &thelock->lock_released );
|
|
|
|
CHECK_STATUS("pthread_cond_destroy");
|
|
|
|
|
|
|
|
free((void *)thelock);
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
int
|
|
|
|
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
|
|
|
int success;
|
1994-05-11 05:42:04 -03:00
|
|
|
pthread_lock *thelock = (pthread_lock *)lock;
|
|
|
|
int status, error = 0;
|
1994-05-09 12:12:46 -03:00
|
|
|
|
2000-06-30 12:01:00 -03:00
|
|
|
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
|
1994-05-11 05:42:04 -03:00
|
|
|
|
|
|
|
status = pthread_mutex_lock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_lock[1]");
|
|
|
|
success = thelock->locked == 0;
|
|
|
|
|
|
|
|
if ( !success && waitflag ) {
|
|
|
|
/* continue trying until we get the lock */
|
|
|
|
|
|
|
|
/* mut must be locked by me -- part of the condition
|
|
|
|
* protocol */
|
|
|
|
while ( thelock->locked ) {
|
|
|
|
status = pthread_cond_wait(&thelock->lock_released,
|
|
|
|
&thelock->mut);
|
|
|
|
CHECK_STATUS("pthread_cond_wait");
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
1994-05-11 05:42:04 -03:00
|
|
|
success = 1;
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
2003-04-18 08:11:09 -03:00
|
|
|
if (success) thelock->locked = 1;
|
|
|
|
status = pthread_mutex_unlock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_unlock[1]");
|
|
|
|
|
1994-05-11 05:42:04 -03:00
|
|
|
if (error) success = 0;
|
2000-06-30 12:01:00 -03:00
|
|
|
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
|
1994-05-09 12:12:46 -03:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
void
|
|
|
|
PyThread_release_lock(PyThread_type_lock lock)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
1994-05-11 05:42:04 -03:00
|
|
|
pthread_lock *thelock = (pthread_lock *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
2000-06-30 12:01:00 -03:00
|
|
|
dprintf(("PyThread_release_lock(%p) called\n", lock));
|
1994-05-11 05:42:04 -03:00
|
|
|
|
|
|
|
status = pthread_mutex_lock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_lock[3]");
|
|
|
|
|
|
|
|
thelock->locked = 0;
|
|
|
|
|
|
|
|
status = pthread_mutex_unlock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_unlock[3]");
|
|
|
|
|
|
|
|
/* wake up someone (anyone, if any) waiting on the lock */
|
|
|
|
status = pthread_cond_signal( &thelock->lock_released );
|
|
|
|
CHECK_STATUS("pthread_cond_signal");
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
2002-03-17 05:53:51 -04:00
|
|
|
|
|
|
|
#endif /* USE_SEMAPHORES */
|
2006-06-13 12:04:24 -03:00
|
|
|
|
|
|
|
/* set the thread stack size.
|
|
|
|
* Return 0 if size is valid, -1 if size is invalid,
|
|
|
|
* -2 if setting stack size is not supported.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
_pythread_pthread_set_stacksize(size_t size)
|
|
|
|
{
|
|
|
|
#if defined(THREAD_STACK_SIZE)
|
|
|
|
pthread_attr_t attrs;
|
|
|
|
size_t tss_min;
|
|
|
|
int rc = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set to default */
|
|
|
|
if (size == 0) {
|
|
|
|
_pythread_stacksize = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(THREAD_STACK_SIZE)
|
|
|
|
#if defined(PTHREAD_STACK_MIN)
|
|
|
|
tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
|
|
|
|
: THREAD_STACK_MIN;
|
|
|
|
#else
|
|
|
|
tss_min = THREAD_STACK_MIN;
|
|
|
|
#endif
|
|
|
|
if (size >= tss_min) {
|
|
|
|
/* validate stack size by setting thread attribute */
|
|
|
|
if (pthread_attr_init(&attrs) == 0) {
|
|
|
|
rc = pthread_attr_setstacksize(&attrs, size);
|
|
|
|
pthread_attr_destroy(&attrs);
|
|
|
|
if (rc == 0) {
|
|
|
|
_pythread_stacksize = size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
return -2;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
|