pthread_local_storage.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <errno.h>
  7. #include <pthread.h>
  8. #include <string.h>
  9. #include "esp_err.h"
  10. #include "esp_log.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "sys/lock.h"
  14. #include "sys/queue.h"
  15. #include "pthread_internal.h"
  16. #define PTHREAD_TLS_INDEX 0
  17. typedef void (*pthread_destructor_t)(void*);
  18. /* This is a very naive implementation of key-indexed thread local storage, using two linked lists
  19. (one is a global list of registered keys, one per thread for thread local storage values).
  20. It won't work well if lots of keys & thread-local values are stored (O(n) lookup for both),
  21. but it should work for small amounts of data.
  22. */
  23. typedef struct key_entry_t_ {
  24. pthread_key_t key;
  25. pthread_destructor_t destructor;
  26. SLIST_ENTRY(key_entry_t_) next;
  27. } key_entry_t;
  28. // List of all keys created with pthread_key_create()
  29. SLIST_HEAD(key_list_t, key_entry_t_) s_keys = SLIST_HEAD_INITIALIZER(s_keys);
  30. static portMUX_TYPE s_keys_lock = portMUX_INITIALIZER_UNLOCKED;
  31. // List of all value entries associated with a thread via pthread_setspecific()
  32. typedef struct value_entry_t_ {
  33. pthread_key_t key;
  34. void *value;
  35. SLIST_ENTRY(value_entry_t_) next;
  36. } value_entry_t;
  37. // Type for the head of the list, as saved as a FreeRTOS thread local storage pointer
  38. SLIST_HEAD(values_list_t_, value_entry_t_);
  39. typedef struct values_list_t_ values_list_t;
  40. int pthread_key_create(pthread_key_t *key, pthread_destructor_t destructor)
  41. {
  42. key_entry_t *new_key = malloc(sizeof(key_entry_t));
  43. if (new_key == NULL) {
  44. return ENOMEM;
  45. }
  46. portENTER_CRITICAL(&s_keys_lock);
  47. const key_entry_t *head = SLIST_FIRST(&s_keys);
  48. new_key->key = (head == NULL) ? 1 : (head->key + 1);
  49. new_key->destructor = destructor;
  50. *key = new_key->key;
  51. SLIST_INSERT_HEAD(&s_keys, new_key, next);
  52. portEXIT_CRITICAL(&s_keys_lock);
  53. return 0;
  54. }
  55. static key_entry_t *find_key(pthread_key_t key)
  56. {
  57. portENTER_CRITICAL(&s_keys_lock);
  58. key_entry_t *result = NULL;;
  59. SLIST_FOREACH(result, &s_keys, next) {
  60. if(result->key == key) {
  61. break;
  62. }
  63. }
  64. portEXIT_CRITICAL(&s_keys_lock);
  65. return result;
  66. }
  67. int pthread_key_delete(pthread_key_t key)
  68. {
  69. portENTER_CRITICAL(&s_keys_lock);
  70. /* Ideally, we would also walk all tasks' thread local storage value_list here
  71. and delete any values associated with this key. We do not do this...
  72. */
  73. key_entry_t *entry = find_key(key);
  74. if (entry != NULL) {
  75. SLIST_REMOVE(&s_keys, entry, key_entry_t_, next);
  76. free(entry);
  77. }
  78. portEXIT_CRITICAL(&s_keys_lock);
  79. return 0;
  80. }
  81. /* Clean up callback for deleted tasks.
  82. This is called from one of two places:
  83. If the thread was created via pthread_create() then it's called by pthread_task_func() when that thread ends,
  84. or calls pthread_exit(), and the FreeRTOS thread-local-storage is removed before the FreeRTOS task is deleted.
  85. For other tasks, this is called when the FreeRTOS idle task performs its task cleanup after the task is deleted.
  86. There are two reasons for calling it early for pthreads:
  87. - To keep the timing consistent with "normal" pthreads, so after pthread_join() the task's destructors have all
  88. been called even if the idle task hasn't run cleanup yet.
  89. - The destructor is always called in the context of the thread itself - which is important if the task then calls
  90. pthread_getspecific() or pthread_setspecific() to update the state further, as allowed for in the spec.
  91. */
  92. static void pthread_local_storage_thread_deleted_callback(int index, void *v_tls)
  93. {
  94. values_list_t *tls = (values_list_t *)v_tls;
  95. assert(tls != NULL);
  96. /* Walk the list, freeing all entries and calling destructors if they are registered */
  97. while (1) {
  98. value_entry_t *entry = SLIST_FIRST(tls);
  99. if (entry == NULL) {
  100. break;
  101. }
  102. SLIST_REMOVE_HEAD(tls, next);
  103. // This is a little slow, walking the linked list of keys once per value,
  104. // but assumes that the thread's value list will have less entries
  105. // than the keys list
  106. key_entry_t *key = find_key(entry->key);
  107. if (key != NULL && key->destructor != NULL) {
  108. key->destructor(entry->value);
  109. }
  110. free(entry);
  111. }
  112. free(tls);
  113. }
  114. #if CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP && !CONFIG_FREERTOS_SMP // IDF-4955
  115. /* Called from FreeRTOS task delete hook */
  116. void pthread_local_storage_cleanup(TaskHandle_t task)
  117. {
  118. void *tls = pvTaskGetThreadLocalStoragePointer(task, PTHREAD_TLS_INDEX);
  119. if (tls != NULL) {
  120. pthread_local_storage_thread_deleted_callback(PTHREAD_TLS_INDEX, tls);
  121. vTaskSetThreadLocalStoragePointer(task, PTHREAD_TLS_INDEX, NULL);
  122. }
  123. }
  124. void __real_vPortCleanUpTCB(void *tcb);
  125. /* If static task cleanup hook is defined then its applications responsibility to define `vPortCleanUpTCB`.
  126. Here we are wrapping it, so that we can do pthread specific TLS cleanup and then invoke application
  127. real specific `vPortCleanUpTCB` */
  128. void __wrap_vPortCleanUpTCB(void *tcb)
  129. {
  130. pthread_local_storage_cleanup(tcb);
  131. __real_vPortCleanUpTCB(tcb);
  132. }
  133. #endif // CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP && !CONFIG_FREERTOS_SMP
  134. /* this function called from pthread_task_func for "early" cleanup of TLS in a pthread */
  135. void pthread_internal_local_storage_destructor_callback(void)
  136. {
  137. void *tls = pvTaskGetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX);
  138. if (tls != NULL) {
  139. pthread_local_storage_thread_deleted_callback(PTHREAD_TLS_INDEX, tls);
  140. /* remove the thread-local-storage pointer to avoid the idle task cleanup
  141. calling it again...
  142. */
  143. #if CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP && !CONFIG_FREERTOS_SMP // IDF-4955
  144. vTaskSetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX, NULL);
  145. #else
  146. vTaskSetThreadLocalStoragePointerAndDelCallback(NULL,
  147. PTHREAD_TLS_INDEX,
  148. NULL,
  149. NULL);
  150. #endif // CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP && !CONFIG_FREERTOS_SMP
  151. }
  152. }
  153. static value_entry_t *find_value(const values_list_t *list, pthread_key_t key)
  154. {
  155. value_entry_t *result = NULL;;
  156. SLIST_FOREACH(result, list, next) {
  157. if(result->key == key) {
  158. break;
  159. }
  160. }
  161. return result;
  162. }
  163. void *pthread_getspecific(pthread_key_t key)
  164. {
  165. values_list_t *tls = (values_list_t *) pvTaskGetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX);
  166. if (tls == NULL) {
  167. return NULL;
  168. }
  169. value_entry_t *entry = find_value(tls, key);
  170. if(entry != NULL) {
  171. return entry->value;
  172. }
  173. return NULL;
  174. }
  175. int pthread_setspecific(pthread_key_t key, const void *value)
  176. {
  177. key_entry_t *key_entry = find_key(key);
  178. if (key_entry == NULL) {
  179. return ENOENT; // this situation is undefined by pthreads standard
  180. }
  181. values_list_t *tls = pvTaskGetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX);
  182. if (tls == NULL) {
  183. tls = calloc(1, sizeof(values_list_t));
  184. if (tls == NULL) {
  185. return ENOMEM;
  186. }
  187. #if defined(CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP)
  188. vTaskSetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX, tls);
  189. #else
  190. vTaskSetThreadLocalStoragePointerAndDelCallback(NULL,
  191. PTHREAD_TLS_INDEX,
  192. tls,
  193. pthread_local_storage_thread_deleted_callback);
  194. #endif
  195. }
  196. value_entry_t *entry = find_value(tls, key);
  197. if (entry != NULL) {
  198. if (value != NULL) {
  199. // cast on next line is necessary as pthreads API uses
  200. // 'const void *' here but elsewhere uses 'void *'
  201. entry->value = (void *) value;
  202. } else { // value == NULL, remove the entry
  203. SLIST_REMOVE(tls, entry, value_entry_t_, next);
  204. free(entry);
  205. }
  206. } else if (value != NULL) {
  207. entry = malloc(sizeof(value_entry_t));
  208. if (entry == NULL) {
  209. return ENOMEM;
  210. }
  211. entry->key = key;
  212. entry->value = (void *) value; // see note above about cast
  213. // insert the new entry at the end of the list. this is important because
  214. // a destructor may call pthread_setspecific() to add a new non-NULL value
  215. // to the list, and this should be processed after all other entries.
  216. //
  217. // See pthread_local_storage_thread_deleted_callback()
  218. value_entry_t *last_entry = NULL;
  219. value_entry_t *it;
  220. SLIST_FOREACH(it, tls, next) {
  221. last_entry = it;
  222. }
  223. if (last_entry == NULL) {
  224. SLIST_INSERT_HEAD(tls, entry, next);
  225. } else {
  226. SLIST_INSERT_AFTER(last_entry, entry, next);
  227. }
  228. }
  229. return 0;
  230. }
  231. /* Hook function to force linking this file */
  232. void pthread_include_pthread_local_storage_impl(void)
  233. {
  234. }