• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_GIL_H
2 #define Py_INTERNAL_GIL_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 #include "pycore_condvar.h"       // PyCOND_T
12 
13 #ifndef Py_HAVE_CONDVAR
14 #  error You need either a POSIX-compatible or a Windows system!
15 #endif
16 
17 /* Enable if you want to force the switching of threads at least
18    every `interval`. */
19 #undef FORCE_SWITCHING
20 #define FORCE_SWITCHING
21 
22 struct _gil_runtime_state {
23 #ifdef Py_GIL_DISABLED
24     /* If this GIL is disabled, enabled == 0.
25 
26        If this GIL is enabled transiently (most likely to initialize a module
27        of unknown safety), enabled indicates the number of active transient
28        requests.
29 
30        If this GIL is enabled permanently, enabled == INT_MAX.
31 
32        It must not be modified directly; use _PyEval_EnableGILTransiently(),
33        _PyEval_EnableGILPermanently(), and _PyEval_DisableGIL()
34 
35        It is always read and written atomically, but a thread can assume its
36        value will be stable as long as that thread is attached or knows that no
37        other threads are attached (e.g., during a stop-the-world.). */
38     int enabled;
39 #endif
40     /* microseconds (the Python API uses seconds, though) */
41     unsigned long interval;
42     /* Last PyThreadState holding / having held the GIL. This helps us
43        know whether anyone else was scheduled after we dropped the GIL. */
44     PyThreadState* last_holder;
45     /* Whether the GIL is already taken (-1 if uninitialized). This is
46        atomic because it can be read without any lock taken in ceval.c. */
47     int locked;
48     /* Number of GIL switches since the beginning. */
49     unsigned long switch_number;
50     /* This condition variable allows one or several threads to wait
51        until the GIL is released. In addition, the mutex also protects
52        the above variables. */
53     PyCOND_T cond;
54     PyMUTEX_T mutex;
55 #ifdef FORCE_SWITCHING
56     /* This condition variable helps the GIL-releasing thread wait for
57        a GIL-awaiting thread to be scheduled and take the GIL. */
58     PyCOND_T switch_cond;
59     PyMUTEX_T switch_mutex;
60 #endif
61 };
62 
63 #ifdef __cplusplus
64 }
65 #endif
66 #endif /* !Py_INTERNAL_GIL_H */
67