pthread_once.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/Atomic.h>
  28. #include <AK/Types.h>
  29. #include <pthread.h>
  30. #include <serenity.h>
  31. enum State : i32 {
  32. INITIAL = PTHREAD_ONCE_INIT,
  33. DONE,
  34. PERFORMING_NO_WAITERS,
  35. PERFORMING_WITH_WAITERS,
  36. };
  37. int pthread_once(pthread_once_t* self, void (*callback)(void))
  38. {
  39. auto& state = reinterpret_cast<Atomic<State>&>(*self);
  40. // See what the current state is, and at the same time grab the lock if we
  41. // got here first. We need acquire ordering here because if we see
  42. // State::DONE, everything we do after that should "happen after" everything
  43. // the other thread has done before writing the State::DONE.
  44. State state2 = State::INITIAL;
  45. bool have_exchanged = state.compare_exchange_strong(
  46. state2, State::PERFORMING_NO_WAITERS, AK::memory_order_acquire);
  47. if (have_exchanged) {
  48. // We observed State::INITIAL and we've changed it to
  49. // State::PERFORMING_NO_WAITERS, so it's us who should perform the
  50. // operation.
  51. callback();
  52. // Now, record that we're done.
  53. state2 = state.exchange(State::DONE, AK::memory_order_release);
  54. switch (state2) {
  55. case State::INITIAL:
  56. case State::DONE:
  57. ASSERT_NOT_REACHED();
  58. case State::PERFORMING_NO_WAITERS:
  59. // The fast path: there's no contention, so we don't have to wake
  60. // anyone.
  61. break;
  62. case State::PERFORMING_WITH_WAITERS:
  63. futex(self, FUTEX_WAKE, INT_MAX, nullptr);
  64. break;
  65. }
  66. return 0;
  67. }
  68. // We did not get there first. Let's see if we have to wait.
  69. // state2 contains the observed state.
  70. while (true) {
  71. switch (state2) {
  72. case State::INITIAL:
  73. ASSERT_NOT_REACHED();
  74. case State::DONE:
  75. // Awesome, nothing to do then.
  76. return 0;
  77. case State::PERFORMING_NO_WAITERS:
  78. // We're going to wait for it, but we have to record that we're
  79. // waiting and the other thread should wake us up. We need acquire
  80. // ordering here for the same reason as above.
  81. have_exchanged = state.compare_exchange_strong(
  82. state2, State::PERFORMING_WITH_WAITERS, AK::memory_order_acquire);
  83. if (!have_exchanged) {
  84. // Something has changed already, reevaluate without waiting.
  85. continue;
  86. }
  87. state2 = State::PERFORMING_WITH_WAITERS;
  88. [[fallthrough]];
  89. case State::PERFORMING_WITH_WAITERS:
  90. // Let's wait for it.
  91. futex(self, FUTEX_WAIT, state2, nullptr);
  92. // We have been woken up, but that might have been due to a signal
  93. // or something, so we have to reevaluate. We need acquire ordering
  94. // here for the same reason as above. Hopefully we'll just see
  95. // State::DONE this time, but who knows.
  96. state2 = state.load(AK::memory_order_acquire);
  97. continue;
  98. }
  99. }
  100. }