Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Created May 16, 2017 18:33
Show Gist options
  • Save yabberyabber/16ac208e1ea877c634f060bc9488f5e2 to your computer and use it in GitHub Desktop.
Save yabberyabber/16ac208e1ea877c634f060bc9488f5e2 to your computer and use it in GitHub Desktop.
/**
* The following preprocessor form allows one to wrap C functions
*/
#define-func-decorator <any> critical_region(args, func)
{
bool enable_interrupts = false;
if (are_interrupts_enabled()) {
DISABLE_INTERRUPTS();
enable_interrupts = true;
}
<any> ret = func(args);
if (enable_interrupts) {
ENABLE_INTERRUPTS();
}
return ret;
}
#decorate[critical_region]
int something_important(int j) {
int i = 0;
if (i == j) {
return 9;
}
return i + 8;
}
/**
* The above code should reduce to something equivalent to the following:
*/
int something_important(int j) {
bool enable_interrupts = false;
if (are_interrupts_enabled()) {
DISABLE_INTERRUPTS();
enable_interrupts = true;
}
int ret;
int i = 0;
if (i == j) {
ret = 9;
}
ret = i + 8;
if (enable_interrupts) {
ENABLE_INTERRUPTS();
}
return ret;
}
/**
* For ease of implementation it might actually reduce to the following
* which should be equivalent for all intents and purposes
*/
int MANGLED_something_important(int j) {
int i = 0;
if (i == j) {
return 9;
}
return i + 8;
}
int something_important(int j) {
bool enable_interrupts = false;
if (are_interrupts_enabled()) {
DISABLE_INTERRUPTS();
enable_interrupts = true;
}
int ret = MANGLED_something_important(j);
if (enable_interrupts) {
ENABLE_INTERRUPTS();
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment