Skip to content

Commit ef45ef8

Browse files
committed
Fix GCC locks for lazy object initailization
Implement the functions __cxa_guard_acquire, __cxa_guard_release and __cxa_guard_abort so lazily initialized function-local static objects are done so in a thread safe manner in GCC.
1 parent 69431da commit ef45ef8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

hal/common/retarget.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,43 @@ extern "C" void __env_unlock( struct _reent *_r )
713713
{
714714
__rtos_env_unlock(_r);
715715
}
716+
717+
#define CXA_GUARD_INIT_DONE (1 << 0)
718+
#define CXA_GUARD_INIT_IN_PROGRESS (1 << 1)
719+
#define CXA_GUARD_MASK (CXA_GUARD_INIT_DONE | CXA_GUARD_INIT_IN_PROGRESS)
720+
721+
extern "C" int __cxa_guard_acquire(int *guard_object_p)
722+
{
723+
uint8_t *guard_object = (uint8_t *)guard_object_p;
724+
if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
725+
return 0;
726+
}
727+
singleton_lock();
728+
if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
729+
singleton_unlock();
730+
return 0;
731+
}
732+
MBED_ASSERT(0 == (*guard_object & CXA_GUARD_MASK));
733+
*guard_object = *guard_object | CXA_GUARD_INIT_IN_PROGRESS;
734+
return 1;
735+
}
736+
737+
extern "C" void __cxa_guard_release(int *guard_object_p)
738+
{
739+
uint8_t *guard_object = (uint8_t *)guard_object_p;
740+
MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
741+
*guard_object = (*guard_object & ~CXA_GUARD_MASK) | CXA_GUARD_INIT_DONE;
742+
singleton_unlock();
743+
}
744+
745+
extern "C" void __cxa_guard_abort(int *guard_object_p)
746+
{
747+
uint8_t *guard_object = (uint8_t *)guard_object_p;
748+
MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
749+
*guard_object = *guard_object & ~CXA_GUARD_INIT_IN_PROGRESS;
750+
singleton_unlock();
751+
}
752+
716753
#endif
717754

718755
void *operator new(std::size_t count)

0 commit comments

Comments
 (0)