Debian Codegraph @

Include/internal/pycore_condvar.h

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