cxx_guards.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <cxxabi.h>
  17. #include <stdint.h>
  18. #include <limits.h>
  19. #include <algorithm>
  20. #include <sys/lock.h>
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/semphr.h"
  23. #include "freertos/task.h"
  24. using __cxxabiv1::__guard;
  25. static SemaphoreHandle_t s_static_init_mutex = NULL; //!< lock used for the critical section
  26. static SemaphoreHandle_t s_static_init_wait_sem = NULL; //!< counting semaphore used by the waiting tasks
  27. static portMUX_TYPE s_init_spinlock = portMUX_INITIALIZER_UNLOCKED; //!< spinlock used to guard initialization of the above two primitives
  28. static size_t s_static_init_waiting_count = 0; //!< number of tasks which are waiting for static init guards
  29. #ifndef _NDEBUG
  30. static size_t s_static_init_max_waiting_count = 0; //!< maximum ever value of the above; can be inspected using GDB for debugging purposes
  31. #endif
  32. extern "C" int __cxa_guard_acquire(__guard* pg);
  33. extern "C" void __cxa_guard_release(__guard* pg);
  34. extern "C" void __cxa_guard_abort(__guard* pg);
  35. extern "C" void __cxa_guard_dummy(void);
  36. /**
  37. * Layout of the guard object (defined by the ABI).
  38. *
  39. * Compiler will check lower byte before calling guard functions.
  40. */
  41. typedef struct {
  42. uint8_t ready; //!< nonzero if initialization is done
  43. uint8_t pending; //!< nonzero if initialization is in progress
  44. } guard_t;
  45. static void static_init_prepare()
  46. {
  47. portENTER_CRITICAL(&s_init_spinlock);
  48. if (s_static_init_mutex == NULL) {
  49. s_static_init_mutex = xSemaphoreCreateMutex();
  50. s_static_init_wait_sem = xSemaphoreCreateCounting(INT_MAX, 0);
  51. if (s_static_init_mutex == NULL || s_static_init_wait_sem == NULL) {
  52. // no way to bail out of static initialization without these
  53. abort();
  54. }
  55. }
  56. portEXIT_CRITICAL(&s_init_spinlock);
  57. }
  58. /**
  59. * Use s_static_init_wait_sem to wait until guard->pending == 0.
  60. * Preconditions:
  61. * - s_static_init_mutex taken
  62. * - guard.pending == 1
  63. * Postconditions:
  64. * - s_static_init_mutex taken
  65. * - guard.pending == 0
  66. */
  67. static void wait_for_guard_obj(guard_t* g)
  68. {
  69. s_static_init_waiting_count++;
  70. #ifndef _NDEBUG
  71. s_static_init_max_waiting_count = std::max(s_static_init_waiting_count,
  72. s_static_init_max_waiting_count);
  73. #endif
  74. do {
  75. auto result = xSemaphoreGive(s_static_init_mutex);
  76. assert(result);
  77. /* Task may be preempted here, but this isn't a problem,
  78. * as the semaphore will be given exactly the s_static_init_waiting_count
  79. * number of times; eventually the current task will execute next statement,
  80. * which will immediately succeed.
  81. */
  82. result = xSemaphoreTake(s_static_init_wait_sem, portMAX_DELAY);
  83. assert(result);
  84. /* At this point the semaphore was given, so all waiting tasks have woken up.
  85. * We take s_static_init_mutex before accessing the state of the guard
  86. * object again.
  87. */
  88. result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  89. assert(result);
  90. /* Semaphore may have been given because some other guard object became ready.
  91. * Check the guard object we need and wait again if it is still pending.
  92. */
  93. } while(g->pending);
  94. s_static_init_waiting_count--;
  95. }
  96. /**
  97. * Unblock tasks waiting for static initialization to complete.
  98. * Preconditions:
  99. * - s_static_init_mutex taken
  100. * Postconditions:
  101. * - s_static_init_mutex taken
  102. */
  103. static void signal_waiting_tasks()
  104. {
  105. auto count = s_static_init_waiting_count;
  106. while (count--) {
  107. xSemaphoreGive(s_static_init_wait_sem);
  108. }
  109. }
  110. extern "C" int __cxa_guard_acquire(__guard* pg)
  111. {
  112. guard_t* g = reinterpret_cast<guard_t*>(pg);
  113. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  114. if (!scheduler_started) {
  115. if (g->pending) {
  116. /* Before the scheduler has started, there we don't support simultaneous
  117. * static initialization. This may be implemented using a spinlock and a
  118. * s32c1i instruction, though.
  119. */
  120. abort();
  121. }
  122. } else {
  123. if (s_static_init_mutex == NULL) {
  124. static_init_prepare();
  125. }
  126. /* We don't need to use double-checked locking pattern here, as the compiler
  127. * must generate code to check if the first byte of *pg is non-zero, before
  128. * calling __cxa_guard_acquire.
  129. */
  130. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  131. assert(result);
  132. if (g->pending) {
  133. /* Another task is doing initialization at the moment; wait until it calls
  134. * __cxa_guard_release or __cxa_guard_abort
  135. */
  136. wait_for_guard_obj(g);
  137. /* At this point there are two scenarios:
  138. * - the task which was doing static initialization has called __cxa_guard_release,
  139. * which means that g->ready is set. We need to return 0.
  140. * - the task which was doing static initialization has called __cxa_guard_abort,
  141. * which means that g->ready is not set; we should acquire the guard and return 1,
  142. * same as for the case if we didn't have to wait.
  143. * Note: actually the second scenario is unlikely to occur in the current
  144. * configuration because exception support is disabled.
  145. */
  146. }
  147. }
  148. int ret;
  149. if (g->ready) {
  150. /* Static initialization has been done by another task; nothing to do here */
  151. ret = 0;
  152. } else {
  153. /* Current task can start doing static initialization */
  154. g->pending = 1;
  155. ret = 1;
  156. }
  157. if (scheduler_started) {
  158. auto result = xSemaphoreGive(s_static_init_mutex);
  159. assert(result);
  160. }
  161. return ret;
  162. }
  163. extern "C" void __cxa_guard_release(__guard* pg)
  164. {
  165. guard_t* g = reinterpret_cast<guard_t*>(pg);
  166. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  167. if (scheduler_started) {
  168. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  169. assert(result);
  170. }
  171. assert(g->pending && "tried to release a guard which wasn't acquired");
  172. g->pending = 0;
  173. /* Initialization was successful */
  174. g->ready = 1;
  175. if (scheduler_started) {
  176. /* Unblock the tasks waiting for static initialization to complete */
  177. signal_waiting_tasks();
  178. auto result = xSemaphoreGive(s_static_init_mutex);
  179. assert(result);
  180. }
  181. }
  182. extern "C" void __cxa_guard_abort(__guard* pg)
  183. {
  184. guard_t* g = reinterpret_cast<guard_t*>(pg);
  185. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  186. if (scheduler_started) {
  187. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  188. assert(result);
  189. }
  190. assert(!g->ready && "tried to abort a guard which is ready");
  191. assert(g->pending && "tried to release a guard which is not acquired");
  192. g->pending = 0;
  193. if (scheduler_started) {
  194. /* Unblock the tasks waiting for static initialization to complete */
  195. signal_waiting_tasks();
  196. auto result = xSemaphoreGive(s_static_init_mutex);
  197. assert(result);
  198. }
  199. }
  200. /**
  201. * Dummy function used to force linking this file instead of the same one in libstdc++.
  202. * This works via -u __cxa_guard_dummy flag in component.mk
  203. */
  204. extern "C" void __cxa_guard_dummy(void)
  205. {
  206. }