pthread_tls.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <errno.h>
  8. #include <pthread.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. // https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
  24. int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
  25. {
  26. int ret = 0;
  27. pthread_mutex_lock(&s_keys.mutex);
  28. if (s_keys.next >= max_keys) {
  29. ret = EAGAIN;
  30. } else {
  31. *key = s_keys.next++;
  32. s_keys.destructors[*key] = destructor;
  33. ret = 0;
  34. }
  35. pthread_mutex_unlock(&s_keys.mutex);
  36. return ret;
  37. }
  38. // https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_delete.html
  39. int pthread_key_delete(pthread_key_t key)
  40. {
  41. if (key < 0 || key >= max_keys)
  42. return EINVAL;
  43. pthread_mutex_lock(&s_keys.mutex);
  44. s_keys.destructors[key] = nullptr;
  45. pthread_mutex_unlock(&s_keys.mutex);
  46. return 0;
  47. }
  48. // https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_getspecific.html
  49. void* pthread_getspecific(pthread_key_t key)
  50. {
  51. if (key < 0)
  52. return nullptr;
  53. if (key >= max_keys)
  54. return nullptr;
  55. return t_specifics.values[key];
  56. }
  57. // https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_setspecific.html
  58. int pthread_setspecific(pthread_key_t key, void const* value)
  59. {
  60. if (key < 0)
  61. return EINVAL;
  62. if (key >= max_keys)
  63. return EINVAL;
  64. t_specifics.values[key] = const_cast<void*>(value);
  65. return 0;
  66. }
  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