test_initialization.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <vector>
  7. #include <numeric>
  8. #include <stdexcept>
  9. #include <string>
  10. #include "unity.h"
  11. #include "esp_log.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/semphr.h"
  15. #include "soc/soc.h"
  16. static const char* TAG = "cxx";
  17. class NonPOD
  18. {
  19. public:
  20. NonPOD(int a_) : a(a_) { }
  21. int a;
  22. };
  23. static int non_pod_test_helper(int new_val)
  24. {
  25. static NonPOD non_pod(42);
  26. int ret = non_pod.a;
  27. non_pod.a = new_val;
  28. return ret;
  29. }
  30. TEST_CASE("can use static initializers for non-POD types", "[cxx]")
  31. {
  32. TEST_ASSERT_EQUAL(42, non_pod_test_helper(1));
  33. TEST_ASSERT_EQUAL(1, non_pod_test_helper(0));
  34. }
  35. /*
  36. * This test exercises static initialization guards for two objects.
  37. * For each object, 4 tasks are created which attempt to perform static initialization.
  38. * We check that constructor runs only once for each object.
  39. */
  40. static SemaphoreHandle_t s_slow_init_sem = NULL;
  41. template<int obj>
  42. class SlowInit
  43. {
  44. public:
  45. SlowInit(int arg) {
  46. ESP_LOGD(TAG, "init obj=%d start, arg=%d\n", obj, arg);
  47. vTaskDelay(300/portTICK_PERIOD_MS);
  48. TEST_ASSERT_EQUAL(-1, mInitBy);
  49. TEST_ASSERT_EQUAL(0, mInitCount);
  50. mInitBy = arg;
  51. ++mInitCount;
  52. ESP_LOGD(TAG, "init obj=%d done\n", obj);
  53. }
  54. static void task(void* arg) {
  55. int taskId = reinterpret_cast<int>(arg);
  56. ESP_LOGD(TAG, "obj=%d before static init, task=%d\n", obj, taskId);
  57. static SlowInit slowinit(taskId);
  58. ESP_LOGD(TAG, "obj=%d after static init, task=%d\n", obj, taskId);
  59. xSemaphoreGive(s_slow_init_sem);
  60. vTaskDelete(NULL);
  61. }
  62. private:
  63. static int mInitBy;
  64. static int mInitCount;
  65. };
  66. template<> int SlowInit<1>::mInitBy = -1;
  67. template<> int SlowInit<1>::mInitCount = 0;
  68. template<> int SlowInit<2>::mInitBy = -1;
  69. template<> int SlowInit<2>::mInitCount = 0;
  70. template<int obj>
  71. static int start_slow_init_task(int id, int affinity)
  72. {
  73. return xTaskCreatePinnedToCore(&SlowInit<obj>::task, "slow_init", 2048,
  74. reinterpret_cast<void*>(id), 3, NULL, affinity) ? 1 : 0;
  75. }
  76. TEST_CASE("static initialization guards work as expected", "[cxx]")
  77. {
  78. s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
  79. TEST_ASSERT_NOT_NULL(s_slow_init_sem);
  80. int task_count = 0;
  81. // four tasks competing for static initialization of one object
  82. task_count += start_slow_init_task<1>(0, PRO_CPU_NUM);
  83. #if portNUM_PROCESSORS == 2
  84. task_count += start_slow_init_task<1>(1, APP_CPU_NUM);
  85. #endif
  86. task_count += start_slow_init_task<1>(2, PRO_CPU_NUM);
  87. task_count += start_slow_init_task<1>(3, tskNO_AFFINITY);
  88. // four tasks competing for static initialization of another object
  89. task_count += start_slow_init_task<2>(0, PRO_CPU_NUM);
  90. #if portNUM_PROCESSORS == 2
  91. task_count += start_slow_init_task<2>(1, APP_CPU_NUM);
  92. #endif
  93. task_count += start_slow_init_task<2>(2, PRO_CPU_NUM);
  94. task_count += start_slow_init_task<2>(3, tskNO_AFFINITY);
  95. // All tasks should
  96. for (int i = 0; i < task_count; ++i) {
  97. TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
  98. }
  99. vSemaphoreDelete(s_slow_init_sem);
  100. vTaskDelay(10); // Allow tasks to clean up, avoids race with leak detector
  101. }
  102. struct GlobalInitTest
  103. {
  104. GlobalInitTest() : index(order++) {
  105. }
  106. int index;
  107. static int order;
  108. };
  109. int GlobalInitTest::order = 0;
  110. GlobalInitTest g_init_test1;
  111. GlobalInitTest g_init_test2;
  112. GlobalInitTest g_init_test3;
  113. TEST_CASE("global initializers run in the correct order", "[cxx]")
  114. {
  115. TEST_ASSERT_EQUAL(0, g_init_test1.index);
  116. TEST_ASSERT_EQUAL(1, g_init_test2.index);
  117. TEST_ASSERT_EQUAL(2, g_init_test3.index);
  118. }
  119. struct StaticInitTestBeforeScheduler
  120. {
  121. StaticInitTestBeforeScheduler()
  122. {
  123. static int first_init_order = getOrder();
  124. index = first_init_order;
  125. }
  126. int getOrder()
  127. {
  128. return order++;
  129. }
  130. int index;
  131. static int order;
  132. };
  133. int StaticInitTestBeforeScheduler::order = 1;
  134. StaticInitTestBeforeScheduler g_static_init_test1;
  135. StaticInitTestBeforeScheduler g_static_init_test2;
  136. StaticInitTestBeforeScheduler g_static_init_test3;
  137. TEST_CASE("before scheduler has started, static initializers work correctly", "[cxx]")
  138. {
  139. TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
  140. TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
  141. TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
  142. TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
  143. }
  144. struct PriorityInitTest
  145. {
  146. PriorityInitTest()
  147. {
  148. index = getOrder();
  149. }
  150. int getOrder()
  151. {
  152. return order++;
  153. }
  154. int index;
  155. static int order;
  156. };
  157. int PriorityInitTest::order = 0;
  158. // init_priority objects are initialized from the lowest to the heighest priority number
  159. // Default init_priority is always the lowest (highest priority number)
  160. PriorityInitTest g_static_init_priority_test2;
  161. PriorityInitTest g_static_init_priority_test1 __attribute__((init_priority(1000)));
  162. PriorityInitTest g_static_init_priority_test0 __attribute__((init_priority(999)));
  163. TEST_CASE("init_priority extension works", "[cxx]")
  164. {
  165. TEST_ASSERT_EQUAL(0, g_static_init_priority_test0.index);
  166. TEST_ASSERT_EQUAL(1, g_static_init_priority_test1.index);
  167. TEST_ASSERT_EQUAL(2, g_static_init_priority_test2.index);
  168. }