pthread_tls.cpp 3.0 KB

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