Created
May 16, 2017 18:33
-
-
Save yabberyabber/16ac208e1ea877c634f060bc9488f5e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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