Created
May 11, 2021 11:51
-
-
Save protocool/3536a021566ad2a5906132ca459dcba6 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
public final class UnfairLockPointer { | |
deinit { | |
pointer.deinitialize(count: 1) | |
pointer.deallocate() | |
} | |
private let pointer: os_unfair_lock_t | |
public init() { | |
// We will directly supply this pointer instead of using | |
// the `&` operator when calling lock/unlock functions. | |
// See: http://www.russbishop.net/the-law | |
pointer = os_unfair_lock_t.allocate(capacity: 1) | |
pointer.initialize(to: os_unfair_lock()) | |
} | |
public func lock() { | |
os_unfair_lock_lock(pointer) | |
} | |
public func tryLock() -> Bool { | |
os_unfair_lock_trylock(pointer) | |
} | |
public func unlock() { | |
os_unfair_lock_unlock(pointer) | |
} | |
public func assertOwned() { | |
os_unfair_lock_assert_owner(pointer) | |
} | |
public func assertNotOwned() { | |
os_unfair_lock_assert_not_owner(pointer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment