pthread_integration.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <AK/NeverDestroyed.h>
  8. #include <AK/Vector.h>
  9. #include <bits/pthread_integration.h>
  10. #include <errno.h>
  11. #include <sched.h>
  12. #include <unistd.h>
  13. namespace {
  14. // Most programs don't need this, no need to incur an extra mutex lock/unlock on them
  15. static Atomic<bool> g_did_touch_atfork { false };
  16. static pthread_mutex_t g_atfork_list_mutex __PTHREAD_MUTEX_INITIALIZER;
  17. static NeverDestroyed<Vector<void (*)(void), 4>> g_atfork_prepare_list;
  18. static NeverDestroyed<Vector<void (*)(void), 4>> g_atfork_child_list;
  19. static NeverDestroyed<Vector<void (*)(void), 4>> g_atfork_parent_list;
  20. }
  21. extern "C" {
  22. void __pthread_fork_prepare(void)
  23. {
  24. if (!g_did_touch_atfork.load())
  25. return;
  26. __pthread_mutex_lock(&g_atfork_list_mutex);
  27. for (auto entry : g_atfork_prepare_list.get())
  28. entry();
  29. __pthread_mutex_unlock(&g_atfork_list_mutex);
  30. }
  31. void __pthread_fork_child(void)
  32. {
  33. if (!g_did_touch_atfork.load())
  34. return;
  35. __pthread_mutex_lock(&g_atfork_list_mutex);
  36. for (auto entry : g_atfork_child_list.get())
  37. entry();
  38. __pthread_mutex_unlock(&g_atfork_list_mutex);
  39. }
  40. void __pthread_fork_parent(void)
  41. {
  42. if (!g_did_touch_atfork.load())
  43. return;
  44. __pthread_mutex_lock(&g_atfork_list_mutex);
  45. for (auto entry : g_atfork_parent_list.get())
  46. entry();
  47. __pthread_mutex_unlock(&g_atfork_list_mutex);
  48. }
  49. void __pthread_fork_atfork_register_prepare(void (*func)(void))
  50. {
  51. g_did_touch_atfork.store(true);
  52. __pthread_mutex_lock(&g_atfork_list_mutex);
  53. g_atfork_prepare_list->append(func);
  54. __pthread_mutex_unlock(&g_atfork_list_mutex);
  55. }
  56. void __pthread_fork_atfork_register_parent(void (*func)(void))
  57. {
  58. g_did_touch_atfork.store(true);
  59. __pthread_mutex_lock(&g_atfork_list_mutex);
  60. g_atfork_parent_list->append(func);
  61. __pthread_mutex_unlock(&g_atfork_list_mutex);
  62. }
  63. void __pthread_fork_atfork_register_child(void (*func)(void))
  64. {
  65. g_did_touch_atfork.store(true);
  66. __pthread_mutex_lock(&g_atfork_list_mutex);
  67. g_atfork_child_list->append(func);
  68. __pthread_mutex_unlock(&g_atfork_list_mutex);
  69. }
  70. int __pthread_self()
  71. {
  72. return gettid();
  73. }
  74. int pthread_self() __attribute__((weak, alias("__pthread_self")));
  75. int __pthread_mutex_lock(pthread_mutex_t* mutex)
  76. {
  77. pthread_t this_thread = __pthread_self();
  78. for (;;) {
  79. u32 expected = 0;
  80. if (!AK::atomic_compare_exchange_strong(&mutex->lock, expected, 1u, AK::memory_order_acquire)) {
  81. if (mutex->type == __PTHREAD_MUTEX_RECURSIVE && mutex->owner == this_thread) {
  82. mutex->level++;
  83. return 0;
  84. }
  85. sched_yield();
  86. continue;
  87. }
  88. mutex->owner = this_thread;
  89. mutex->level = 0;
  90. return 0;
  91. }
  92. }
  93. int pthread_mutex_lock(pthread_mutex_t*) __attribute__((weak, alias("__pthread_mutex_lock")));
  94. int __pthread_mutex_unlock(pthread_mutex_t* mutex)
  95. {
  96. if (mutex->type == __PTHREAD_MUTEX_RECURSIVE && mutex->level > 0) {
  97. mutex->level--;
  98. return 0;
  99. }
  100. mutex->owner = 0;
  101. AK::atomic_store(&mutex->lock, 0u, AK::memory_order_release);
  102. return 0;
  103. }
  104. int pthread_mutex_unlock(pthread_mutex_t*) __attribute__((weak, alias("__pthread_mutex_unlock")));
  105. int __pthread_mutex_trylock(pthread_mutex_t* mutex)
  106. {
  107. u32 expected = 0;
  108. if (!AK::atomic_compare_exchange_strong(&mutex->lock, expected, 1u, AK::memory_order_acquire)) {
  109. if (mutex->type == __PTHREAD_MUTEX_RECURSIVE && mutex->owner == pthread_self()) {
  110. mutex->level++;
  111. return 0;
  112. }
  113. return EBUSY;
  114. }
  115. mutex->owner = pthread_self();
  116. mutex->level = 0;
  117. return 0;
  118. }
  119. int pthread_mutex_trylock(pthread_mutex_t* mutex) __attribute__((weak, alias("__pthread_mutex_trylock")));
  120. int __pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
  121. {
  122. mutex->lock = 0;
  123. mutex->owner = 0;
  124. mutex->level = 0;
  125. mutex->type = attributes ? attributes->type : __PTHREAD_MUTEX_NORMAL;
  126. return 0;
  127. }
  128. int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __attribute__((weak, alias("__pthread_mutex_init")));
  129. }