Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:45

0001 #ifndef Py_INTERNAL_CONDVAR_H
0002 #define Py_INTERNAL_CONDVAR_H
0003 
0004 #ifndef Py_BUILD_CORE
0005 #  error "this header requires Py_BUILD_CORE define"
0006 #endif
0007 
0008 #include "pycore_pythread.h"      // _POSIX_THREADS
0009 
0010 
0011 #ifdef _POSIX_THREADS
0012 /*
0013  * POSIX support
0014  */
0015 #define Py_HAVE_CONDVAR
0016 
0017 #ifdef HAVE_PTHREAD_H
0018 #  include <pthread.h>            // pthread_mutex_t
0019 #endif
0020 
0021 #define PyMUTEX_T pthread_mutex_t
0022 #define PyCOND_T pthread_cond_t
0023 
0024 #elif defined(NT_THREADS)
0025 /*
0026  * Windows (XP, 2003 server and later, as well as (hopefully) CE) support
0027  *
0028  * Emulated condition variables ones that work with XP and later, plus
0029  * example native support on VISTA and onwards.
0030  */
0031 #define Py_HAVE_CONDVAR
0032 
0033 /* include windows if it hasn't been done before */
0034 #ifndef WIN32_LEAN_AND_MEAN
0035 #  define WIN32_LEAN_AND_MEAN
0036 #endif
0037 #include <windows.h>              // CRITICAL_SECTION
0038 
0039 /* options */
0040 /* emulated condition variables are provided for those that want
0041  * to target Windows XP or earlier. Modify this macro to enable them.
0042  */
0043 #ifndef _PY_EMULATED_WIN_CV
0044 #define _PY_EMULATED_WIN_CV 0  /* use non-emulated condition variables */
0045 #endif
0046 
0047 /* fall back to emulation if targeting earlier than Vista */
0048 #if !defined NTDDI_VISTA || NTDDI_VERSION < NTDDI_VISTA
0049 #undef _PY_EMULATED_WIN_CV
0050 #define _PY_EMULATED_WIN_CV 1
0051 #endif
0052 
0053 #if _PY_EMULATED_WIN_CV
0054 
0055 typedef CRITICAL_SECTION PyMUTEX_T;
0056 
0057 /* The ConditionVariable object.  From XP onwards it is easily emulated
0058    with a Semaphore.
0059    Semaphores are available on Windows XP (2003 server) and later.
0060    We use a Semaphore rather than an auto-reset event, because although
0061    an auto-reset event might appear to solve the lost-wakeup bug (race
0062    condition between releasing the outer lock and waiting) because it
0063    maintains state even though a wait hasn't happened, there is still
0064    a lost wakeup problem if more than one thread are interrupted in the
0065    critical place.  A semaphore solves that, because its state is
0066    counted, not Boolean.
0067    Because it is ok to signal a condition variable with no one
0068    waiting, we need to keep track of the number of
0069    waiting threads.  Otherwise, the semaphore's state could rise
0070    without bound.  This also helps reduce the number of "spurious wakeups"
0071    that would otherwise happen.
0072  */
0073 
0074 typedef struct _PyCOND_T
0075 {
0076     HANDLE sem;
0077     int waiting; /* to allow PyCOND_SIGNAL to be a no-op */
0078 } PyCOND_T;
0079 
0080 #else /* !_PY_EMULATED_WIN_CV */
0081 
0082 /* Use native Windows primitives if build target is Vista or higher */
0083 
0084 /* SRWLOCK is faster and better than CriticalSection */
0085 typedef SRWLOCK PyMUTEX_T;
0086 
0087 typedef CONDITION_VARIABLE  PyCOND_T;
0088 
0089 #endif /* _PY_EMULATED_WIN_CV */
0090 
0091 #endif /* _POSIX_THREADS, NT_THREADS */
0092 
0093 #endif /* Py_INTERNAL_CONDVAR_H */