pthread_cond.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2019, Andreas Kling <kling@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 <bits/pthread_cancel.h>
  11. #include <errno.h>
  12. #include <pthread.h>
  13. #include <serenity.h>
  14. #include <sys/types.h>
  15. #include <time.h>
  16. // Condition variable attributes.
  17. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_init.html
  18. int pthread_condattr_init(pthread_condattr_t* attr)
  19. {
  20. attr->clockid = CLOCK_MONOTONIC_COARSE;
  21. return 0;
  22. }
  23. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_destroy.html
  24. int pthread_condattr_destroy(pthread_condattr_t*)
  25. {
  26. return 0;
  27. }
  28. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_getclock.html
  29. int pthread_condattr_getclock(pthread_condattr_t* attr, clockid_t* clock)
  30. {
  31. *clock = attr->clockid;
  32. return 0;
  33. }
  34. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_setclock.html
  35. int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock)
  36. {
  37. switch (clock) {
  38. case CLOCK_REALTIME:
  39. case CLOCK_REALTIME_COARSE:
  40. case CLOCK_MONOTONIC:
  41. case CLOCK_MONOTONIC_COARSE:
  42. case CLOCK_MONOTONIC_RAW:
  43. attr->clockid = clock;
  44. return 0;
  45. default:
  46. return EINVAL;
  47. }
  48. }
  49. // Condition variables.
  50. // cond->value is the generation number (number of times the variable has been
  51. // signaled) multiplied by INCREMENT, or'ed with the NEED_TO_WAKE flags. It's
  52. // done this way instead of putting the flags into the high bits because the
  53. // sequence number can easily overflow, which is completely fine but should not
  54. // cause it to corrupt the flags.
  55. static constexpr u32 NEED_TO_WAKE_ONE = 1;
  56. static constexpr u32 NEED_TO_WAKE_ALL = 2;
  57. static constexpr u32 INCREMENT = 4;
  58. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_init.html
  59. int pthread_cond_init(pthread_cond_t* cond, pthread_condattr_t const* attr)
  60. {
  61. cond->mutex = nullptr;
  62. cond->value = 0;
  63. cond->clockid = attr ? attr->clockid : CLOCK_REALTIME_COARSE;
  64. return 0;
  65. }
  66. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_destroy.html
  67. int pthread_cond_destroy(pthread_cond_t*)
  68. {
  69. return 0;
  70. }
  71. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html
  72. int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
  73. {
  74. return pthread_cond_timedwait(cond, mutex, nullptr);
  75. }
  76. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html
  77. int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  78. {
  79. __pthread_maybe_cancel();
  80. // Save the mutex this condition variable is associated with. We don't (yet)
  81. // support changing this mutex once set.
  82. pthread_mutex_t* old_mutex = AK::atomic_exchange(&cond->mutex, mutex, AK::memory_order_relaxed);
  83. if (old_mutex && old_mutex != mutex)
  84. TODO();
  85. // Fetch the current value, and record that we're about to wait. Fetching
  86. // the current value has to be done while we hold the mutex, because the
  87. // value might change as soon as we unlock it.
  88. u32 value = AK::atomic_fetch_or(&cond->value, NEED_TO_WAKE_ONE | NEED_TO_WAKE_ALL, AK::memory_order_release) | NEED_TO_WAKE_ONE | NEED_TO_WAKE_ALL;
  89. pthread_mutex_unlock(mutex);
  90. int rc = futex_wait(&cond->value, value, abstime, cond->clockid, false);
  91. if (rc < 0 && errno != EAGAIN)
  92. return errno;
  93. // We might have been re-queued onto the mutex while we were sleeping. Take
  94. // the pessimistic locking path.
  95. __pthread_mutex_lock_pessimistic_np(mutex);
  96. return 0;
  97. }
  98. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html
  99. int pthread_cond_signal(pthread_cond_t* cond)
  100. {
  101. // Increment the generation.
  102. u32 value = AK::atomic_fetch_add(&cond->value, INCREMENT, AK::memory_order_relaxed);
  103. // Fast path: nobody's waiting (or at least, nobody has to be woken).
  104. if (!(value & NEED_TO_WAKE_ONE)) [[likely]]
  105. return 0;
  106. // Wake someone, and clear the NEED_TO_WAKE_ONE flag if there was nobody for
  107. // us to wake, to take the fast path the next time. Since we only learn
  108. // whether there has been somebody waiting or not after we have tried to
  109. // wake them, it would make sense for us to clear the flag after trying to
  110. // wake someone up and seeing there was nobody waiting; but that would race
  111. // with somebody else setting the flag. Therefore, we do it like this:
  112. // attempt to clear the flag first...
  113. value = AK::atomic_fetch_and(&cond->value, ~NEED_TO_WAKE_ONE, AK::memory_order_relaxed);
  114. // ...check if it was already cleared by someone else...
  115. if (!(value & NEED_TO_WAKE_ONE)) [[likely]]
  116. return 0;
  117. // ...try to wake someone...
  118. int rc = futex_wake(&cond->value, 1, false);
  119. VERIFY(rc >= 0);
  120. // ...and if we have woken someone, put the flag back.
  121. if (rc > 0)
  122. AK::atomic_fetch_or(&cond->value, NEED_TO_WAKE_ONE, AK::memory_order_relaxed);
  123. return 0;
  124. }
  125. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html
  126. int pthread_cond_broadcast(pthread_cond_t* cond)
  127. {
  128. // Increment the generation.
  129. u32 value = AK::atomic_fetch_add(&cond->value, INCREMENT, AK::memory_order_relaxed);
  130. // Fast path: nobody's waiting (or at least, nobody has to be woken).
  131. if (!(value & NEED_TO_WAKE_ALL)) [[likely]]
  132. return 0;
  133. AK::atomic_fetch_and(&cond->value, ~(NEED_TO_WAKE_ONE | NEED_TO_WAKE_ALL), AK::memory_order_acquire);
  134. pthread_mutex_t* mutex = AK::atomic_load(&cond->mutex, AK::memory_order_relaxed);
  135. VERIFY(mutex);
  136. int rc = futex(&cond->value, FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG, 1, nullptr, &mutex->lock, INT_MAX);
  137. VERIFY(rc >= 0);
  138. return 0;
  139. }