You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
8.4 KiB

  1. /*
  2. * Implementation of the Global Interpreter Lock (GIL).
  3. */
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include "pycore_atomic.h"
  7. /*
  8. Notes about the implementation:
  9. - The GIL is just a boolean variable (locked) whose access is protected
  10. by a mutex (gil_mutex), and whose changes are signalled by a condition
  11. variable (gil_cond). gil_mutex is taken for short periods of time,
  12. and therefore mostly uncontended.
  13. - In the GIL-holding thread, the main loop (PyEval_EvalFrameEx) must be
  14. able to release the GIL on demand by another thread. A volatile boolean
  15. variable (gil_drop_request) is used for that purpose, which is checked
  16. at every turn of the eval loop. That variable is set after a wait of
  17. `interval` microseconds on `gil_cond` has timed out.
  18. [Actually, another volatile boolean variable (eval_breaker) is used
  19. which ORs several conditions into one. Volatile booleans are
  20. sufficient as inter-thread signalling means since Python is run
  21. on cache-coherent architectures only.]
  22. - A thread wanting to take the GIL will first let pass a given amount of
  23. time (`interval` microseconds) before setting gil_drop_request. This
  24. encourages a defined switching period, but doesn't enforce it since
  25. opcodes can take an arbitrary time to execute.
  26. The `interval` value is available for the user to read and modify
  27. using the Python API `sys.{get,set}switchinterval()`.
  28. - When a thread releases the GIL and gil_drop_request is set, that thread
  29. ensures that another GIL-awaiting thread gets scheduled.
  30. It does so by waiting on a condition variable (switch_cond) until
  31. the value of last_holder is changed to something else than its
  32. own thread state pointer, indicating that another thread was able to
  33. take the GIL.
  34. This is meant to prohibit the latency-adverse behaviour on multi-core
  35. machines where one thread would speculatively release the GIL, but still
  36. run and end up being the first to re-acquire it, making the "timeslices"
  37. much longer than expected.
  38. (Note: this mechanism is enabled with FORCE_SWITCHING above)
  39. */
  40. #include "condvar.h"
  41. #define MUTEX_INIT(mut) \
  42. if (PyMUTEX_INIT(&(mut))) { \
  43. Py_FatalError("PyMUTEX_INIT(" #mut ") failed"); };
  44. #define MUTEX_FINI(mut) \
  45. if (PyMUTEX_FINI(&(mut))) { \
  46. Py_FatalError("PyMUTEX_FINI(" #mut ") failed"); };
  47. #define MUTEX_LOCK(mut) \
  48. if (PyMUTEX_LOCK(&(mut))) { \
  49. Py_FatalError("PyMUTEX_LOCK(" #mut ") failed"); };
  50. #define MUTEX_UNLOCK(mut) \
  51. if (PyMUTEX_UNLOCK(&(mut))) { \
  52. Py_FatalError("PyMUTEX_UNLOCK(" #mut ") failed"); };
  53. #define COND_INIT(cond) \
  54. if (PyCOND_INIT(&(cond))) { \
  55. Py_FatalError("PyCOND_INIT(" #cond ") failed"); };
  56. #define COND_FINI(cond) \
  57. if (PyCOND_FINI(&(cond))) { \
  58. Py_FatalError("PyCOND_FINI(" #cond ") failed"); };
  59. #define COND_SIGNAL(cond) \
  60. if (PyCOND_SIGNAL(&(cond))) { \
  61. Py_FatalError("PyCOND_SIGNAL(" #cond ") failed"); };
  62. #define COND_WAIT(cond, mut) \
  63. if (PyCOND_WAIT(&(cond), &(mut))) { \
  64. Py_FatalError("PyCOND_WAIT(" #cond ") failed"); };
  65. #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \
  66. { \
  67. int r = PyCOND_TIMEDWAIT(&(cond), &(mut), (microseconds)); \
  68. if (r < 0) \
  69. Py_FatalError("PyCOND_WAIT(" #cond ") failed"); \
  70. if (r) /* 1 == timeout, 2 == impl. can't say, so assume timeout */ \
  71. timeout_result = 1; \
  72. else \
  73. timeout_result = 0; \
  74. } \
  75. #define DEFAULT_INTERVAL 5000
  76. static void _gil_initialize(struct _gil_runtime_state *gil)
  77. {
  78. _Py_atomic_int uninitialized = {-1};
  79. gil->locked = uninitialized;
  80. gil->interval = DEFAULT_INTERVAL;
  81. }
  82. static int gil_created(struct _gil_runtime_state *gil)
  83. {
  84. return (_Py_atomic_load_explicit(&gil->locked, _Py_memory_order_acquire) >= 0);
  85. }
  86. static void create_gil(struct _gil_runtime_state *gil)
  87. {
  88. MUTEX_INIT(gil->mutex);
  89. #ifdef FORCE_SWITCHING
  90. MUTEX_INIT(gil->switch_mutex);
  91. #endif
  92. COND_INIT(gil->cond);
  93. #ifdef FORCE_SWITCHING
  94. COND_INIT(gil->switch_cond);
  95. #endif
  96. _Py_atomic_store_relaxed(&gil->last_holder, 0);
  97. _Py_ANNOTATE_RWLOCK_CREATE(&gil->locked);
  98. _Py_atomic_store_explicit(&gil->locked, 0, _Py_memory_order_release);
  99. }
  100. static void destroy_gil(struct _gil_runtime_state *gil)
  101. {
  102. /* some pthread-like implementations tie the mutex to the cond
  103. * and must have the cond destroyed first.
  104. */
  105. COND_FINI(gil->cond);
  106. MUTEX_FINI(gil->mutex);
  107. #ifdef FORCE_SWITCHING
  108. COND_FINI(gil->switch_cond);
  109. MUTEX_FINI(gil->switch_mutex);
  110. #endif
  111. _Py_atomic_store_explicit(&gil->locked, -1,
  112. _Py_memory_order_release);
  113. _Py_ANNOTATE_RWLOCK_DESTROY(&gil->locked);
  114. }
  115. static void recreate_gil(struct _gil_runtime_state *gil)
  116. {
  117. _Py_ANNOTATE_RWLOCK_DESTROY(&gil->locked);
  118. /* XXX should we destroy the old OS resources here? */
  119. create_gil(gil);
  120. }
  121. static void
  122. drop_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate)
  123. {
  124. struct _gil_runtime_state *gil = &ceval->gil;
  125. if (!_Py_atomic_load_relaxed(&gil->locked)) {
  126. Py_FatalError("drop_gil: GIL is not locked");
  127. }
  128. /* tstate is allowed to be NULL (early interpreter init) */
  129. if (tstate != NULL) {
  130. /* Sub-interpreter support: threads might have been switched
  131. under our feet using PyThreadState_Swap(). Fix the GIL last
  132. holder variable so that our heuristics work. */
  133. _Py_atomic_store_relaxed(&gil->last_holder, (uintptr_t)tstate);
  134. }
  135. MUTEX_LOCK(gil->mutex);
  136. _Py_ANNOTATE_RWLOCK_RELEASED(&gil->locked, /*is_write=*/1);
  137. _Py_atomic_store_relaxed(&gil->locked, 0);
  138. COND_SIGNAL(gil->cond);
  139. MUTEX_UNLOCK(gil->mutex);
  140. #ifdef FORCE_SWITCHING
  141. if (_Py_atomic_load_relaxed(&ceval->gil_drop_request) && tstate != NULL) {
  142. MUTEX_LOCK(gil->switch_mutex);
  143. /* Not switched yet => wait */
  144. if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) == tstate)
  145. {
  146. RESET_GIL_DROP_REQUEST(ceval);
  147. /* NOTE: if COND_WAIT does not atomically start waiting when
  148. releasing the mutex, another thread can run through, take
  149. the GIL and drop it again, and reset the condition
  150. before we even had a chance to wait for it. */
  151. COND_WAIT(gil->switch_cond, gil->switch_mutex);
  152. }
  153. MUTEX_UNLOCK(gil->switch_mutex);
  154. }
  155. #endif
  156. }
  157. static void
  158. take_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate)
  159. {
  160. if (tstate == NULL) {
  161. Py_FatalError("take_gil: NULL tstate");
  162. }
  163. struct _gil_runtime_state *gil = &ceval->gil;
  164. int err = errno;
  165. MUTEX_LOCK(gil->mutex);
  166. if (!_Py_atomic_load_relaxed(&gil->locked)) {
  167. goto _ready;
  168. }
  169. while (_Py_atomic_load_relaxed(&gil->locked)) {
  170. int timed_out = 0;
  171. unsigned long saved_switchnum;
  172. saved_switchnum = gil->switch_number;
  173. unsigned long interval = (gil->interval >= 1 ? gil->interval : 1);
  174. COND_TIMED_WAIT(gil->cond, gil->mutex, interval, timed_out);
  175. /* If we timed out and no switch occurred in the meantime, it is time
  176. to ask the GIL-holding thread to drop it. */
  177. if (timed_out &&
  178. _Py_atomic_load_relaxed(&gil->locked) &&
  179. gil->switch_number == saved_switchnum)
  180. {
  181. SET_GIL_DROP_REQUEST(ceval);
  182. }
  183. }
  184. _ready:
  185. #ifdef FORCE_SWITCHING
  186. /* This mutex must be taken before modifying gil->last_holder:
  187. see drop_gil(). */
  188. MUTEX_LOCK(gil->switch_mutex);
  189. #endif
  190. /* We now hold the GIL */
  191. _Py_atomic_store_relaxed(&gil->locked, 1);
  192. _Py_ANNOTATE_RWLOCK_ACQUIRED(&gil->locked, /*is_write=*/1);
  193. if (tstate != (PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) {
  194. _Py_atomic_store_relaxed(&gil->last_holder, (uintptr_t)tstate);
  195. ++gil->switch_number;
  196. }
  197. #ifdef FORCE_SWITCHING
  198. COND_SIGNAL(gil->switch_cond);
  199. MUTEX_UNLOCK(gil->switch_mutex);
  200. #endif
  201. if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
  202. RESET_GIL_DROP_REQUEST(ceval);
  203. }
  204. if (tstate->async_exc != NULL) {
  205. _PyEval_SignalAsyncExc(ceval);
  206. }
  207. MUTEX_UNLOCK(gil->mutex);
  208. errno = err;
  209. }
  210. void _PyEval_SetSwitchInterval(unsigned long microseconds)
  211. {
  212. _PyRuntime.ceval.gil.interval = microseconds;
  213. }
  214. unsigned long _PyEval_GetSwitchInterval()
  215. {
  216. return _PyRuntime.ceval.gil.interval;
  217. }