-
Notifications
You must be signed in to change notification settings - Fork 88
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 577b925
Showing
3 changed files
with
60 additions
and
3 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 os.lock | ||
|
||
final class UnfairLock { | ||
private let lockPointer: os_unfair_lock_t | ||
|
||
init() { | ||
self.lockPointer = .allocate(capacity: 1) | ||
self.lockPointer.initialize(to: os_unfair_lock()) | ||
} | ||
|
||
deinit { | ||
self.lockPointer.deinitialize(count: 1) | ||
self.lockPointer.deallocate() | ||
} | ||
|
||
func synchronized<T>(_ block: () -> T) -> T { | ||
self.lock() | ||
defer { self.unlock() } | ||
return block() | ||
} | ||
|
||
private func lock() { | ||
os_unfair_lock_lock(self.lockPointer) | ||
} | ||
|
||
private func unlock() { | ||
os_unfair_lock_unlock(self.lockPointer) | ||
} | ||
} |