pthread_once.cpp 3.1 KB

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