atexit.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "atexit.h"
  2. #include "yassert.h"
  3. #include "spinlock.h"
  4. #include "thread.h"
  5. #include <util/generic/ylimits.h>
  6. #include <util/generic/utility.h>
  7. #include <util/generic/deque.h>
  8. #include <util/generic/queue.h>
  9. #include <atomic>
  10. #include <tuple>
  11. #include <cstdlib>
  12. namespace {
  13. class TAtExit {
  14. struct TFunc {
  15. TAtExitFunc Func;
  16. void* Ctx;
  17. size_t Priority;
  18. size_t Number;
  19. };
  20. struct TCmp {
  21. inline bool operator()(const TFunc* l, const TFunc* r) const noexcept {
  22. return std::tie(l->Priority, l->Number) < std::tie(r->Priority, r->Number);
  23. }
  24. };
  25. public:
  26. inline TAtExit() noexcept
  27. : FinishStarted_(false)
  28. {
  29. }
  30. inline void Finish() noexcept {
  31. FinishStarted_.store(true);
  32. if (ExitHandlersDisabled_.load()) {
  33. return;
  34. }
  35. auto guard = Guard(Lock_);
  36. while (Items_) {
  37. auto c = Items_.top();
  38. Y_ASSERT(c);
  39. Items_.pop();
  40. {
  41. auto unguard = Unguard(guard);
  42. try {
  43. c->Func(c->Ctx);
  44. } catch (...) {
  45. // ¯\_(ツ)_/¯
  46. }
  47. }
  48. }
  49. }
  50. inline void Register(TAtExitFunc func, void* ctx, size_t priority) {
  51. with_lock (Lock_) {
  52. Store_.push_back({func, ctx, priority, Store_.size()});
  53. Items_.push(&Store_.back());
  54. }
  55. }
  56. inline bool FinishStarted() const {
  57. return FinishStarted_.load();
  58. }
  59. inline void DisableExitHandlers() {
  60. ExitHandlersDisabled_.store(true);
  61. }
  62. private:
  63. TAdaptiveLock Lock_;
  64. std::atomic<bool> FinishStarted_;
  65. TDeque<TFunc> Store_;
  66. TPriorityQueue<TFunc*, TVector<TFunc*>, TCmp> Items_;
  67. std::atomic<bool> ExitHandlersDisabled_{false};
  68. };
  69. static TAdaptiveLock atExitLock;
  70. static std::atomic<TAtExit*> atExitPtr = nullptr;
  71. alignas(TAtExit) static char atExitMem[sizeof(TAtExit)];
  72. static void OnExit() {
  73. if (TAtExit* const atExit = atExitPtr.load()) {
  74. atExit->Finish();
  75. atExit->~TAtExit();
  76. atExitPtr.store(nullptr);
  77. }
  78. }
  79. static inline TAtExit* Instance() {
  80. if (TAtExit* const atExit = atExitPtr.load(std::memory_order_acquire)) {
  81. return atExit;
  82. }
  83. with_lock (atExitLock) {
  84. if (TAtExit* const atExit = atExitPtr.load()) {
  85. return atExit;
  86. }
  87. atexit(OnExit);
  88. TAtExit* const atExit = new (atExitMem) TAtExit;
  89. atExitPtr.store(atExit, std::memory_order_release);
  90. return atExit;
  91. }
  92. }
  93. } // namespace
  94. void ManualRunAtExitFinalizers() {
  95. OnExit();
  96. }
  97. bool ExitStarted() {
  98. if (TAtExit* const atExit = atExitPtr.load(std::memory_order_acquire)) {
  99. return atExit->FinishStarted();
  100. }
  101. return false;
  102. }
  103. void AtExit(TAtExitFunc func, void* ctx, size_t priority) {
  104. Instance()->Register(func, ctx, priority);
  105. }
  106. void AtExit(TAtExitFunc func, void* ctx) {
  107. AtExit(func, ctx, Max<size_t>());
  108. }
  109. static void TraditionalCloser(void* ctx) {
  110. reinterpret_cast<TTraditionalAtExitFunc>(ctx)();
  111. }
  112. void AtExit(TTraditionalAtExitFunc func) {
  113. AtExit(TraditionalCloser, reinterpret_cast<void*>(func));
  114. }
  115. void AtExit(TTraditionalAtExitFunc func, size_t priority) {
  116. AtExit(TraditionalCloser, reinterpret_cast<void*>(func), priority);
  117. }
  118. void DisableExitHandlers() {
  119. Instance()->DisableExitHandlers();
  120. }