Skip to content

Instantly share code, notes, and snippets.

@CodaFi
Created February 8, 2014 20:28
Show Gist options
  • Save CodaFi/8889704 to your computer and use it in GitHub Desktop.
Save CodaFi/8889704 to your computer and use it in GitHub Desktop.
A macro for automatically locking and unlocking critical sections of code with OSSpinLocks.
/**
* Usage:
*
* static OSSpinlock lock;
*
* @critical_section(lock, ^{
* //Synchronized Section
* });
*
*/
static inline void __nui_cleanup(volatile OSSpinLock **lock) {
OSSpinLockUnlock(*lock);
}
#ifndef critical_section
#define critical_section(LOCK, BLOCK) \
autoreleasepool {} \
do {\
volatile OSSpinLock *__nui_local_lock __attribute__ ((__cleanup__(__nui_cleanup))) = &LOCK; \
OSSpinLockLock(__nui_local_lock); \
BLOCK(); \
} while (0)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment