TGreenletGlobals.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
  2. /**
  3. * Implementation of GreenletGlobals.
  4. *
  5. * Format with:
  6. * clang-format -i --style=file src/greenlet/greenlet.c
  7. *
  8. *
  9. * Fix missing braces with:
  10. * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
  11. */
  12. #ifndef T_GREENLET_GLOBALS
  13. #define T_GREENLET_GLOBALS
  14. #include "greenlet_refs.hpp"
  15. #include "greenlet_exceptions.hpp"
  16. #include "greenlet_thread_support.hpp"
  17. #include "greenlet_internal.hpp"
  18. namespace greenlet {
  19. // This encapsulates what were previously module global "constants"
  20. // established at init time.
  21. // This is a step towards Python3 style module state that allows
  22. // reloading.
  23. //
  24. // In an earlier iteration of this code, we used placement new to be
  25. // able to allocate this object statically still, so that references
  26. // to its members don't incur an extra pointer indirection.
  27. // But under some scenarios, that could result in crashes at
  28. // shutdown because apparently the destructor was getting run twice?
  29. class GreenletGlobals
  30. {
  31. public:
  32. const greenlet::refs::ImmortalEventName event_switch;
  33. const greenlet::refs::ImmortalEventName event_throw;
  34. const greenlet::refs::ImmortalException PyExc_GreenletError;
  35. const greenlet::refs::ImmortalException PyExc_GreenletExit;
  36. const greenlet::refs::ImmortalObject empty_tuple;
  37. const greenlet::refs::ImmortalObject empty_dict;
  38. const greenlet::refs::ImmortalString str_run;
  39. Mutex* const thread_states_to_destroy_lock;
  40. greenlet::cleanup_queue_t thread_states_to_destroy;
  41. GreenletGlobals() :
  42. event_switch("switch"),
  43. event_throw("throw"),
  44. PyExc_GreenletError("greenlet.error"),
  45. PyExc_GreenletExit("greenlet.GreenletExit", PyExc_BaseException),
  46. empty_tuple(Require(PyTuple_New(0))),
  47. empty_dict(Require(PyDict_New())),
  48. str_run("run"),
  49. thread_states_to_destroy_lock(new Mutex())
  50. {}
  51. ~GreenletGlobals()
  52. {
  53. // This object is (currently) effectively immortal, and not
  54. // just because of those placement new tricks; if we try to
  55. // deallocate the static object we allocated, and overwrote,
  56. // we would be doing so at C++ teardown time, which is after
  57. // the final Python GIL is released, and we can't use the API
  58. // then.
  59. // (The members will still be destructed, but they also don't
  60. // do any deallocation.)
  61. }
  62. void queue_to_destroy(ThreadState* ts) const
  63. {
  64. // we're currently accessed through a static const object,
  65. // implicitly marking our members as const, so code can't just
  66. // call push_back (or pop_back) without casting away the
  67. // const.
  68. //
  69. // Do that for callers.
  70. greenlet::cleanup_queue_t& q = const_cast<greenlet::cleanup_queue_t&>(this->thread_states_to_destroy);
  71. q.push_back(ts);
  72. }
  73. ThreadState* take_next_to_destroy() const
  74. {
  75. greenlet::cleanup_queue_t& q = const_cast<greenlet::cleanup_queue_t&>(this->thread_states_to_destroy);
  76. ThreadState* result = q.back();
  77. q.pop_back();
  78. return result;
  79. }
  80. };
  81. }; // namespace greenlet
  82. static const greenlet::GreenletGlobals* mod_globs;
  83. #endif // T_GREENLET_GLOBALS