pthread_cond.cpp 5.7 KB

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