test_pthread_local_storage.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }