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