1#ifndef Py_INTERNAL_CONDVAR_H
2#define Py_INTERNAL_CONDVAR_H
3
4#ifndef Py_BUILD_CORE
5#  error "this header requires Py_BUILD_CORE define"
6#endif
7
8#ifndef _POSIX_THREADS
9/* This means pthreads are not implemented in libc headers, hence the macro
10   not present in unistd.h. But they still can be implemented as an external
11   library (e.g. gnu pth in pthread emulation) */
12# ifdef HAVE_PTHREAD_H
13#  include <pthread.h> /* _POSIX_THREADS */
14# endif
15#endif
16
17#ifdef _POSIX_THREADS
18/*
19 * POSIX support
20 */
21#define Py_HAVE_CONDVAR
22
23#ifdef HAVE_PTHREAD_H
24#  include <pthread.h>
25#endif
26
27#define PyMUTEX_T pthread_mutex_t
28#define PyCOND_T pthread_cond_t
29
30#elif defined(NT_THREADS)
31/*
32 * Windows (XP, 2003 server and later, as well as (hopefully) CE) support
33 *
34 * Emulated condition variables ones that work with XP and later, plus
35 * example native support on VISTA and onwards.
36 */
37#define Py_HAVE_CONDVAR
38
39/* include windows if it hasn't been done before */
40#define WIN32_LEAN_AND_MEAN
41#include <windows.h>
42
43/* options */
44/* non-emulated condition variables are provided for those that want
45 * to target Windows Vista.  Modify this macro to enable them.
46 */
47#ifndef _PY_EMULATED_WIN_CV
48#define _PY_EMULATED_WIN_CV 1  /* use emulated condition variables */
49#endif
50
51/* fall back to emulation if not targeting Vista */
52#if !defined NTDDI_VISTA || NTDDI_VERSION < NTDDI_VISTA
53#undef _PY_EMULATED_WIN_CV
54#define _PY_EMULATED_WIN_CV 1
55#endif
56
57#if _PY_EMULATED_WIN_CV
58
59typedef CRITICAL_SECTION PyMUTEX_T;
60
61/* The ConditionVariable object.  From XP onwards it is easily emulated
62   with a Semaphore.
63   Semaphores are available on Windows XP (2003 server) and later.
64   We use a Semaphore rather than an auto-reset event, because although
65   an auto-reset event might appear to solve the lost-wakeup bug (race
66   condition between releasing the outer lock and waiting) because it
67   maintains state even though a wait hasn't happened, there is still
68   a lost wakeup problem if more than one thread are interrupted in the
69   critical place.  A semaphore solves that, because its state is
70   counted, not Boolean.
71   Because it is ok to signal a condition variable with no one
72   waiting, we need to keep track of the number of
73   waiting threads.  Otherwise, the semaphore's state could rise
74   without bound.  This also helps reduce the number of "spurious wakeups"
75   that would otherwise happen.
76 */
77
78typedef struct _PyCOND_T
79{
80    HANDLE sem;
81    int waiting; /* to allow PyCOND_SIGNAL to be a no-op */
82} PyCOND_T;
83
84#else /* !_PY_EMULATED_WIN_CV */
85
86/* Use native Win7 primitives if build target is Win7 or higher */
87
88/* SRWLOCK is faster and better than CriticalSection */
89typedef SRWLOCK PyMUTEX_T;
90
91typedef CONDITION_VARIABLE  PyCOND_T;
92
93#endif /* _PY_EMULATED_WIN_CV */
94
95#endif /* _POSIX_THREADS, NT_THREADS */
96
97#endif /* Py_INTERNAL_CONDVAR_H */
98