pthread_cond.cpp 4.6 KB

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