pycore_condvar.h 2.8 KB

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