pthread_cond.cpp 5.3 KB

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