30#ifdef F___lock___sfp_recursive_mutex
31struct __lock __lock___sfp_recursive_mutex;
34#ifdef F___lock___atexit_recursive_mutex
35struct __lock __lock___atexit_recursive_mutex;
38#ifdef F___lock___at_quick_exit_mutex
39struct __lock __lock___at_quick_exit_mutex;
42#ifdef F___lock___malloc_recursive_mutex
43struct __lock __lock___malloc_recursive_mutex;
46#ifdef F___lock___env_recursive_mutex
47struct __lock __lock___env_recursive_mutex;
50#ifdef F___lock___tz_mutex
51struct __lock __lock___tz_mutex;
54#ifdef F___lock___dd_hash_mutex
55struct __lock __lock___dd_hash_mutex;
58#ifdef F___lock___arc4random_mutex
59struct __lock __lock___arc4random_mutex;
62static inline void __common_lock_init(_LOCK_T lock)
69 sema.wait_threads = 0;
70 lock->sem_id = CreateSema(&sema);
75static inline void __common_lock_init_recursive(_LOCK_T lock)
82 sema.wait_threads = 0;
83 lock->sem_id = CreateSema(&sema);
88static inline void __common_lock_close(_LOCK_T lock)
90 DeleteSema(lock->sem_id);
93static inline void __common_lock_close_recursive(_LOCK_T lock)
95 DeleteSema(lock->sem_id);
98#ifdef F___retarget_lock_init
99void __retarget_lock_init(_LOCK_T *lock)
101 _LOCK_T new_lock = (_LOCK_T)malloc(
sizeof(
struct __lock));
102 __common_lock_init(new_lock);
107#ifdef F___retarget_lock_init_recursive
108void __retarget_lock_init_recursive(_LOCK_T *lock)
110 _LOCK_T new_lock = (_LOCK_T)malloc(
sizeof(
struct __lock));
111 __common_lock_init_recursive(new_lock);
116#ifdef F___retarget_lock_close
117void __retarget_lock_close(_LOCK_T lock)
119 __common_lock_close(lock);
124#ifdef F___retarget_lock_close_recursive
125void __retarget_lock_close_recursive(_LOCK_T lock)
127 __common_lock_close_recursive(lock);
132#ifdef F___retarget_lock_acquire
133void __retarget_lock_acquire(_LOCK_T lock)
135 WaitSema(lock->sem_id);
139#ifdef F___retarget_lock_acquire_recursive
140void __retarget_lock_acquire_recursive(_LOCK_T lock)
142 bool starting =
false;
143 int32_t thread_id = GetThreadId();
144 starting = lock->count == 0;
146 lock->thread_id = thread_id;
148 if (lock->thread_id == thread_id) {
151 WaitSema(lock->sem_id);
154 WaitSema(lock->sem_id);
159 lock->thread_id = thread_id;
165#ifdef F___retarget_lock_try_acquire
166int __retarget_lock_try_acquire(_LOCK_T lock)
168 return PollSema(lock->sem_id) > 0 ? 0 : 1;
172#ifdef F___retarget_lock_try_acquire_recursive
173int __retarget_lock_try_acquire_recursive(_LOCK_T lock)
176 bool starting =
false;
177 int32_t thread_id = GetThreadId();
178 starting = lock->count == 0;
180 lock->thread_id = thread_id;
182 if (lock->thread_id == thread_id) {
185 res = PollSema(lock->sem_id) > 0 ? 0 : 1;
188 res = PollSema(lock->sem_id) > 0 ? 0 : 1;
193 lock->thread_id = thread_id;
200#ifdef F___retarget_lock_release
201void __retarget_lock_release(_LOCK_T lock)
203 SignalSema(lock->sem_id);
207#ifdef F___retarget_lock_release_recursive
208void __retarget_lock_release_recursive(_LOCK_T lock)
210 bool tobeRelease =
false;
211 int32_t thread_id = GetThreadId();
212 if (lock->thread_id != thread_id) {
214 perror(
"Error: Trying to release a lock that was not acquired by the current thread");
218 tobeRelease = lock->count == 1;
220 if (lock->count == 0) {
221 lock->thread_id = -1;
224 SignalSema(lock->sem_id);
230extern struct __lock __lock___malloc_recursive_mutex;
231extern struct __lock __lock___atexit_recursive_mutex;
232extern struct __lock __lock___at_quick_exit_mutex;
233extern struct __lock __lock___sfp_recursive_mutex;
234extern struct __lock __lock___env_recursive_mutex;
235extern struct __lock __lock___tz_mutex;
236extern struct __lock __lock___dd_hash_mutex;
237extern struct __lock __lock___arc4random_mutex;
241 _LOCK_T lock_malloc = &__lock___malloc_recursive_mutex;
242 _LOCK_T lock_atexit = &__lock___atexit_recursive_mutex;
243 _LOCK_T lock_quick_exit = &__lock___at_quick_exit_mutex;
244 _LOCK_T lock_sfp = &__lock___sfp_recursive_mutex;
245 _LOCK_T lock_env = &__lock___env_recursive_mutex;
246 _LOCK_T lock_tz = &__lock___tz_mutex;
247 _LOCK_T lock_dd_hash = &__lock___dd_hash_mutex;
248 _LOCK_T lock_arc4random = &__lock___arc4random_mutex;
250 __common_lock_init_recursive(lock_malloc);
251 __common_lock_init_recursive(lock_atexit);
252 __common_lock_init(lock_quick_exit);
253 __common_lock_init_recursive(lock_sfp);
254 __common_lock_init_recursive(lock_env);
255 __common_lock_init(lock_tz);
256 __common_lock_init(lock_dd_hash);
257 __common_lock_init(lock_arc4random);
261#ifdef F___locks_deinit
262extern struct __lock __lock___malloc_recursive_mutex;
263extern struct __lock __lock___atexit_recursive_mutex;
264extern struct __lock __lock___at_quick_exit_mutex;
265extern struct __lock __lock___sfp_recursive_mutex;
266extern struct __lock __lock___env_recursive_mutex;
267extern struct __lock __lock___tz_mutex;
268extern struct __lock __lock___dd_hash_mutex;
269extern struct __lock __lock___arc4random_mutex;
273 _LOCK_T lock_malloc = &__lock___malloc_recursive_mutex;
274 _LOCK_T lock_atexit = &__lock___atexit_recursive_mutex;
275 _LOCK_T lock_quick_exit = &__lock___at_quick_exit_mutex;
276 _LOCK_T lock_sfp = &__lock___sfp_recursive_mutex;
277 _LOCK_T lock_env = &__lock___env_recursive_mutex;
278 _LOCK_T lock_tz = &__lock___tz_mutex;
279 _LOCK_T lock_dd_hash = &__lock___dd_hash_mutex;
280 _LOCK_T lock_arc4random = &__lock___arc4random_mutex;
283 __common_lock_close_recursive(lock_malloc);
284 __common_lock_close_recursive(lock_atexit);
285 __common_lock_close(lock_quick_exit);
286 __common_lock_close_recursive(lock_sfp);
287 __common_lock_close_recursive(lock_env);
288 __common_lock_close(lock_tz);
289 __common_lock_close(lock_dd_hash);
290 __common_lock_close(lock_arc4random);