test_pthread_local_storage.c 3.8 KB

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