semaphore.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  3. * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/Atomic.h>
  9. #include <AK/Types.h>
  10. #include <errno.h>
  11. #include <semaphore.h>
  12. #include <serenity.h>
  13. // Whether sem_wait() or sem_post() is responsible for waking any sleeping
  14. // threads.
  15. static constexpr u32 POST_WAKES = 1 << 31;
  16. sem_t* sem_open(const char*, int, ...)
  17. {
  18. errno = ENOSYS;
  19. return nullptr;
  20. }
  21. int sem_close(sem_t*)
  22. {
  23. errno = ENOSYS;
  24. return -1;
  25. }
  26. int sem_unlink(const char*)
  27. {
  28. errno = ENOSYS;
  29. return -1;
  30. }
  31. int sem_init(sem_t* sem, int shared, unsigned int value)
  32. {
  33. if (shared) {
  34. errno = ENOSYS;
  35. return -1;
  36. }
  37. if (value > SEM_VALUE_MAX) {
  38. errno = EINVAL;
  39. return -1;
  40. }
  41. sem->value = value;
  42. return 0;
  43. }
  44. int sem_destroy(sem_t*)
  45. {
  46. return 0;
  47. }
  48. int sem_getvalue(sem_t* sem, int* sval)
  49. {
  50. u32 value = AK::atomic_load(&sem->value, AK::memory_order_relaxed);
  51. *sval = value & ~POST_WAKES;
  52. return 0;
  53. }
  54. int sem_post(sem_t* sem)
  55. {
  56. u32 value = AK::atomic_fetch_add(&sem->value, 1u, AK::memory_order_release);
  57. // Fast path: no need to wake.
  58. if (!(value & POST_WAKES)) [[likely]]
  59. return 0;
  60. // Pass the responsibility for waking more threads if more slots become
  61. // available later to sem_wait() in the thread we're about to wake, as
  62. // opposed to further sem_post() calls that free up those slots.
  63. value = AK::atomic_fetch_and(&sem->value, ~POST_WAKES, AK::memory_order_relaxed);
  64. // Check if another sem_post() call has handled it already.
  65. if (!(value & POST_WAKES)) [[likely]]
  66. return 0;
  67. int rc = futex_wake(&sem->value, 1);
  68. VERIFY(rc >= 0);
  69. return 0;
  70. }
  71. int sem_trywait(sem_t* sem)
  72. {
  73. u32 value = AK::atomic_load(&sem->value, AK::memory_order_relaxed);
  74. u32 count = value & ~POST_WAKES;
  75. if (count == 0)
  76. return EAGAIN;
  77. // Decrement the count without touching the flag.
  78. u32 desired = (count - 1) | (value & POST_WAKES);
  79. bool exchanged = AK::atomic_compare_exchange_strong(&sem->value, value, desired, AK::memory_order_acquire);
  80. if (exchanged) [[likely]]
  81. return 0;
  82. else
  83. return EAGAIN;
  84. }
  85. int sem_wait(sem_t* sem)
  86. {
  87. return sem_timedwait(sem, nullptr);
  88. }
  89. int sem_timedwait(sem_t* sem, const struct timespec* abstime)
  90. {
  91. u32 value = AK::atomic_load(&sem->value, AK::memory_order_relaxed);
  92. bool responsible_for_waking = false;
  93. while (true) {
  94. u32 count = value & ~POST_WAKES;
  95. if (count > 0) [[likely]] {
  96. // It looks like there are some free slots.
  97. u32 whether_post_wakes = value & POST_WAKES;
  98. bool going_to_wake = false;
  99. if (responsible_for_waking && !whether_post_wakes) {
  100. // If we have ourselves been woken up previously, and the
  101. // POST_WAKES flag is not set, that means some more slots might
  102. // be available now, and it's us who has to wake up additional
  103. // threads.
  104. if (count > 1) [[unlikely]]
  105. going_to_wake = true;
  106. // Pass the responsibility for waking up further threads back to
  107. // sem_post() calls. In particular, we don't want the threads
  108. // we're about to wake to try to wake anyone else.
  109. whether_post_wakes = POST_WAKES;
  110. }
  111. // Now, try to commit this.
  112. u32 desired = (count - 1) | whether_post_wakes;
  113. bool exchanged = AK::atomic_compare_exchange_strong(&sem->value, value, desired, AK::memory_order_acquire);
  114. if (!exchanged) [[unlikely]]
  115. // Re-evaluate.
  116. continue;
  117. if (going_to_wake) [[unlikely]] {
  118. int rc = futex_wake(&sem->value, count - 1);
  119. VERIFY(rc >= 0);
  120. }
  121. return 0;
  122. }
  123. // We're probably going to sleep, so attempt to set the flag. We do not
  124. // commit to sleeping yet, though, as setting the flag may fail and
  125. // cause us to reevaluate what we're doing.
  126. if (value == 0) {
  127. bool exchanged = AK::atomic_compare_exchange_strong(&sem->value, value, POST_WAKES, AK::memory_order_relaxed);
  128. if (!exchanged) [[unlikely]]
  129. // Re-evaluate.
  130. continue;
  131. value = POST_WAKES;
  132. }
  133. // At this point, we're committed to sleeping.
  134. responsible_for_waking = true;
  135. futex_wait(&sem->value, value, abstime, CLOCK_REALTIME);
  136. // This is the state we will probably see upon being waked:
  137. value = 1;
  138. }
  139. }