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
|
2010-05-09 11:46:46 -03:00
|
|
|
#define THREAD_STACK_SIZE 0 /* use default stack size */
|
2006-06-13 12:04:24 -03:00
|
|
|
#endif
|
2011-05-28 04:11:54 -03:00
|
|
|
|
|
|
|
#if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
|
|
|
|
/* The default stack size for new threads on OSX is small enough that
|
|
|
|
* we'll get hard crashes instead of 'maximum recursion depth exceeded'
|
|
|
|
* exceptions.
|
|
|
|
*
|
|
|
|
* The default stack size below is the minimal stack size where a
|
|
|
|
* simple recursive function doesn't cause a hard crash.
|
|
|
|
*/
|
|
|
|
#undef THREAD_STACK_SIZE
|
|
|
|
#define THREAD_STACK_SIZE 0x400000
|
|
|
|
#endif
|
2006-06-13 12:04:24 -03:00
|
|
|
/* for safety, ensure a viable minimum stacksize */
|
2010-05-09 11:46:46 -03:00
|
|
|
#define THREAD_STACK_MIN 0x8000 /* 32kB */
|
2006-06-13 12:04:24 -03:00
|
|
|
#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
|
2010-05-09 11:46:46 -03:00
|
|
|
_POSIX_SEMAPHORES. */
|
2002-03-17 05:53:51 -04:00
|
|
|
#ifdef _POSIX_SEMAPHORES
|
2010-05-09 11:46:46 -03:00
|
|
|
/* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
|
2005-03-28 08:34:20 -04:00
|
|
|
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 {
|
2010-05-09 11:46:46 -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
|
|
|
{
|
2010-05-09 11:46:46 -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);
|
1998-10-07 13:39:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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__)
|
2013-06-18 17:17:48 -03:00
|
|
|
extern void pthread_init(void);
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_init();
|
1998-09-10 00:04:40 -03:00
|
|
|
#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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_t th;
|
|
|
|
int status;
|
2001-09-10 11:10:54 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_attr_t attrs;
|
2001-08-29 12:24:53 -03:00
|
|
|
#endif
|
2006-06-13 12:04:24 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE)
|
2010-05-09 11:46:46 -03:00
|
|
|
size_t tss;
|
2006-06-13 12:04:24 -03:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_start_new_thread called\n"));
|
|
|
|
if (!initialized)
|
|
|
|
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)
|
2010-05-09 11:46:46 -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)
|
2010-05-09 11:46:46 -03:00
|
|
|
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)
|
2010-05-09 11:46:46 -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
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = pthread_create(&th,
|
2001-09-10 11:10:54 -03:00
|
|
|
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
2010-05-09 11:46:46 -03:00
|
|
|
&attrs,
|
2001-08-29 12:24:53 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
(pthread_attr_t*)NULL,
|
2001-08-29 12:24:53 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
(void* (*)(void *))func,
|
|
|
|
(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)
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_attr_destroy(&attrs);
|
2001-08-29 12:24:53 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
if (status != 0)
|
|
|
|
return -1;
|
2003-04-19 04:44:52 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_detach(th);
|
2003-04-19 04:44:52 -03:00
|
|
|
|
2001-10-16 18:13:49 -03:00
|
|
|
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
|
2010-05-09 11:46:46 -03:00
|
|
|
return (long) th;
|
2001-10-16 18:13:49 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
return (long) *(long *) &th;
|
2001-10-16 18:13:49 -03:00
|
|
|
#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.
|
|
|
|
*/
|
2010-05-09 11:46:46 -03:00
|
|
|
long
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_get_thread_ident(void)
|
1994-05-23 09:43:41 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
volatile pthread_t threadid;
|
|
|
|
if (!initialized)
|
|
|
|
PyThread_init_thread();
|
|
|
|
/* 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
|
2010-05-09 11:46:46 -03:00
|
|
|
return (long) threadid;
|
2000-08-23 18:33:05 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
return (long) *(long *) &threadid;
|
2000-08-23 18:33:05 -03:00
|
|
|
#endif
|
1994-05-23 09:43:41 -03:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
void
|
2010-02-23 19:19:39 -04:00
|
|
|
PyThread_exit_thread(void)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_exit_thread called\n"));
|
2014-06-16 23:39:18 -03:00
|
|
|
if (!initialized)
|
2010-05-09 11:46:46 -03:00
|
|
|
exit(0);
|
2014-06-16 23:39:18 -03:00
|
|
|
pthread_exit(0);
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2002-03-17 05:53:51 -04:00
|
|
|
#ifdef USE_SEMAPHORES
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock support.
|
|
|
|
*/
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThread_type_lock
|
2002-03-17 05:53:51 -04:00
|
|
|
PyThread_allocate_lock(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
sem_t *lock;
|
|
|
|
int status, error = 0;
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_allocate_lock called\n"));
|
|
|
|
if (!initialized)
|
|
|
|
PyThread_init_thread();
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
lock = (sem_t *)malloc(sizeof(sem_t));
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (lock) {
|
|
|
|
status = sem_init(lock,0,1);
|
|
|
|
CHECK_STATUS("sem_init");
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (error) {
|
|
|
|
free((void *)lock);
|
|
|
|
lock = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
|
|
|
|
return (PyThread_type_lock)lock;
|
2002-03-17 05:53:51 -04:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
void
|
2002-03-17 05:53:51 -04:00
|
|
|
PyThread_free_lock(PyThread_type_lock lock)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
sem_t *thelock = (sem_t *)lock;
|
|
|
|
int status, error = 0;
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2012-12-05 09:41:11 -04:00
|
|
|
(void) error; /* silence unused-but-set-variable warning */
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_free_lock(%p) called\n", lock));
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!thelock)
|
|
|
|
return;
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = sem_destroy(thelock);
|
|
|
|
CHECK_STATUS("sem_destroy");
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
free((void *)thelock);
|
2002-03-17 05:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return (status == -1) ? errno : status;
|
2002-03-17 05:53:51 -04:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
int
|
2002-03-17 05:53:51 -04:00
|
|
|
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int success;
|
|
|
|
sem_t *thelock = (sem_t *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
2012-12-05 09:41:11 -04:00
|
|
|
(void) error; /* silence unused-but-set-variable warning */
|
2010-05-09 11:46:46 -03:00
|
|
|
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;
|
2002-03-17 05:53:51 -04:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
void
|
2002-03-17 05:53:51 -04:00
|
|
|
PyThread_release_lock(PyThread_type_lock lock)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
sem_t *thelock = (sem_t *)lock;
|
|
|
|
int status, error = 0;
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2012-12-05 09:41:11 -04:00
|
|
|
(void) error; /* silence unused-but-set-variable warning */
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_release_lock(%p) called\n", lock));
|
2002-03-17 05:53:51 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = sem_post(thelock);
|
|
|
|
CHECK_STATUS("sem_post");
|
2002-03-17 05:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* USE_SEMAPHORES */
|
|
|
|
|
1994-05-09 12:12:46 -03:00
|
|
|
/*
|
|
|
|
* Lock support.
|
|
|
|
*/
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThread_type_lock
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_allocate_lock(void)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_lock *lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_allocate_lock called\n"));
|
|
|
|
if (!initialized)
|
|
|
|
PyThread_init_thread();
|
|
|
|
|
|
|
|
lock = (pthread_lock *) malloc(sizeof(pthread_lock));
|
|
|
|
if (lock) {
|
|
|
|
memset((void *)lock, '\0', sizeof(pthread_lock));
|
|
|
|
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) {
|
|
|
|
free((void *)lock);
|
|
|
|
lock = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
|
|
|
|
return (PyThread_type_lock) lock;
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_free_lock(PyThread_type_lock lock)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_lock *thelock = (pthread_lock *)lock;
|
|
|
|
int status, error = 0;
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2013-06-18 17:17:48 -03:00
|
|
|
(void) error; /* silence unused-but-set-variable warning */
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_free_lock(%p) called\n", lock));
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = pthread_mutex_destroy( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_destroy");
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = pthread_cond_destroy( &thelock->lock_released );
|
|
|
|
CHECK_STATUS("pthread_cond_destroy");
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
free((void *)thelock);
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int success;
|
|
|
|
pthread_lock *thelock = (pthread_lock *)lock;
|
|
|
|
int status, error = 0;
|
|
|
|
|
|
|
|
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
if (success) thelock->locked = 1;
|
|
|
|
status = pthread_mutex_unlock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_unlock[1]");
|
|
|
|
|
|
|
|
if (error) success = 0;
|
|
|
|
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
|
|
|
|
return success;
|
1994-05-09 12:12:46 -03:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThread_release_lock(PyThread_type_lock lock)
|
1994-05-09 12:12:46 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_lock *thelock = (pthread_lock *)lock;
|
|
|
|
int status, error = 0;
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2013-06-18 17:17:48 -03:00
|
|
|
(void) error; /* silence unused-but-set-variable warning */
|
2010-05-09 11:46:46 -03:00
|
|
|
dprintf(("PyThread_release_lock(%p) called\n", lock));
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = pthread_mutex_lock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_lock[3]");
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
thelock->locked = 0;
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
status = pthread_mutex_unlock( &thelock->mut );
|
|
|
|
CHECK_STATUS("pthread_mutex_unlock[3]");
|
1994-05-11 05:42:04 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* 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)
|
2010-05-09 11:46:46 -03:00
|
|
|
pthread_attr_t attrs;
|
|
|
|
size_t tss_min;
|
|
|
|
int rc = 0;
|
2006-06-13 12:04:24 -03:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* set to default */
|
|
|
|
if (size == 0) {
|
|
|
|
_pythread_stacksize = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-13 12:04:24 -03:00
|
|
|
|
|
|
|
#if defined(THREAD_STACK_SIZE)
|
|
|
|
#if defined(PTHREAD_STACK_MIN)
|
2010-05-09 11:46:46 -03:00
|
|
|
tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
|
|
|
|
: THREAD_STACK_MIN;
|
2006-06-13 12:04:24 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
tss_min = THREAD_STACK_MIN;
|
2006-06-13 12:04:24 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
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;
|
2006-06-13 12:04:24 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
return -2;
|
2006-06-13 12:04:24 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
|