pthread_tls.cpp 3.0 KB

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