-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Seweryn Plażuk <[email protected]>
- Loading branch information
1 parent
1b2d25b
commit 73f806e
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
|
||
final class UnfairLock { | ||
private let unfairLock: os_unfair_lock_t | ||
|
||
init() { | ||
unfairLock = .allocate(capacity: 1) | ||
unfairLock.initialize(to: os_unfair_lock()) | ||
} | ||
|
||
deinit { | ||
unfairLock.deinitialize(count: 1) | ||
unfairLock.deallocate() | ||
} | ||
|
||
func around<T>(_ block: () -> T) -> T { | ||
lock() | ||
defer { unlock() } | ||
return block() | ||
} | ||
|
||
private func lock() { | ||
os_unfair_lock_lock(unfairLock) | ||
} | ||
|
||
private func unlock() { | ||
os_unfair_lock_unlock(unfairLock) | ||
} | ||
} |