test_pthread_local_storage.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. TEST_CASE("pthread local storage basics", "[pthread]")
  8. {
  9. pthread_key_t key;
  10. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, NULL));
  11. TEST_ASSERT_NULL(pthread_getspecific(key));
  12. int val = 3;
  13. printf("Setting to %p...\n", &val);
  14. TEST_ASSERT_EQUAL(0, pthread_setspecific(key, &val));
  15. printf("Reading back...\n");
  16. TEST_ASSERT_EQUAL_PTR(&val, pthread_getspecific(key));
  17. printf("Setting to NULL...\n");
  18. TEST_ASSERT_EQUAL(0, pthread_setspecific(key, NULL));
  19. printf("Reading back...\n");
  20. TEST_ASSERT_NULL(pthread_getspecific(key));
  21. TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
  22. }
  23. TEST_CASE("pthread local storage unique keys", "[pthread]")
  24. {
  25. const int NUM_KEYS = 10;
  26. pthread_key_t keys[NUM_KEYS];
  27. for (int i = 0; i < NUM_KEYS; i++) {
  28. TEST_ASSERT_EQUAL(0, pthread_key_create(&keys[i], NULL));
  29. printf("New key %d = %d\n", i, keys[i]);
  30. }
  31. for (int i = 0; i < NUM_KEYS; i++) {
  32. for (int j = 0; j < NUM_KEYS; j++) {
  33. if (i != j) {
  34. TEST_ASSERT_NOT_EQUAL(keys[i], keys[j]);
  35. }
  36. }
  37. }
  38. for (int i = 0; i < NUM_KEYS; i++) {
  39. TEST_ASSERT_EQUAL(0, pthread_key_delete(keys[i]));
  40. }
  41. }
  42. static void test_pthread_destructor(void *);
  43. static void *expected_destructor_ptr;
  44. static void *actual_destructor_ptr;
  45. static void *thread_test_pthread_destructor(void *);
  46. TEST_CASE("pthread local storage destructor", "[pthread]")
  47. {
  48. pthread_t thread;
  49. pthread_key_t key = -1;
  50. expected_destructor_ptr = NULL;
  51. actual_destructor_ptr = NULL;
  52. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
  53. TEST_ASSERT_EQUAL(0, pthread_create(&thread, NULL, thread_test_pthread_destructor, (void *)key));
  54. TEST_ASSERT_EQUAL(0, pthread_join(thread, NULL));
  55. printf("Joined...\n");
  56. TEST_ASSERT_NOT_NULL(expected_destructor_ptr);
  57. TEST_ASSERT_NOT_NULL(actual_destructor_ptr);
  58. TEST_ASSERT_EQUAL_PTR(expected_destructor_ptr, actual_destructor_ptr);
  59. TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
  60. }
  61. static void task_test_pthread_destructor(void *v_key);
  62. TEST_CASE("pthread local storage destructor in FreeRTOS task", "[pthread]")
  63. {
  64. // Same as previous test case, but doesn't use pthread APIs therefore must wait
  65. // for the idle task to call the destructor
  66. pthread_key_t key = -1;
  67. expected_destructor_ptr = NULL;
  68. actual_destructor_ptr = NULL;
  69. TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
  70. xTaskCreate(task_test_pthread_destructor,
  71. "ptdest", 8192, (void *)key, UNITY_FREERTOS_PRIORITY+1,
  72. NULL);
  73. // Above task has higher priority to us, so should run immediately
  74. // but we need to wait for the idle task cleanup to run
  75. vTaskDelay(20);
  76. TEST_ASSERT_NOT_NULL(expected_destructor_ptr);
  77. TEST_ASSERT_NOT_NULL(actual_destructor_ptr);
  78. TEST_ASSERT_EQUAL_PTR(expected_destructor_ptr, actual_destructor_ptr);
  79. TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
  80. }
  81. static void *thread_test_pthread_destructor(void *v_key)
  82. {
  83. printf("Local storage thread running...\n");
  84. pthread_key_t key = (pthread_key_t) v_key;
  85. expected_destructor_ptr = &key; // address of stack variable in the task...
  86. pthread_setspecific(key, expected_destructor_ptr);
  87. printf("Local storage thread done.\n");
  88. return NULL;
  89. }
  90. static void test_pthread_destructor(void *value)
  91. {
  92. printf("Destructor called...\n");
  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. }