1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "freertos/FreeRTOS.h"
23 #include "freertos/semphr.h"
24 #include "freertos/task.h"
26 using __cxxabiv1::__guard;
28 static SemaphoreHandle_t s_static_init_mutex = NULL; //!< lock used for the critical section
29 static SemaphoreHandle_t s_static_init_wait_sem = NULL; //!< counting semaphore used by the waiting tasks
30 static portMUX_TYPE s_init_spinlock = portMUX_INITIALIZER_UNLOCKED; //!< spinlock used to guard initialization of the above two primitives
31 static size_t s_static_init_waiting_count = 0; //!< number of tasks which are waiting for static init guards
33 static size_t s_static_init_max_waiting_count = 0; //!< maximum ever value of the above; can be inspected using GDB for debugging purposes
36 extern "C" int __cxa_guard_acquire(__guard* pg);
37 extern "C" void __cxa_guard_release(__guard* pg);
38 extern "C" void __cxa_guard_abort(__guard* pg);
39 extern "C" void __cxa_guard_dummy();
42 * Layout of the guard object (defined by the ABI).
44 * Compiler will check lower byte before calling guard functions.
47 uint8_t ready; //!< nonzero if initialization is done
48 uint8_t pending; //!< nonzero if initialization is in progress
51 static void static_init_prepare()
53 portENTER_CRITICAL(&s_init_spinlock);
54 if (s_static_init_mutex == NULL) {
55 s_static_init_mutex = xSemaphoreCreateMutex();
56 s_static_init_wait_sem = xSemaphoreCreateCounting(INT_MAX, 0);
57 if (s_static_init_mutex == NULL || s_static_init_wait_sem == NULL) {
58 // no way to bail out of static initialization without these
62 portEXIT_CRITICAL(&s_init_spinlock);
66 * Use s_static_init_wait_sem to wait until guard->pending == 0.
68 * - s_static_init_mutex taken
69 * - guard.pending == 1
71 * - s_static_init_mutex taken
72 * - guard.pending == 0
74 static void wait_for_guard_obj(guard_t* g)
76 s_static_init_waiting_count++;
78 s_static_init_max_waiting_count = std::max(s_static_init_waiting_count,
79 s_static_init_max_waiting_count);
83 auto result = xSemaphoreGive(s_static_init_mutex);
85 /* Task may be preempted here, but this isn't a problem,
86 * as the semaphore will be given exactly the s_static_init_waiting_count
87 * number of times; eventually the current task will execute next statement,
88 * which will immediately succeed.
90 result = xSemaphoreTake(s_static_init_wait_sem, portMAX_DELAY);
92 /* At this point the semaphore was given, so all waiting tasks have woken up.
93 * We take s_static_init_mutex before accessing the state of the guard
96 result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
98 /* Semaphore may have been given because some other guard object became ready.
99 * Check the guard object we need and wait again if it is still pending.
102 s_static_init_waiting_count--;
106 * Unblock tasks waiting for static initialization to complete.
108 * - s_static_init_mutex taken
110 * - s_static_init_mutex taken
112 static void signal_waiting_tasks()
114 auto count = s_static_init_waiting_count;
116 xSemaphoreGive(s_static_init_wait_sem);
120 extern "C" int __cxa_guard_acquire(__guard* pg)
122 guard_t* g = reinterpret_cast<guard_t*>(pg);
123 const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
124 if (!scheduler_started) {
126 /* Before the scheduler has started, there we don't support simultaneous
127 * static initialization. This may be implemented using a spinlock and a
128 * s32c1i instruction, though.
133 if (s_static_init_mutex == NULL) {
134 static_init_prepare();
137 /* We don't need to use double-checked locking pattern here, as the compiler
138 * must generate code to check if the first byte of *pg is non-zero, before
139 * calling __cxa_guard_acquire.
141 auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
144 /* Another task is doing initialization at the moment; wait until it calls
145 * __cxa_guard_release or __cxa_guard_abort
147 wait_for_guard_obj(g);
148 /* At this point there are two scenarios:
149 * - the task which was doing static initialization has called __cxa_guard_release,
150 * which means that g->ready is set. We need to return 0.
151 * - the task which was doing static initialization has called __cxa_guard_abort,
152 * which means that g->ready is not set; we should acquire the guard and return 1,
153 * same as for the case if we didn't have to wait.
154 * Note: actually the second scenario is unlikely to occur in the current
155 * configuration because exception support is disabled.
161 /* Static initialization has been done by another task; nothing to do here */
164 /* Current task can start doing static initialization */
168 if (scheduler_started) {
169 auto result = xSemaphoreGive(s_static_init_mutex);
175 extern "C" void __cxa_guard_release(__guard* pg)
177 guard_t* g = reinterpret_cast<guard_t*>(pg);
178 const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
179 if (scheduler_started) {
180 auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
183 assert(g->pending && "tried to release a guard which wasn't acquired");
185 /* Initialization was successful */
187 if (scheduler_started) {
188 /* Unblock the tasks waiting for static initialization to complete */
189 signal_waiting_tasks();
190 auto result = xSemaphoreGive(s_static_init_mutex);
195 extern "C" void __cxa_guard_abort(__guard* pg)
197 guard_t* g = reinterpret_cast<guard_t*>(pg);
198 const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
199 if (scheduler_started) {
200 auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
203 assert(!g->ready && "tried to abort a guard which is ready");
204 assert(g->pending && "tried to release a guard which is not acquired");
206 if (scheduler_started) {
207 /* Unblock the tasks waiting for static initialization to complete */
208 signal_waiting_tasks();
209 auto result = xSemaphoreGive(s_static_init_mutex);
215 * Dummy function used to force linking this file instead of the same one in libstdc++.
216 * This works via -u __cxa_guard_dummy flag in component.mk
218 extern "C" void __cxa_guard_dummy()