Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:18:06

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