pthread_tls.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <LibPthread/pthread.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #ifndef _DYNAMIC_LOADER
  11. extern "C" {
  12. static constexpr int max_keys = PTHREAD_KEYS_MAX;
  13. struct KeyTable {
  14. KeyDestructor destructors[max_keys] { nullptr };
  15. int next { 0 };
  16. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  17. };
  18. struct SpecificTable {
  19. void* values[max_keys] { nullptr };
  20. };
  21. static KeyTable s_keys;
  22. # ifndef X86_64_NO_TLS
  23. __thread
  24. # endif
  25. SpecificTable t_specifics;
  26. int __pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
  27. {
  28. int ret = 0;
  29. __pthread_mutex_lock(&s_keys.mutex);
  30. if (s_keys.next >= max_keys) {
  31. ret = EAGAIN;
  32. } else {
  33. *key = s_keys.next++;
  34. s_keys.destructors[*key] = destructor;
  35. ret = 0;
  36. }
  37. __pthread_mutex_unlock(&s_keys.mutex);
  38. return ret;
  39. }
  40. int pthread_key_create(pthread_key_t*, KeyDestructor) __attribute__((weak, alias("__pthread_key_create")));
  41. int __pthread_key_delete(pthread_key_t key)
  42. {
  43. if (key < 0 || key >= max_keys)
  44. return EINVAL;
  45. __pthread_mutex_lock(&s_keys.mutex);
  46. s_keys.destructors[key] = nullptr;
  47. __pthread_mutex_unlock(&s_keys.mutex);
  48. return 0;
  49. }
  50. int pthread_key_delete(pthread_key_t) __attribute__((weak, alias("__pthread_key_delete")));
  51. void* __pthread_getspecific(pthread_key_t key)
  52. {
  53. if (key < 0)
  54. return nullptr;
  55. if (key >= max_keys)
  56. return nullptr;
  57. return t_specifics.values[key];
  58. }
  59. void* pthread_getspecific(pthread_key_t) __attribute__((weak, alias("__pthread_getspecific")));
  60. int __pthread_setspecific(pthread_key_t key, const void* value)
  61. {
  62. if (key < 0)
  63. return EINVAL;
  64. if (key >= max_keys)
  65. return EINVAL;
  66. t_specifics.values[key] = const_cast<void*>(value);
  67. return 0;
  68. }
  69. int pthread_setspecific(pthread_key_t, const void*) __attribute__((weak, alias("__pthread_setspecific")));
  70. void __pthread_key_destroy_for_current_thread()
  71. {
  72. // This function will either be called during exit_thread, for a pthread, or
  73. // during global program shutdown for the main thread.
  74. __pthread_mutex_lock(&s_keys.mutex);
  75. size_t num_used_keys = s_keys.next;
  76. // Dr. POSIX accounts for weird key destructors setting their own key again.
  77. // Or even, setting other unrelated keys? Odd, but whatever the Doc says goes.
  78. for (size_t destruct_iteration = 0; destruct_iteration < PTHREAD_DESTRUCTOR_ITERATIONS; ++destruct_iteration) {
  79. bool any_nonnull_destructors = false;
  80. for (size_t key_index = 0; key_index < num_used_keys; ++key_index) {
  81. void* value = exchange(t_specifics.values[key_index], nullptr);
  82. if (value && s_keys.destructors[key_index]) {
  83. any_nonnull_destructors = true;
  84. (*s_keys.destructors[key_index])(value);
  85. }
  86. }
  87. if (!any_nonnull_destructors)
  88. break;
  89. }
  90. __pthread_mutex_unlock(&s_keys.mutex);
  91. }
  92. }
  93. #endif