Blame Python/condvar.h

rpm-build 2bd099
/*
rpm-build 2bd099
 * Portable condition variable support for windows and pthreads.
rpm-build 2bd099
 * Everything is inline, this header can be included where needed.
rpm-build 2bd099
 *
rpm-build 2bd099
 * APIs generally return 0 on success and non-zero on error,
rpm-build 2bd099
 * and the caller needs to use its platform's error mechanism to
rpm-build 2bd099
 * discover the error (errno, or GetLastError())
rpm-build 2bd099
 *
rpm-build 2bd099
 * Note that some implementations cannot distinguish between a
rpm-build 2bd099
 * condition variable wait time-out and successful wait. Most often
rpm-build 2bd099
 * the difference is moot anyway since the wait condition must be
rpm-build 2bd099
 * re-checked.
rpm-build 2bd099
 * PyCOND_TIMEDWAIT, in addition to returning negative on error,
rpm-build 2bd099
 * thus returns 0 on regular success, 1 on timeout
rpm-build 2bd099
 * or 2 if it can't tell.
rpm-build 2bd099
 *
rpm-build 2bd099
 * There are at least two caveats with using these condition variables,
rpm-build 2bd099
 * due to the fact that they may be emulated with Semaphores on
rpm-build 2bd099
 * Windows:
rpm-build 2bd099
 * 1) While PyCOND_SIGNAL() will wake up at least one thread, we
rpm-build 2bd099
 *    cannot currently guarantee that it will be one of the threads
rpm-build 2bd099
 *    already waiting in a PyCOND_WAIT() call.  It _could_ cause
rpm-build 2bd099
 *    the wakeup of a subsequent thread to try a PyCOND_WAIT(),
rpm-build 2bd099
 *    including the thread doing the PyCOND_SIGNAL() itself.
rpm-build 2bd099
 *    The same applies to PyCOND_BROADCAST(), if N threads are waiting
rpm-build 2bd099
 *    then at least N threads will be woken up, but not necessarily
rpm-build 2bd099
 *    those already waiting.
rpm-build 2bd099
 *    For this reason, don't make the scheduling assumption that a
rpm-build 2bd099
 *    specific other thread will get the wakeup signal
rpm-build 2bd099
 * 2) The _mutex_ must be held when calling PyCOND_SIGNAL() and
rpm-build 2bd099
 *    PyCOND_BROADCAST().
rpm-build 2bd099
 *    While e.g. the posix standard strongly recommends that the mutex
rpm-build 2bd099
 *    associated with the condition variable is held when a
rpm-build 2bd099
 *    pthread_cond_signal() call is made, this is not a hard requirement,
rpm-build 2bd099
 *    although scheduling will not be "reliable" if it isn't.  Here
rpm-build 2bd099
 *    the mutex is used for internal synchronization of the emulated
rpm-build 2bd099
 *    Condition Variable.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
#ifndef _CONDVAR_H_
rpm-build 2bd099
#define _CONDVAR_H_
rpm-build 2bd099
rpm-build 2bd099
#include "Python.h"
rpm-build 2bd099
rpm-build 2bd099
#ifndef _POSIX_THREADS
rpm-build 2bd099
/* This means pthreads are not implemented in libc headers, hence the macro
rpm-build 2bd099
   not present in unistd.h. But they still can be implemented as an external
rpm-build 2bd099
   library (e.g. gnu pth in pthread emulation) */
rpm-build 2bd099
# ifdef HAVE_PTHREAD_H
rpm-build 2bd099
#  include <pthread.h> /* _POSIX_THREADS */
rpm-build 2bd099
# endif
rpm-build 2bd099
#endif
rpm-build 2bd099
rpm-build 2bd099
#ifdef _POSIX_THREADS
rpm-build 2bd099
/*
rpm-build 2bd099
 * POSIX support
rpm-build 2bd099
 */
rpm-build 2bd099
#define Py_HAVE_CONDVAR
rpm-build 2bd099
rpm-build 2bd099
#include <pthread.h>
rpm-build 2bd099
rpm-build 2bd099
#define PyCOND_ADD_MICROSECONDS(tv, interval) \
rpm-build 2bd099
do { /* TODO: add overflow and truncation checks */ \
rpm-build 2bd099
    tv.tv_usec += (long) interval; \
rpm-build 2bd099
    tv.tv_sec += tv.tv_usec / 1000000; \
rpm-build 2bd099
    tv.tv_usec %= 1000000; \
rpm-build 2bd099
} while (0)
rpm-build 2bd099
rpm-build 2bd099
/* We assume all modern POSIX systems have gettimeofday() */
rpm-build 2bd099
#ifdef GETTIMEOFDAY_NO_TZ
rpm-build 2bd099
#define PyCOND_GETTIMEOFDAY(ptv) gettimeofday(ptv)
rpm-build 2bd099
#else
rpm-build 2bd099
#define PyCOND_GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
rpm-build 2bd099
#endif
rpm-build 2bd099
rpm-build 2bd099
/* The following functions return 0 on success, nonzero on error */
rpm-build 2bd099
#define PyMUTEX_T pthread_mutex_t
rpm-build 2bd099
#define PyMUTEX_INIT(mut)       pthread_mutex_init((mut), NULL)
rpm-build 2bd099
#define PyMUTEX_FINI(mut)       pthread_mutex_destroy(mut)
rpm-build 2bd099
#define PyMUTEX_LOCK(mut)       pthread_mutex_lock(mut)
rpm-build 2bd099
#define PyMUTEX_UNLOCK(mut)     pthread_mutex_unlock(mut)
rpm-build 2bd099
rpm-build 2bd099
#define PyCOND_T pthread_cond_t
rpm-build 2bd099
#define PyCOND_INIT(cond)       pthread_cond_init((cond), NULL)
rpm-build 2bd099
#define PyCOND_FINI(cond)       pthread_cond_destroy(cond)
rpm-build 2bd099
#define PyCOND_SIGNAL(cond)     pthread_cond_signal(cond)
rpm-build 2bd099
#define PyCOND_BROADCAST(cond)  pthread_cond_broadcast(cond)
rpm-build 2bd099
#define PyCOND_WAIT(cond, mut)  pthread_cond_wait((cond), (mut))
rpm-build 2bd099
rpm-build 2bd099
/* return 0 for success, 1 on timeout, -1 on error */
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long long us)
rpm-build 2bd099
{
rpm-build 2bd099
    int r;
rpm-build 2bd099
    struct timespec ts;
rpm-build 2bd099
    struct timeval deadline;
rpm-build 2bd099
rpm-build 2bd099
    PyCOND_GETTIMEOFDAY(&deadline);
rpm-build 2bd099
    PyCOND_ADD_MICROSECONDS(deadline, us);
rpm-build 2bd099
    ts.tv_sec = deadline.tv_sec;
rpm-build 2bd099
    ts.tv_nsec = deadline.tv_usec * 1000;
rpm-build 2bd099
rpm-build 2bd099
    r = pthread_cond_timedwait((cond), (mut), &ts);
rpm-build 2bd099
    if (r == ETIMEDOUT)
rpm-build 2bd099
        return 1;
rpm-build 2bd099
    else if (r)
rpm-build 2bd099
        return -1;
rpm-build 2bd099
    else
rpm-build 2bd099
        return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
#elif defined(NT_THREADS)
rpm-build 2bd099
/*
rpm-build 2bd099
 * Windows (XP, 2003 server and later, as well as (hopefully) CE) support
rpm-build 2bd099
 *
rpm-build 2bd099
 * Emulated condition variables ones that work with XP and later, plus
rpm-build 2bd099
 * example native support on VISTA and onwards.
rpm-build 2bd099
 */
rpm-build 2bd099
#define Py_HAVE_CONDVAR
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/* include windows if it hasn't been done before */
rpm-build 2bd099
#define WIN32_LEAN_AND_MEAN
rpm-build 2bd099
#include <windows.h>
rpm-build 2bd099
rpm-build 2bd099
/* options */
rpm-build 2bd099
/* non-emulated condition variables are provided for those that want
rpm-build 2bd099
 * to target Windows Vista.  Modify this macro to enable them.
rpm-build 2bd099
 */
rpm-build 2bd099
#ifndef _PY_EMULATED_WIN_CV
rpm-build 2bd099
#define _PY_EMULATED_WIN_CV 1  /* use emulated condition variables */
rpm-build 2bd099
#endif
rpm-build 2bd099
rpm-build 2bd099
/* fall back to emulation if not targeting Vista */
rpm-build 2bd099
#if !defined NTDDI_VISTA || NTDDI_VERSION < NTDDI_VISTA
rpm-build 2bd099
#undef _PY_EMULATED_WIN_CV
rpm-build 2bd099
#define _PY_EMULATED_WIN_CV 1
rpm-build 2bd099
#endif
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
#if _PY_EMULATED_WIN_CV
rpm-build 2bd099
rpm-build 2bd099
/* The mutex is a CriticalSection object and
rpm-build 2bd099
   The condition variables is emulated with the help of a semaphore.
rpm-build 2bd099
   Semaphores are available on Windows XP (2003 server) and later.
rpm-build 2bd099
   We use a Semaphore rather than an auto-reset event, because although
rpm-build 2bd099
   an auto-resent event might appear to solve the lost-wakeup bug (race
rpm-build 2bd099
   condition between releasing the outer lock and waiting) because it
rpm-build 2bd099
   maintains state even though a wait hasn't happened, there is still
rpm-build 2bd099
   a lost wakeup problem if more than one thread are interrupted in the
rpm-build 2bd099
   critical place.  A semaphore solves that, because its state is counted,
rpm-build 2bd099
   not Boolean.
rpm-build 2bd099
   Because it is ok to signal a condition variable with no one
rpm-build 2bd099
   waiting, we need to keep track of the number of
rpm-build 2bd099
   waiting threads.  Otherwise, the semaphore's state could rise
rpm-build 2bd099
   without bound.  This also helps reduce the number of "spurious wakeups"
rpm-build 2bd099
   that would otherwise happen.
rpm-build 2bd099
rpm-build 2bd099
   This implementation still has the problem that the threads woken
rpm-build 2bd099
   with a "signal" aren't necessarily those that are already
rpm-build 2bd099
   waiting.  It corresponds to listing 2 in:
rpm-build 2bd099
   http://birrell.org/andrew/papers/ImplementingCVs.pdf
rpm-build 2bd099
rpm-build 2bd099
   Generic emulations of the pthread_cond_* API using
rpm-build 2bd099
   earlier Win32 functions can be found on the Web.
rpm-build 2bd099
   The following read can be give background information to these issues,
rpm-build 2bd099
   but the implementations are all broken in some way.
rpm-build 2bd099
   http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
rpm-build 2bd099
*/
rpm-build 2bd099
rpm-build 2bd099
typedef CRITICAL_SECTION PyMUTEX_T;
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_INIT(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    InitializeCriticalSection(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_FINI(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    DeleteCriticalSection(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_LOCK(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    EnterCriticalSection(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_UNLOCK(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    LeaveCriticalSection(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/* The ConditionVariable object.  From XP onwards it is easily emulated with
rpm-build 2bd099
 * a Semaphore
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
typedef struct _PyCOND_T
rpm-build 2bd099
{
rpm-build 2bd099
    HANDLE sem;
rpm-build 2bd099
    int waiting; /* to allow PyCOND_SIGNAL to be a no-op */
rpm-build 2bd099
} PyCOND_T;
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_INIT(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
    /* A semaphore with a "large" max value,  The positive value
rpm-build 2bd099
     * is only needed to catch those "lost wakeup" events and
rpm-build 2bd099
     * race conditions when a timed wait elapses.
rpm-build 2bd099
     */
rpm-build 2bd099
    cv->sem = CreateSemaphore(NULL, 0, 100000, NULL);
rpm-build 2bd099
    if (cv->sem==NULL)
rpm-build 2bd099
        return -1;
rpm-build 2bd099
    cv->waiting = 0;
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_FINI(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
    return CloseHandle(cv->sem) ? 0 : -1;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/* this implementation can detect a timeout.  Returns 1 on timeout,
rpm-build 2bd099
 * 0 otherwise (and -1 on error)
rpm-build 2bd099
 */
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
_PyCOND_WAIT_MS(PyCOND_T *cv, PyMUTEX_T *cs, DWORD ms)
rpm-build 2bd099
{
rpm-build 2bd099
    DWORD wait;
rpm-build 2bd099
    cv->waiting++;
rpm-build 2bd099
    PyMUTEX_UNLOCK(cs);
rpm-build 2bd099
    /* "lost wakeup bug" would occur if the caller were interrupted here,
rpm-build 2bd099
     * but we are safe because we are using a semaphore which has an internal
rpm-build 2bd099
     * count.
rpm-build 2bd099
     */
rpm-build 2bd099
    wait = WaitForSingleObjectEx(cv->sem, ms, FALSE);
rpm-build 2bd099
    PyMUTEX_LOCK(cs);
rpm-build 2bd099
    if (wait != WAIT_OBJECT_0)
rpm-build 2bd099
        --cv->waiting;
rpm-build 2bd099
        /* Here we have a benign race condition with PyCOND_SIGNAL.
rpm-build 2bd099
         * When failure occurs or timeout, it is possible that
rpm-build 2bd099
         * PyCOND_SIGNAL also decrements this value
rpm-build 2bd099
         * and signals releases the mutex.  This is benign because it
rpm-build 2bd099
         * just means an extra spurious wakeup for a waiting thread.
rpm-build 2bd099
         * ('waiting' corresponds to the semaphore's "negative" count and
rpm-build 2bd099
         * we may end up with e.g. (waiting == -1 && sem.count == 1).  When
rpm-build 2bd099
         * a new thread comes along, it will pass right throuhgh, having
rpm-build 2bd099
         * adjusted it to (waiting == 0 && sem.count == 0).
rpm-build 2bd099
         */
rpm-build 2bd099
rpm-build 2bd099
    if (wait == WAIT_FAILED)
rpm-build 2bd099
        return -1;
rpm-build 2bd099
    /* return 0 on success, 1 on timeout */
rpm-build 2bd099
    return wait != WAIT_OBJECT_0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    int result = _PyCOND_WAIT_MS(cv, cs, INFINITE);
rpm-build 2bd099
    return result >= 0 ? 0 : result;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long long us)
rpm-build 2bd099
{
rpm-build 2bd099
    return _PyCOND_WAIT_MS(cv, cs, (DWORD)(us/1000));
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_SIGNAL(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
    /* this test allows PyCOND_SIGNAL to be a no-op unless required
rpm-build 2bd099
     * to wake someone up, thus preventing an unbounded increase of
rpm-build 2bd099
     * the semaphore's internal counter.
rpm-build 2bd099
     */
rpm-build 2bd099
    if (cv->waiting > 0) {
rpm-build 2bd099
        /* notifying thread decreases the cv->waiting count so that
rpm-build 2bd099
         * a delay between notify and actual wakeup of the target thread
rpm-build 2bd099
         * doesn't cause a number of extra ReleaseSemaphore calls.
rpm-build 2bd099
         */
rpm-build 2bd099
        cv->waiting--;
rpm-build 2bd099
        return ReleaseSemaphore(cv->sem, 1, NULL) ? 0 : -1;
rpm-build 2bd099
    }
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_BROADCAST(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
    int waiting = cv->waiting;
rpm-build 2bd099
    if (waiting > 0) {
rpm-build 2bd099
        cv->waiting = 0;
rpm-build 2bd099
        return ReleaseSemaphore(cv->sem, waiting, NULL) ? 0 : -1;
rpm-build 2bd099
    }
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
#else
rpm-build 2bd099
rpm-build 2bd099
/* Use native Win7 primitives if build target is Win7 or higher */
rpm-build 2bd099
rpm-build 2bd099
/* SRWLOCK is faster and better than CriticalSection */
rpm-build 2bd099
typedef SRWLOCK PyMUTEX_T;
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_INIT(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    InitializeSRWLock(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_FINI(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_LOCK(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    AcquireSRWLockExclusive(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyMUTEX_UNLOCK(PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    ReleaseSRWLockExclusive(cs);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
typedef CONDITION_VARIABLE  PyCOND_T;
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_INIT(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
    InitializeConditionVariable(cv);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_FINI(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
rpm-build 2bd099
{
rpm-build 2bd099
    return SleepConditionVariableSRW(cv, cs, INFINITE, 0) ? 0 : -1;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/* This implementation makes no distinction about timeouts.  Signal
rpm-build 2bd099
 * 2 to indicate that we don't know.
rpm-build 2bd099
 */
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long long us)
rpm-build 2bd099
{
rpm-build 2bd099
    return SleepConditionVariableSRW(cv, cs, (DWORD)(us/1000), 0) ? 2 : -1;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_SIGNAL(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
     WakeConditionVariable(cv);
rpm-build 2bd099
     return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
Py_LOCAL_INLINE(int)
rpm-build 2bd099
PyCOND_BROADCAST(PyCOND_T *cv)
rpm-build 2bd099
{
rpm-build 2bd099
     WakeAllConditionVariable(cv);
rpm-build 2bd099
     return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
#endif /* _PY_EMULATED_WIN_CV */
rpm-build 2bd099
rpm-build 2bd099
#endif /* _POSIX_THREADS, NT_THREADS */
rpm-build 2bd099
rpm-build 2bd099
#endif /* _CONDVAR_H_ */