Last active
January 26, 2020 02:56
-
-
Save Kentzo/d55a3ad458ff5965854685e9e0911808 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
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) |
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
@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