#ifndef Py_INTERNAL_CONDVAR_H1#define Py_INTERNAL_CONDVAR_H23#ifndef Py_BUILD_CORE4# error "this header requires Py_BUILD_CORE define"5#endif67#ifndef _POSIX_THREADS8/* This means pthreads are not implemented in libc headers, hence the macro9not present in unistd.h. But they still can be implemented as an external10library (e.g. gnu pth in pthread emulation) */11# ifdef HAVE_PTHREAD_H12# include <pthread.h> /* _POSIX_THREADS */13# endif14#endif1516#ifdef _POSIX_THREADS17/*18* POSIX support19*/20#define Py_HAVE_CONDVAR2122#ifdef HAVE_PTHREAD_H23# include <pthread.h>24#endif2526#define PyMUTEX_T pthread_mutex_t27#define PyCOND_T pthread_cond_t2829#elif defined(NT_THREADS)30/*31* Windows (XP, 2003 server and later, as well as (hopefully) CE) support32*33* Emulated condition variables ones that work with XP and later, plus34* example native support on VISTA and onwards.35*/36#define Py_HAVE_CONDVAR3738/* include windows if it hasn't been done before */39#define WIN32_LEAN_AND_MEAN40#include <windows.h>4142/* options */43/* non-emulated condition variables are provided for those that want44* to target Windows Vista. Modify this macro to enable them.45*/46#ifndef _PY_EMULATED_WIN_CV47#define _PY_EMULATED_WIN_CV 1 /* use emulated condition variables */48#endif4950/* fall back to emulation if not targeting Vista */51#if !defined NTDDI_VISTA || NTDDI_VERSION < NTDDI_VISTA52#undef _PY_EMULATED_WIN_CV53#define _PY_EMULATED_WIN_CV 154#endif5556#if _PY_EMULATED_WIN_CV5758typedef CRITICAL_SECTION PyMUTEX_T;5960/* The ConditionVariable object. From XP onwards it is easily emulated61with a Semaphore.62Semaphores are available on Windows XP (2003 server) and later.63We use a Semaphore rather than an auto-reset event, because although64an auto-reset event might appear to solve the lost-wakeup bug (race65condition between releasing the outer lock and waiting) because it66maintains state even though a wait hasn't happened, there is still67a lost wakeup problem if more than one thread are interrupted in the68critical place. A semaphore solves that, because its state is69counted, not Boolean.70Because it is ok to signal a condition variable with no one71waiting, we need to keep track of the number of72waiting threads. Otherwise, the semaphore's state could rise73without bound. This also helps reduce the number of "spurious wakeups"74that would otherwise happen.75*/7677typedef struct _PyCOND_T78{79HANDLE sem;80int waiting; /* to allow PyCOND_SIGNAL to be a no-op */81} PyCOND_T;8283#else /* !_PY_EMULATED_WIN_CV */8485/* Use native Win7 primitives if build target is Win7 or higher */8687/* SRWLOCK is faster and better than CriticalSection */88typedef SRWLOCK PyMUTEX_T;8990typedef CONDITION_VARIABLE PyCOND_T;9192#endif /* _PY_EMULATED_WIN_CV */9394#endif /* _POSIX_THREADS, NT_THREADS */9596#endif /* Py_INTERNAL_CONDVAR_H */979899