cxx_guards.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  33. * Layout of the guard object (defined by the ABI).
  34. *
  35. * Compiler will check lower byte before calling guard functions.
  36. */
  37. typedef struct {
  38. uint8_t ready; //!< nonzero if initialization is done
  39. uint8_t pending; //!< nonzero if initialization is in progress
  40. } guard_t;
  41. static void static_init_prepare()
  42. {
  43. portENTER_CRITICAL(&s_init_spinlock);
  44. if (s_static_init_mutex == NULL) {
  45. s_static_init_mutex = xSemaphoreCreateMutex();
  46. s_static_init_wait_sem = xSemaphoreCreateCounting(INT_MAX, 0);
  47. if (s_static_init_mutex == NULL || s_static_init_wait_sem == NULL) {
  48. // no way to bail out of static initialization without these
  49. abort();
  50. }
  51. }
  52. portEXIT_CRITICAL(&s_init_spinlock);
  53. }
  54. /**
  55. * Use s_static_init_wait_sem to wait until guard->pending == 0.
  56. * Preconditions:
  57. * - s_static_init_mutex taken
  58. * - guard.pending == 1
  59. * Postconditions:
  60. * - s_static_init_mutex taken
  61. * - guard.pending == 0
  62. */
  63. static void wait_for_guard_obj(guard_t* g)
  64. {
  65. s_static_init_waiting_count++;
  66. #ifndef _NDEBUG
  67. s_static_init_max_waiting_count = std::max(s_static_init_waiting_count,
  68. s_static_init_max_waiting_count);
  69. #endif
  70. do {
  71. auto result = xSemaphoreGive(s_static_init_mutex);
  72. assert(result);
  73. /* Task may be preempted here, but this isn't a problem,
  74. * as the semaphore will be given exactly the s_static_init_waiting_count
  75. * number of times; eventually the current task will execute next statement,
  76. * which will immediately succeed.
  77. */
  78. result = xSemaphoreTake(s_static_init_wait_sem, portMAX_DELAY);
  79. assert(result);
  80. /* At this point the semaphore was given, so all waiting tasks have woken up.
  81. * We take s_static_init_mutex before accessing the state of the guard
  82. * object again.
  83. */
  84. result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  85. assert(result);
  86. /* Semaphore may have been given because some other guard object became ready.
  87. * Check the guard object we need and wait again if it is still pending.
  88. */
  89. } while(g->pending);
  90. s_static_init_waiting_count--;
  91. }
  92. /**
  93. * Unblock tasks waiting for static initialization to complete.
  94. * Preconditions:
  95. * - s_static_init_mutex taken
  96. * Postconditions:
  97. * - s_static_init_mutex taken
  98. */
  99. static void signal_waiting_tasks()
  100. {
  101. auto count = s_static_init_waiting_count;
  102. while (count--) {
  103. xSemaphoreGive(s_static_init_wait_sem);
  104. }
  105. }
  106. extern "C" int __cxa_guard_acquire(__guard* pg)
  107. {
  108. guard_t* g = reinterpret_cast<guard_t*>(pg);
  109. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  110. if (!scheduler_started) {
  111. if (g->pending) {
  112. /* Before the scheduler has started, there we don't support simultaneous
  113. * static initialization. This may be implemented using a spinlock and a
  114. * s32c1i instruction, though.
  115. */
  116. abort();
  117. }
  118. } else {
  119. if (s_static_init_mutex == NULL) {
  120. static_init_prepare();
  121. }
  122. /* We don't need to use double-checked locking pattern here, as the compiler
  123. * must generate code to check if the first byte of *pg is non-zero, before
  124. * calling __cxa_guard_acquire.
  125. */
  126. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  127. assert(result);
  128. if (g->pending) {
  129. /* Another task is doing initialization at the moment; wait until it calls
  130. * __cxa_guard_release or __cxa_guard_abort
  131. */
  132. wait_for_guard_obj(g);
  133. /* At this point there are two scenarios:
  134. * - the task which was doing static initialization has called __cxa_guard_release,
  135. * which means that g->ready is set. We need to return 0.
  136. * - the task which was doing static initialization has called __cxa_guard_abort,
  137. * which means that g->ready is not set; we should acquire the guard and return 1,
  138. * same as for the case if we didn't have to wait.
  139. * Note: actually the second scenario is unlikely to occur in the current
  140. * configuration because exception support is disabled.
  141. */
  142. }
  143. }
  144. int ret;
  145. if (g->ready) {
  146. /* Static initialization has been done by another task; nothing to do here */
  147. ret = 0;
  148. } else {
  149. /* Current task can start doing static initialization */
  150. g->pending = 1;
  151. ret = 1;
  152. }
  153. if (scheduler_started) {
  154. auto result = xSemaphoreGive(s_static_init_mutex);
  155. assert(result);
  156. }
  157. return ret;
  158. }
  159. extern "C" void __cxa_guard_release(__guard* pg)
  160. {
  161. guard_t* g = reinterpret_cast<guard_t*>(pg);
  162. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  163. if (scheduler_started) {
  164. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  165. assert(result);
  166. }
  167. assert(g->pending && "tried to release a guard which wasn't acquired");
  168. g->pending = 0;
  169. /* Initialization was successful */
  170. g->ready = 1;
  171. if (scheduler_started) {
  172. /* Unblock the tasks waiting for static initialization to complete */
  173. signal_waiting_tasks();
  174. auto result = xSemaphoreGive(s_static_init_mutex);
  175. assert(result);
  176. }
  177. }
  178. extern "C" void __cxa_guard_abort(__guard* pg)
  179. {
  180. guard_t* g = reinterpret_cast<guard_t*>(pg);
  181. const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
  182. if (scheduler_started) {
  183. auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
  184. assert(result);
  185. }
  186. assert(!g->ready && "tried to abort a guard which is ready");
  187. assert(g->pending && "tried to release a guard which is not acquired");
  188. g->pending = 0;
  189. if (scheduler_started) {
  190. /* Unblock the tasks waiting for static initialization to complete */
  191. signal_waiting_tasks();
  192. auto result = xSemaphoreGive(s_static_init_mutex);
  193. assert(result);
  194. }
  195. }
  196. /**
  197. * Dummy function used to force linking this file instead of the same one in libstdc++.
  198. * This works via -u __cxa_guard_dummy flag in component.mk
  199. */
  200. extern "C" void __cxa_guard_dummy()
  201. {
  202. }