Created
October 5, 2016 18:27
-
-
Save misbell/2dc38c47ff56f91c6775dde2be1fceb4 to your computer and use it in GitHub Desktop.
os_unfair_lock, simple useage from wwdc 2016 concurrency talk by Pierre Habouzit, objective-c, bridging header and Swift 3
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
#import "FoundationLocks.h" | |
#import "os/lock.h" | |
@implementation LockableObject | |
os_unfair_lock _lock; | |
- (instancetype) init { | |
(os_unfair_lock){0}; | |
return self; | |
} | |
- (void) lock { | |
os_unfair_lock_lock(&_lock); | |
} | |
- (void) unlock { | |
os_unfair_lock_unlock(&_lock); | |
} | |
// and header | |
#import <Foundation/Foundation.h> | |
@interface LockableObject : NSObject | |
- (void) lock; | |
- (void) unlock; | |
@end | |
// and bridging header | |
// | |
// Use this file to import your target's public headers that you would like to expose to Swift. | |
// | |
#import "FoundationLocks.h" | |
// and Swift | |
let l = LockableObject() | |
let _ = l.lock() | |
let _ = l.unlock() |
MadCoder
commented
Oct 5, 2016
excellent, thank you!
here's the trylock
-
(Boolean) trylock {
if (os_unfair_lock_trylock(&_lock)) {
return true;
}
return false;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment