TMainGreenlet.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
  2. /**
  3. * Implementation of greenlet::MainGreenlet.
  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_MAIN_GREENLET_CPP
  13. #define T_MAIN_GREENLET_CPP
  14. #include "TGreenlet.hpp"
  15. // Protected by the GIL. Incremented when we create a main greenlet,
  16. // in a new thread, decremented when it is destroyed.
  17. static Py_ssize_t G_TOTAL_MAIN_GREENLETS;
  18. namespace greenlet {
  19. greenlet::PythonAllocator<MainGreenlet> MainGreenlet::allocator;
  20. void* MainGreenlet::operator new(size_t UNUSED(count))
  21. {
  22. return allocator.allocate(1);
  23. }
  24. void MainGreenlet::operator delete(void* ptr)
  25. {
  26. return allocator.deallocate(static_cast<MainGreenlet*>(ptr),
  27. 1);
  28. }
  29. MainGreenlet::MainGreenlet(PyGreenlet* p, ThreadState* state)
  30. : Greenlet(p, StackState::make_main()),
  31. _self(p),
  32. _thread_state(state)
  33. {
  34. G_TOTAL_MAIN_GREENLETS++;
  35. }
  36. MainGreenlet::~MainGreenlet()
  37. {
  38. G_TOTAL_MAIN_GREENLETS--;
  39. this->tp_clear();
  40. }
  41. ThreadState*
  42. MainGreenlet::thread_state() const noexcept
  43. {
  44. return this->_thread_state;
  45. }
  46. void
  47. MainGreenlet::thread_state(ThreadState* t) noexcept
  48. {
  49. assert(!t);
  50. this->_thread_state = t;
  51. }
  52. const BorrowedMainGreenlet
  53. MainGreenlet::main_greenlet() const
  54. {
  55. return this->_self;
  56. }
  57. BorrowedMainGreenlet
  58. MainGreenlet::find_main_greenlet_in_lineage() const
  59. {
  60. return BorrowedMainGreenlet(this->_self);
  61. }
  62. bool
  63. MainGreenlet::was_running_in_dead_thread() const noexcept
  64. {
  65. return !this->_thread_state;
  66. }
  67. OwnedObject
  68. MainGreenlet::g_switch()
  69. {
  70. try {
  71. this->check_switch_allowed();
  72. }
  73. catch (const PyErrOccurred&) {
  74. this->release_args();
  75. throw;
  76. }
  77. switchstack_result_t err = this->g_switchstack();
  78. if (err.status < 0) {
  79. // XXX: This code path is untested, but it is shared
  80. // with the UserGreenlet path that is tested.
  81. return this->on_switchstack_or_initialstub_failure(
  82. this,
  83. err,
  84. true, // target was me
  85. false // was initial stub
  86. );
  87. }
  88. return err.the_new_current_greenlet->g_switch_finish(err);
  89. }
  90. int
  91. MainGreenlet::tp_traverse(visitproc visit, void* arg)
  92. {
  93. if (this->_thread_state) {
  94. // we've already traversed main, (self), don't do it again.
  95. int result = this->_thread_state->tp_traverse(visit, arg, false);
  96. if (result) {
  97. return result;
  98. }
  99. }
  100. return Greenlet::tp_traverse(visit, arg);
  101. }
  102. const OwnedObject&
  103. MainGreenlet::run() const
  104. {
  105. throw AttributeError("Main greenlets do not have a run attribute.");
  106. }
  107. void
  108. MainGreenlet::run(const BorrowedObject UNUSED(nrun))
  109. {
  110. throw AttributeError("Main greenlets do not have a run attribute.");
  111. }
  112. void
  113. MainGreenlet::parent(const BorrowedObject raw_new_parent)
  114. {
  115. if (!raw_new_parent) {
  116. throw AttributeError("can't delete attribute");
  117. }
  118. throw AttributeError("cannot set the parent of a main greenlet");
  119. }
  120. const OwnedGreenlet
  121. MainGreenlet::parent() const
  122. {
  123. return OwnedGreenlet(); // null becomes None
  124. }
  125. }; // namespace greenlet
  126. #endif