pthread_tls.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-10-26 Bernard the first version
  9. */
  10. #include <pthread.h>
  11. #include "pthread_internal.h"
  12. _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
  13. /* initialize key area */
  14. static int pthread_key_system_init(void)
  15. {
  16. rt_memset(&_thread_keys[0], 0, sizeof(_thread_keys));
  17. return 0;
  18. }
  19. INIT_COMPONENT_EXPORT(pthread_key_system_init);
  20. /**
  21. * @brief Retrieves the value associated with a thread-specific data key for the calling thread.
  22. *
  23. * This function returns the value that was previously set for the specified key
  24. * in the calling thread using `pthread_setspecific`. Each thread has its own independent
  25. * value for the same key.
  26. *
  27. * @param[in] key
  28. * The thread-specific data key, created using `pthread_key_create`.
  29. *
  30. * @return
  31. * - The value associated with the key for the calling thread, or `NULL` if no value
  32. * has been set for the key in this thread.
  33. *
  34. * @note
  35. * - If no value has been set for the key in the calling thread, `pthread_getspecific`
  36. * returns `NULL`. This does not indicate an error unless `NULL` was explicitly set as the value.
  37. * - The value retrieved is specific to the calling thread and may differ for other threads.
  38. *
  39. * @attention
  40. * - If the key has been deleted using `pthread_key_delete`, the behavior of this
  41. * function is undefined.
  42. *
  43. * @see pthread_key_create(), pthread_setspecific(), pthread_key_delete()
  44. */
  45. void *pthread_getspecific(pthread_key_t key)
  46. {
  47. struct _pthread_data* ptd;
  48. if (rt_thread_self() == NULL) return NULL;
  49. /* get pthread data from user data of thread */
  50. ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
  51. RT_ASSERT(ptd != NULL);
  52. if (ptd->tls == NULL)
  53. return NULL;
  54. if ((key < PTHREAD_KEY_MAX) && (_thread_keys[key].is_used))
  55. return ptd->tls[key];
  56. return NULL;
  57. }
  58. RTM_EXPORT(pthread_getspecific);
  59. /**
  60. * @brief Associates a value with a thread-specific data key for the calling thread.
  61. *
  62. * This function sets a thread-specific value for the given key. Each thread has its
  63. * own independent value associated with the same key, and this value is not shared with
  64. * other threads.
  65. *
  66. * @param[in] key
  67. * The thread-specific data key, created using `pthread_key_create`.
  68. * @param[in] value
  69. * The value to associate with the key for the calling thread. The value can be
  70. * a pointer to any data or `NULL`.
  71. *
  72. * @return
  73. * - `0`: The value was successfully set.
  74. * - `EINVAL`: The specified key is invalid or not initialized.
  75. *
  76. * @note
  77. * - Setting a new value for a key in a thread overwrites any previously set value
  78. * for that key in the same thread.
  79. * - The value set using `pthread_setspecific` is accessible via `pthread_getspecific`
  80. * for the same thread.
  81. * - The association between the key and value is valid until the thread terminates,
  82. * the key is deleted using `pthread_key_delete`, or a new value is set with this function.
  83. *
  84. * @attention
  85. * - The value is specific to the calling thread; other threads will not be affected
  86. * and will continue to maintain their own independent values for the same key.
  87. *
  88. * @see pthread_key_create(), pthread_getspecific(), pthread_key_delete()
  89. */
  90. int pthread_setspecific(pthread_key_t key, const void *value)
  91. {
  92. struct _pthread_data* ptd;
  93. if (rt_thread_self() == NULL) return EINVAL;
  94. /* get pthread data from user data of thread */
  95. ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
  96. RT_ASSERT(ptd != NULL);
  97. /* check tls area */
  98. if (ptd->tls == NULL)
  99. {
  100. ptd->tls = (void**)rt_calloc(PTHREAD_KEY_MAX, sizeof(void*));
  101. }
  102. if ((key < PTHREAD_KEY_MAX) && _thread_keys[key].is_used)
  103. {
  104. ptd->tls[key] = (void *)value;
  105. return 0;
  106. }
  107. return EINVAL;
  108. }
  109. RTM_EXPORT(pthread_setspecific);
  110. /**
  111. * @brief Creates a thread-specific data key.
  112. *
  113. * This function allocates a unique key for thread-specific data (TSD).
  114. * Each thread can use this key to access its own specific data associated with it.
  115. *
  116. * @param[out] key
  117. * A pointer to a variable where the newly created key will be stored.
  118. * On success, `*key` will hold the newly allocated key.
  119. * @param[in] destructor
  120. * An optional destructor function pointer. This function will be called to
  121. * clean up thread-specific data associated with the key when a thread terminates.
  122. * Pass `NULL` if no cleanup is required.
  123. *
  124. * @return
  125. * - `0`: The key was successfully created.
  126. * - `EAGAIN`: The system has reached the maximum number of available keys, and no new key can be allocated.
  127. *
  128. * @note
  129. * - Each thread can use `pthread_setspecific` and `pthread_getspecific` to set and retrieve the thread-specific data associated with the key.
  130. * - The destructor function will be invoked automatically by the system when the thread terminates, if not explicitly called.
  131. *
  132. * @attention
  133. * - If the `destructor` function is invoked and it sets thread-specific data for the same key,
  134. * the destructor may be called multiple times, up to a limit defined by the system
  135. * (typically `PTHREAD_DESTRUCTOR_ITERATIONS`).
  136. *
  137. * @see pthread_setspecific(), pthread_getspecific(), pthread_key_delete()
  138. */
  139. int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
  140. {
  141. rt_uint32_t index;
  142. rt_enter_critical();
  143. for (index = 0; index < PTHREAD_KEY_MAX; index ++)
  144. {
  145. if (_thread_keys[index].is_used == 0)
  146. {
  147. _thread_keys[index].is_used = 1;
  148. _thread_keys[index].destructor = destructor;
  149. *key = index;
  150. rt_exit_critical();
  151. return 0;
  152. }
  153. }
  154. rt_exit_critical();
  155. return EAGAIN;
  156. }
  157. RTM_EXPORT(pthread_key_create);
  158. /**
  159. * @brief Deletes a thread-specific data key.
  160. *
  161. * This function deletes a previously created thread-specific data key.
  162. * The key will no longer be valid for use in `pthread_setspecific` or `pthread_getspecific`.
  163. *
  164. * @param[in] key
  165. * The thread-specific data key to delete. The key must have been created with
  166. * `pthread_key_create`.
  167. *
  168. * @return
  169. * - `0`: The key was successfully deleted.
  170. * - `EINVAL`: The specified key is invalid or has not been initialized.
  171. *
  172. * @note
  173. * - Deleting a key does not automatically free or clean up the thread-specific data
  174. * associated with the key. It is the programmer's responsibility to ensure that
  175. * associated resources are properly released, if necessary.
  176. * - After deletion, using the key in `pthread_setspecific` or `pthread_getspecific`
  177. * will result in undefined behavior.
  178. *
  179. * @see pthread_key_create(), pthread_setspecific(), pthread_getspecific()
  180. */
  181. int pthread_key_delete(pthread_key_t key)
  182. {
  183. if (key >= PTHREAD_KEY_MAX)
  184. return EINVAL;
  185. rt_enter_critical();
  186. _thread_keys[key].is_used = 0;
  187. _thread_keys[key].destructor = 0;
  188. rt_exit_critical();
  189. return 0;
  190. }
  191. RTM_EXPORT(pthread_key_delete);