Created
December 22, 2023 10:18
-
-
Save pookjw/154e90397fd448af4166d294bebe080b to your computer and use it in GitHub Desktop.
This file contains 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
#import <Foundation/Foundation.h> | |
OBJC_EXPORT id objc_storeWeak(id *location, id newObj); | |
OBJC_EXPORT id objc_loadWeak(id *location); | |
OBJC_EXPORT void objc_destroyWeak(id *location); | |
OBJC_EXPORT void objc_setProperty_atomic(id _Nullable self, SEL _Nonnull _cmd, id _Nullable newValue, ptrdiff_t offset); // objc_setProperty, objc_setProperty_atomic, objc_setProperty_nonatomic, objc_setProperty_nonatomic_copy | |
OBJC_EXPORT id _Nullable objc_getProperty(id _Nullable self, SEL _Nonnull _cmd, ptrdiff_t offset, BOOL atomic); | |
@interface MyObject : NSObject { | |
@private id _weak_property; | |
@private id _atomic_property; | |
} | |
@property (assign, nonatomic) id weak_property; | |
@property (retain, nonatomic) id atomic_property; | |
@end | |
@implementation MyObject | |
- (void)dealloc { | |
objc_destroyWeak(&_weak_property); | |
[_atomic_property release]; | |
[super dealloc]; | |
} | |
- (void)setWeak_property:(id)property { | |
objc_storeWeak(&_weak_property, property); | |
} | |
- (id)weak_property { | |
return objc_loadWeak(&_weak_property); | |
} | |
- (id)atomic_property { | |
return objc_getProperty(self, | |
@selector(atomic_property), | |
reinterpret_cast<uintptr_t>(self) - reinterpret_cast<uintptr_t>(&_atomic_property), | |
YES); | |
} | |
- (void)setAtomic_property:(id)atomic_property { | |
// [_atomic_property release]; | |
// _atomic_property = [atomic_property retain]; | |
objc_setProperty_atomic(self, | |
@selector(setAtomic_property:), | |
atomic_property, | |
reinterpret_cast<uintptr_t>(self) - reinterpret_cast<uintptr_t>(&_atomic_property)); | |
} | |
@end | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
MyObject *object = [MyObject new]; | |
// | |
NSObject *weak_property = [NSObject new]; | |
object.weak_property = weak_property; | |
@autoreleasepool { | |
NSLog(@"%@", object.weak_property); | |
} | |
[weak_property release]; | |
@autoreleasepool { | |
NSLog(@"%@", object.weak_property); | |
} | |
// | |
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); | |
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{ | |
for (int i = 0; i < 10000; i++) { | |
@autoreleasepool { | |
NSObject *atomic_property = [NSObject new]; | |
object.atomic_property = atomic_property; | |
[atomic_property release]; | |
[object atomic_property]; | |
} | |
} | |
dispatch_semaphore_signal(semaphore); | |
}); | |
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{ | |
for (int i = 0; i < 10000; i++) { | |
@autoreleasepool { | |
NSObject *atomic_property = [NSObject new]; | |
object.atomic_property = atomic_property; | |
[atomic_property release]; | |
[object atomic_property]; | |
} | |
} | |
dispatch_semaphore_signal(semaphore); | |
}); | |
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{ | |
@autoreleasepool { | |
NSObject *atomic_property = [NSObject new]; | |
object.atomic_property = atomic_property; | |
[atomic_property release]; | |
[object atomic_property]; | |
} | |
dispatch_semaphore_signal(semaphore); | |
}); | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
dispatch_release(semaphore); | |
[object release]; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment