test_pthread_local_storage.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Test pthread_create_key, pthread_delete_key, pthread_setspecific, pthread_getspecific
  2. #include <pthread.h>
  3. #include "unity.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "test_utils.h"
  7. #include "esp_system.h"
  8. TEST_CASE("pthread local storage basics", "[pthread]")
  9. {
  10. pthread_key_t key;
  11. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, NULL));
  12. TEST_ASSERT_NULL(pthread_getspecific(key));
  13. int val = 3;
  14. printf("Setting to %p...\n", &val);
  15. TEST_ASSERT_EQUAL(0, pthread_setspecific(key, &val));
  16. printf("Reading back...\n");
  17. TEST_ASSERT_EQUAL_PTR(&val, pthread_getspecific(key));
  18. printf("Setting to NULL...\n");
  19. TEST_ASSERT_EQUAL(0, pthread_setspecific(key, NULL));
  20. printf("Reading back...\n");
  21. TEST_ASSERT_NULL(pthread_getspecific(key));
  22. TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
  23. }
  24. TEST_CASE("pthread local storage unique keys", "[pthread]")
  25. {
  26. const int NUM_KEYS = 10;
  27. pthread_key_t keys[NUM_KEYS];
  28. for (int i = 0; i < NUM_KEYS; i++) {
  29. TEST_ASSERT_EQUAL(0, pthread_key_create(&keys[i], NULL));
  30. printf("New key %d = %d\n", i, keys[i]);
  31. }
  32. for (int i = 0; i < NUM_KEYS; i++) {
  33. for (int j = 0; j < NUM_KEYS; j++) {
  34. if (i != j) {
  35. TEST_ASSERT_NOT_EQUAL(keys[i], keys[j]);
  36. }
  37. }
  38. }
  39. for (int i = 0; i < NUM_KEYS; i++) {
  40. TEST_ASSERT_EQUAL(0, pthread_key_delete(keys[i]));
  41. }
  42. }
  43. static void test_pthread_destructor(void *);
  44. static void *expected_destructor_ptr;
  45. static void *actual_destructor_ptr;
  46. static void *thread_test_pthread_destructor(void *);
  47. TEST_CASE("pthread local storage destructor", "[pthread]")
  48. {
  49. pthread_t thread;
  50. pthread_key_t key = -1;
  51. expected_destructor_ptr = NULL;
  52. actual_destructor_ptr = NULL;
  53. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
  54. TEST_ASSERT_EQUAL(0, pthread_create(&thread, NULL, thread_test_pthread_destructor, (void *)key));
  55. TEST_ASSERT_EQUAL(0, pthread_join(thread, NULL));
  56. printf("Joined...\n");
  57. TEST_ASSERT_NOT_NULL(expected_destructor_ptr);
  58. TEST_ASSERT_NOT_NULL(actual_destructor_ptr);
  59. TEST_ASSERT_EQUAL_PTR(expected_destructor_ptr, actual_destructor_ptr);
  60. TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
  61. }
  62. static void task_test_pthread_destructor(void *v_key);
  63. TEST_CASE("pthread local storage destructor in FreeRTOS task", "[pthread]")
  64. {
  65. // Same as previous test case, but doesn't use pthread APIs therefore must wait
  66. // for the idle task to call the destructor
  67. pthread_key_t key = -1;
  68. expected_destructor_ptr = NULL;
  69. actual_destructor_ptr = NULL;
  70. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
  71. xTaskCreate(task_test_pthread_destructor,
  72. "ptdest", 8192, (void *)key, UNITY_FREERTOS_PRIORITY+1,
  73. NULL);
  74. // Above task has higher priority to us, so should run immediately
  75. // but we need to wait for the idle task cleanup to run
  76. vTaskDelay(20);
  77. TEST_ASSERT_NOT_NULL(expected_destructor_ptr);
  78. TEST_ASSERT_NOT_NULL(actual_destructor_ptr);
  79. TEST_ASSERT_EQUAL_PTR(expected_destructor_ptr, actual_destructor_ptr);
  80. TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
  81. }
  82. static void *thread_test_pthread_destructor(void *v_key)
  83. {
  84. printf("Local storage thread running...\n");
  85. pthread_key_t key = (pthread_key_t) v_key;
  86. expected_destructor_ptr = &key; // address of stack variable in the task...
  87. pthread_setspecific(key, expected_destructor_ptr);
  88. printf("Local storage thread done.\n");
  89. return NULL;
  90. }
  91. static void test_pthread_destructor(void *value)
  92. {
  93. actual_destructor_ptr = value;
  94. }
  95. static void task_test_pthread_destructor(void *v_key)
  96. {
  97. /* call the pthread main routine, then delete ourselves... */
  98. thread_test_pthread_destructor(v_key);
  99. vTaskDelete(NULL);
  100. }
  101. #define STRESS_NUMITER 2000000
  102. #define STRESS_NUMTASKS 16
  103. static void *thread_stress_test(void *v_key)
  104. {
  105. pthread_key_t key = (pthread_key_t) v_key;
  106. void *tls_value = (void *)esp_random();
  107. pthread_setspecific(key, tls_value);
  108. for(int i = 0; i < STRESS_NUMITER; i++) {
  109. TEST_ASSERT_EQUAL_HEX32(pthread_getspecific(key), tls_value);
  110. }
  111. return NULL;
  112. }
  113. // This test case added to reproduce issues with unpinned tasks and TLS
  114. TEST_CASE("pthread local storage stress test", "[pthread]")
  115. {
  116. pthread_key_t key = -1;
  117. pthread_t threads[STRESS_NUMTASKS] = { 0 };
  118. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
  119. for (int i = 0; i < STRESS_NUMTASKS; i++) {
  120. TEST_ASSERT_EQUAL(0, pthread_create(&threads[i], NULL, thread_stress_test, (void *)key));
  121. }
  122. for (int i = 0; i < STRESS_NUMTASKS; i++) {
  123. TEST_ASSERT_EQUAL(0, pthread_join(threads[i], NULL));
  124. }
  125. }
  126. #define NUM_KEYS 4 // number of keys used in repeat destructor test
  127. #define NUM_REPEATS 17 // number of times we re-set a key to a non-NULL value to re-trigger destructor
  128. typedef struct {
  129. pthread_key_t keys[NUM_KEYS]; // pthread local storage keys used in test
  130. unsigned count; // number of times the destructor has been called
  131. int last_idx; // index of last key where destructor was called
  132. } destr_test_state_t;
  133. static void s_test_repeat_destructor(void *vp_state);
  134. static void *s_test_repeat_destructor_thread(void *vp_state);
  135. // Test the correct behaviour of a pthread destructor function that uses
  136. // pthread_setspecific() to set another value when it runs, and also
  137. //
  138. // As described in https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
  139. TEST_CASE("pthread local storage 'repeat' destructor test", "[pthread]")
  140. {
  141. int r;
  142. destr_test_state_t state = { .last_idx = -1 };
  143. pthread_t thread;
  144. for (int i = 0; i < NUM_KEYS; i++) {
  145. r = pthread_key_create(&state.keys[i], s_test_repeat_destructor);
  146. TEST_ASSERT_EQUAL(0, r);
  147. }
  148. r = pthread_create(&thread, NULL, s_test_repeat_destructor_thread, &state);
  149. TEST_ASSERT_EQUAL(0, r);
  150. r = pthread_join(thread, NULL);
  151. TEST_ASSERT_EQUAL(0 ,r);
  152. // Cheating here to make sure compiler reads the value of 'count' from memory not from a register
  153. //
  154. // We expect the destructor was called NUM_REPEATS times when it repeated, then NUM_KEYS times when it didn't
  155. TEST_ASSERT_EQUAL(NUM_REPEATS + NUM_KEYS, ((volatile destr_test_state_t)state).count);
  156. // cleanup
  157. for (int i = 0; i < NUM_KEYS; i++) {
  158. r = pthread_key_delete(state.keys[i]);
  159. TEST_ASSERT_EQUAL(0, r);
  160. }
  161. }
  162. static void s_test_repeat_destructor(void *vp_state)
  163. {
  164. destr_test_state_t *state = vp_state;
  165. state->count++;
  166. printf("Destructor! Arg %p Count %d\n", state, state->count);
  167. if (state->count > NUM_REPEATS) {
  168. return; // Stop replacing values after NUM_REPEATS destructors have been called, they will be NULLed out now
  169. }
  170. // Find the key which has a NULL value, this is the key for this destructor. We will set it back to 'state' to repeat later.
  171. // At this point only one key should have a NULL value
  172. int null_idx = -1;
  173. for (int i = 0; i < NUM_KEYS; i++) {
  174. if (pthread_getspecific(state->keys[i]) == NULL) {
  175. TEST_ASSERT_EQUAL(-1, null_idx); // If more than one key has a NULL value, something has gone wrong
  176. null_idx = i;
  177. // don't break, verify the other keys have non-NULL values
  178. }
  179. }
  180. TEST_ASSERT_NOT_EQUAL(-1, null_idx); // One key should have a NULL value
  181. // The same key shouldn't be destroyed twice in a row, as new non-NULL values should be destroyed
  182. // after existing non-NULL values (to match spec behaviour)
  183. TEST_ASSERT_NOT_EQUAL(null_idx, state->last_idx);
  184. printf("Re-setting index %d\n", null_idx);
  185. pthread_setspecific(state->keys[null_idx], state);
  186. state->last_idx = null_idx;
  187. }
  188. static void *s_test_repeat_destructor_thread(void *vp_state)
  189. {
  190. destr_test_state_t *state = vp_state;
  191. for (int i = 0; i < NUM_KEYS; i++) {
  192. pthread_setspecific(state->keys[i], state);
  193. }
  194. pthread_exit(NULL);
  195. }