-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thread-safe static initialization for interrupts and FreeRTOS #345
Comments
Additional headaches: I've created a simple example that recursively initializes a static class: void constructDummy();
class Dummy
{
public:
Dummy()
{
MODM_LOG_INFO << "Dummy class constructed" << modm::endl;
constructDummy(); // recursive initialization
}
};
void constructDummy()
{
static Dummy dummy;
MODM_LOG_INFO << "constructDummy() called" << modm::endl;
} With For that alone, we should overwrite this terrible default implementation. |
I use compare-and-swap intrinsics here: https://github.com/embeddedartistry/libcpp/blob/master/src/lightabi/cxa_guard.cpp |
I checked carefully, and I'm fairly certain that this is also how I'm doing it, minus the support for thread-safety at the moment. |
During #343, I discovered that we compile with
-fno-threadsafe-statics
.Background information (these are some good reads):
When compiling without this flag, the default implementation of stdlibc++ is used, which compiles to this:
which if you compare this to the source code and mentally resolve all necessary #defines, is basically this simple implementation:
This traps recursive initialization, however, it is neither atomic nor interrupt-safe on Cortex-M where interrupts can be nested! I would assume that accessing the guard atomically would suffice to make it interrupt safe for plain code, I don't think we need to lock the entire initialization procedure (you could rely on interrupts inside a constructor by logging for example).
However, when using the FreeRTOS module (or any other RTOS), static initialization needs to additionally be guarded by a mutex to prevent context switching during initialization?
cc @chris-durand @rleh @dergraaf
The text was updated successfully, but these errors were encountered: