-
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
375 additions
and
302 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
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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
import Foundation | ||
|
||
/// A Mutex protects a value with an NSLock. | ||
/// | ||
/// We'll replace it with the SE-0433 Mutex when it is available. | ||
/// <https://github.com/apple/swift-evolution/blob/main/proposals/0433-mutex.md> | ||
final class Mutex<T> { | ||
private var _value: T | ||
private var lock = NSLock() | ||
|
||
init(_ value: T) { | ||
_value = value | ||
} | ||
|
||
/// Runs the provided closure while holding a lock on the value. | ||
/// | ||
/// - parameter body: A closure that can modify the value. | ||
func withLock<U>(_ body: (inout T) throws -> U) rethrows -> U { | ||
lock.lock() | ||
defer { lock.unlock() } | ||
return try body(&_value) | ||
} | ||
} | ||
|
||
// Inspired by <https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/58> | ||
extension Mutex { | ||
func load() -> T { | ||
withLock { $0 } | ||
} | ||
|
||
func store(_ value: T) { | ||
withLock { $0 = value } | ||
} | ||
} | ||
|
||
extension Mutex where T: Numeric { | ||
@discardableResult | ||
func increment() -> T { | ||
withLock { n in | ||
n += 1 | ||
return n | ||
} | ||
} | ||
|
||
@discardableResult | ||
func decrement() -> T { | ||
withLock { n in | ||
n -= 1 | ||
return n | ||
} | ||
} | ||
} | ||
|
||
extension Mutex: @unchecked Sendable where T: Sendable { } |
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
Oops, something went wrong.