pthread_once.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Atomic.h>
  8. #include <AK/Types.h>
  9. #include <pthread.h>
  10. #include <serenity.h>
  11. enum State : i32 {
  12. INITIAL = PTHREAD_ONCE_INIT,
  13. DONE,
  14. PERFORMING_NO_WAITERS,
  15. PERFORMING_WITH_WAITERS,
  16. };
  17. int pthread_once(pthread_once_t* self, void (*callback)(void))
  18. {
  19. auto& state = reinterpret_cast<Atomic<State>&>(*self);
  20. // See what the current state is, and at the same time grab the lock if we
  21. // got here first. We need acquire ordering here because if we see
  22. // State::DONE, everything we do after that should "happen after" everything
  23. // the other thread has done before writing the State::DONE.
  24. State state2 = State::INITIAL;
  25. bool have_exchanged = state.compare_exchange_strong(
  26. state2, State::PERFORMING_NO_WAITERS, AK::memory_order_acquire);
  27. if (have_exchanged) {
  28. // We observed State::INITIAL and we've changed it to
  29. // State::PERFORMING_NO_WAITERS, so it's us who should perform the
  30. // operation.
  31. callback();
  32. // Now, record that we're done.
  33. state2 = state.exchange(State::DONE, AK::memory_order_release);
  34. switch (state2) {
  35. case State::INITIAL:
  36. case State::DONE:
  37. VERIFY_NOT_REACHED();
  38. case State::PERFORMING_NO_WAITERS:
  39. // The fast path: there's no contention, so we don't have to wake
  40. // anyone.
  41. break;
  42. case State::PERFORMING_WITH_WAITERS:
  43. futex_wake(self, INT_MAX);
  44. break;
  45. }
  46. return 0;
  47. }
  48. // We did not get there first. Let's see if we have to wait.
  49. // state2 contains the observed state.
  50. while (true) {
  51. switch (state2) {
  52. case State::INITIAL:
  53. VERIFY_NOT_REACHED();
  54. case State::DONE:
  55. // Awesome, nothing to do then.
  56. return 0;
  57. case State::PERFORMING_NO_WAITERS:
  58. // We're going to wait for it, but we have to record that we're
  59. // waiting and the other thread should wake us up. We need acquire
  60. // ordering here for the same reason as above.
  61. have_exchanged = state.compare_exchange_strong(
  62. state2, State::PERFORMING_WITH_WAITERS, AK::memory_order_acquire);
  63. if (!have_exchanged) {
  64. // Something has changed already, reevaluate without waiting.
  65. continue;
  66. }
  67. state2 = State::PERFORMING_WITH_WAITERS;
  68. [[fallthrough]];
  69. case State::PERFORMING_WITH_WAITERS:
  70. // Let's wait for it.
  71. futex_wait(self, state2, nullptr, 0);
  72. // We have been woken up, but that might have been due to a signal
  73. // or something, so we have to reevaluate. We need acquire ordering
  74. // here for the same reason as above. Hopefully we'll just see
  75. // State::DONE this time, but who knows.
  76. state2 = state.load(AK::memory_order_acquire);
  77. continue;
  78. }
  79. }
  80. }