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
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <cxxabi.h>
18 #include <stdint.h>
19 #include <limits.h>
20 #include <algorithm>
21 #include <sys/lock.h>
22 #include "esp_osal/esp_osal.h"
23 #include "esp_osal/semphr.h"
24 #include "esp_osal/task.h"
25
26 using __cxxabiv1::__guard;
27
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
32 #ifndef _NDEBUG
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
34 #endif
35
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(void);
40
41 /**
42 * Layout of the guard object (defined by the ABI).
43 *
44 * Compiler will check lower byte before calling guard functions.
45 */
46 typedef struct {
47 uint8_t ready; //!< nonzero if initialization is done
48 uint8_t pending; //!< nonzero if initialization is in progress
49 } guard_t;
50
static_init_prepare()51 static void static_init_prepare()
52 {
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
59 abort();
60 }
61 }
62 portEXIT_CRITICAL(&s_init_spinlock);
63 }
64
65 /**
66 * Use s_static_init_wait_sem to wait until guard->pending == 0.
67 * Preconditions:
68 * - s_static_init_mutex taken
69 * - guard.pending == 1
70 * Postconditions:
71 * - s_static_init_mutex taken
72 * - guard.pending == 0
73 */
wait_for_guard_obj(guard_t * g)74 static void wait_for_guard_obj(guard_t* g)
75 {
76 s_static_init_waiting_count++;
77 #ifndef _NDEBUG
78 s_static_init_max_waiting_count = std::max(s_static_init_waiting_count,
79 s_static_init_max_waiting_count);
80 #endif
81
82 do {
83 auto result = xSemaphoreGive(s_static_init_mutex);
84 assert(result);
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.
89 */
90 result = xSemaphoreTake(s_static_init_wait_sem, portMAX_DELAY);
91 assert(result);
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
94 * object again.
95 */
96 result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
97 assert(result);
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.
100 */
101 } while(g->pending);
102 s_static_init_waiting_count--;
103 }
104
105 /**
106 * Unblock tasks waiting for static initialization to complete.
107 * Preconditions:
108 * - s_static_init_mutex taken
109 * Postconditions:
110 * - s_static_init_mutex taken
111 */
signal_waiting_tasks()112 static void signal_waiting_tasks()
113 {
114 auto count = s_static_init_waiting_count;
115 while (count--) {
116 xSemaphoreGive(s_static_init_wait_sem);
117 }
118 }
119
__cxa_guard_acquire(__guard * pg)120 extern "C" int __cxa_guard_acquire(__guard* pg)
121 {
122 guard_t* g = reinterpret_cast<guard_t*>(pg);
123 const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
124 if (!scheduler_started) {
125 if (g->pending) {
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.
129 */
130 abort();
131 }
132 } else {
133 if (s_static_init_mutex == NULL) {
134 static_init_prepare();
135 }
136
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.
140 */
141 auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
142 assert(result);
143 if (g->pending) {
144 /* Another task is doing initialization at the moment; wait until it calls
145 * __cxa_guard_release or __cxa_guard_abort
146 */
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.
156 */
157 }
158 }
159 int ret;
160 if (g->ready) {
161 /* Static initialization has been done by another task; nothing to do here */
162 ret = 0;
163 } else {
164 /* Current task can start doing static initialization */
165 g->pending = 1;
166 ret = 1;
167 }
168 if (scheduler_started) {
169 auto result = xSemaphoreGive(s_static_init_mutex);
170 assert(result);
171 }
172 return ret;
173 }
174
__cxa_guard_release(__guard * pg)175 extern "C" void __cxa_guard_release(__guard* pg)
176 {
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);
181 assert(result);
182 }
183 assert(g->pending && "tried to release a guard which wasn't acquired");
184 g->pending = 0;
185 /* Initialization was successful */
186 g->ready = 1;
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);
191 assert(result);
192 }
193 }
194
__cxa_guard_abort(__guard * pg)195 extern "C" void __cxa_guard_abort(__guard* pg)
196 {
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);
201 assert(result);
202 }
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");
205 g->pending = 0;
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);
210 assert(result);
211 }
212 }
213
214 /**
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
217 */
__cxa_guard_dummy(void)218 extern "C" void __cxa_guard_dummy(void)
219 {
220 }
221