cxx_guards.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. static_cast<void>(result);
  78. /* Task may be preempted here, but this isn't a problem,
  79. * as the semaphore will be given exactly the s_static_init_waiting_count
  80. * number of times; eventually the current task will execute next statement,
  81. * which will immediately succeed.
  82. */
  83. result = xSemaphoreTake(s_static_init_wait_sem, portMAX_DELAY);
  84. assert(result);
  85. /* At this point the semaphore was given, so all waiting tasks have woken up.
  86. * We take s_static_init_mutex before accessing the state of the guard
  87. * object again.
  88. */
  89. result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  90. assert(result);
  91. /* Semaphore may have been given because some other guard object became ready.
  92. * Check the guard object we need and wait again if it is still pending.
  93. */
  94. } while(g->pending);
  95. s_static_init_waiting_count--;
  96. }
  97. /**
  98. * Unblock tasks waiting for static initialization to complete.
  99. * Preconditions:
  100. * - s_static_init_mutex taken
  101. * Postconditions:
  102. * - s_static_init_mutex taken
  103. */
  104. static void signal_waiting_tasks()
  105. {
  106. auto count = s_static_init_waiting_count;
  107. while (count--) {
  108. xSemaphoreGive(s_static_init_wait_sem);
  109. }
  110. }
  111. extern "C" int __cxa_guard_acquire(__guard* pg)
  112. {
  113. guard_t* g = reinterpret_cast<guard_t*>(pg);
  114. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  115. if (!scheduler_started) {
  116. if (g->pending) {
  117. /* Before the scheduler has started, there we don't support simultaneous
  118. * static initialization. This may be implemented using a spinlock and a
  119. * s32c1i instruction, though.
  120. */
  121. abort();
  122. }
  123. } else {
  124. if (s_static_init_mutex == NULL) {
  125. static_init_prepare();
  126. }
  127. /* We don't need to use double-checked locking pattern here, as the compiler
  128. * must generate code to check if the first byte of *pg is non-zero, before
  129. * calling __cxa_guard_acquire.
  130. */
  131. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  132. assert(result);
  133. static_cast<void>(result);
  134. if (g->pending) {
  135. /* Another task is doing initialization at the moment; wait until it calls
  136. * __cxa_guard_release or __cxa_guard_abort
  137. */
  138. wait_for_guard_obj(g);
  139. /* At this point there are two scenarios:
  140. * - the task which was doing static initialization has called __cxa_guard_release,
  141. * which means that g->ready is set. We need to return 0.
  142. * - the task which was doing static initialization has called __cxa_guard_abort,
  143. * which means that g->ready is not set; we should acquire the guard and return 1,
  144. * same as for the case if we didn't have to wait.
  145. * Note: actually the second scenario is unlikely to occur in the current
  146. * configuration because exception support is disabled.
  147. */
  148. }
  149. }
  150. int ret;
  151. if (g->ready) {
  152. /* Static initialization has been done by another task; nothing to do here */
  153. ret = 0;
  154. } else {
  155. /* Current task can start doing static initialization */
  156. g->pending = 1;
  157. ret = 1;
  158. }
  159. if (scheduler_started) {
  160. auto result = xSemaphoreGive(s_static_init_mutex);
  161. assert(result);
  162. static_cast<void>(result);
  163. }
  164. return ret;
  165. }
  166. extern "C" void __cxa_guard_release(__guard* pg)
  167. {
  168. guard_t* g = reinterpret_cast<guard_t*>(pg);
  169. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  170. if (scheduler_started) {
  171. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  172. assert(result);
  173. static_cast<void>(result);
  174. }
  175. assert(g->pending && "tried to release a guard which wasn't acquired");
  176. g->pending = 0;
  177. /* Initialization was successful */
  178. g->ready = 1;
  179. if (scheduler_started) {
  180. /* Unblock the tasks waiting for static initialization to complete */
  181. signal_waiting_tasks();
  182. auto result = xSemaphoreGive(s_static_init_mutex);
  183. assert(result);
  184. static_cast<void>(result);
  185. }
  186. }
  187. extern "C" void __cxa_guard_abort(__guard* pg)
  188. {
  189. guard_t* g = reinterpret_cast<guard_t*>(pg);
  190. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  191. if (scheduler_started) {
  192. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  193. assert(result);
  194. static_cast<void>(result);
  195. }
  196. assert(!g->ready && "tried to abort a guard which is ready");
  197. assert(g->pending && "tried to release a guard which is not acquired");
  198. g->pending = 0;
  199. if (scheduler_started) {
  200. /* Unblock the tasks waiting for static initialization to complete */
  201. signal_waiting_tasks();
  202. auto result = xSemaphoreGive(s_static_init_mutex);
  203. assert(result);
  204. static_cast<void>(result);
  205. }
  206. }
  207. /**
  208. * Dummy function used to force linking this file instead of the same one in libstdc++.
  209. * This works via -u __cxa_guard_dummy flag in component.mk
  210. */
  211. extern "C" void __cxa_guard_dummy(void)
  212. {
  213. }