Skip to content

Instantly share code, notes, and snippets.

@Kentzo
Last active January 26, 2020 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kentzo/d55a3ad458ff5965854685e9e0911808 to your computer and use it in GitHub Desktop.
Save Kentzo/d55a3ad458ff5965854685e9e0911808 to your computer and use it in GitHub Desktop.
void _NoReentryScopeEnter(BOOL *aVar)
{
if (*aVar)
[NSException raise:NSInternalInconsistencyException format:@"the method is not reentrable"];
*aVar = YES;
}
void _NoReentryScopeLeave(BOOL **aVar)
{
**aVar = NO;
}
#define _NoReentryScope(var, aVarPtr) \
__attribute__((__cleanup__(_NoReentryScopeLeave))) BOOL *var = aVarPtr; \
_NoReentryScopeEnter(var);
#define NoReentryScope(aVarPtr) _NoReentryScope(OS_CONCAT(NoReentryScope, __COUNTER__), aVarPtr)
@implementation MyObject
{
BOOL _canary;
}
- (void)stateMutation
{
NoReentryScope(&_canary);
// Mutate State
}
- (void)stateMutationWithKVO
{
{
NoReentryScope(&_canary);
[self willChangeValueForKey:@"..."]; // calling -stateMutation / stateMutationWithKVO is an error
}
// Mutate the state
{
NoReentryScope(&_canary);
[self didChangeValueForKey:@"..."]; // calling -stateMutation / stateMutationWithKVO is an error
}
// Continue State Mutation
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment