pycore_gil.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef Py_INTERNAL_GIL_H
  2. #define Py_INTERNAL_GIL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. #include "pycore_atomic.h" /* _Py_atomic_address */
  10. #include "pycore_condvar.h" /* PyCOND_T */
  11. #ifndef Py_HAVE_CONDVAR
  12. # error You need either a POSIX-compatible or a Windows system!
  13. #endif
  14. /* Enable if you want to force the switching of threads at least
  15. every `interval`. */
  16. #undef FORCE_SWITCHING
  17. #define FORCE_SWITCHING
  18. struct _gil_runtime_state {
  19. /* microseconds (the Python API uses seconds, though) */
  20. unsigned long interval;
  21. /* Last PyThreadState holding / having held the GIL. This helps us
  22. know whether anyone else was scheduled after we dropped the GIL. */
  23. _Py_atomic_address last_holder;
  24. /* Whether the GIL is already taken (-1 if uninitialized). This is
  25. atomic because it can be read without any lock taken in ceval.c. */
  26. _Py_atomic_int locked;
  27. /* Number of GIL switches since the beginning. */
  28. unsigned long switch_number;
  29. /* This condition variable allows one or several threads to wait
  30. until the GIL is released. In addition, the mutex also protects
  31. the above variables. */
  32. PyCOND_T cond;
  33. PyMUTEX_T mutex;
  34. #ifdef FORCE_SWITCHING
  35. /* This condition variable helps the GIL-releasing thread wait for
  36. a GIL-awaiting thread to be scheduled and take the GIL. */
  37. PyCOND_T switch_cond;
  38. PyMUTEX_T switch_mutex;
  39. #endif
  40. };
  41. #ifdef __cplusplus
  42. }
  43. #endif
  44. #endif /* !Py_INTERNAL_GIL_H */