|
1 | 1 | import Foundation |
2 | 2 |
|
3 | | -#if compiler(>=6) |
4 | | - /// A synchronization primitive that protects shared mutable state via mutual exclusion. |
5 | | - /// |
6 | | - /// A back-port of Swift's `Mutex` type for wider platform availability. |
7 | | - #if hasFeature(StaticExclusiveOnly) |
8 | | - @_staticExclusiveOnly |
9 | | - #endif |
10 | | - package struct Mutex<Value: ~Copyable>: ~Copyable { |
11 | | - private let _lock = NSLock() |
12 | | - private let _box: Box |
| 3 | +package struct Mutex<Value> { |
| 4 | + private let _lock = NSLock() |
| 5 | + private let _box: Box |
13 | 6 |
|
14 | | - /// Initializes a value of this mutex with the given initial state. |
15 | | - /// |
16 | | - /// - Parameter initialValue: The initial value to give to the mutex. |
17 | | - package init(_ initialValue: consuming sending Value) { |
18 | | - _box = Box(initialValue) |
19 | | - } |
20 | | - |
21 | | - private final class Box { |
22 | | - var value: Value |
23 | | - init(_ initialValue: consuming sending Value) { |
24 | | - value = initialValue |
25 | | - } |
26 | | - } |
| 7 | + package init(_ initialValue: consuming Value) { |
| 8 | + _box = Box(initialValue) |
27 | 9 | } |
28 | 10 |
|
29 | | - extension Mutex: @unchecked Sendable where Value: ~Copyable {} |
30 | | - |
31 | | - extension Mutex where Value: ~Copyable { |
32 | | - /// Calls the given closure after acquiring the lock and then releases ownership. |
33 | | - borrowing package func withLock<Result: ~Copyable, E: Error>( |
34 | | - _ body: (inout sending Value) throws(E) -> sending Result |
35 | | - ) throws(E) -> sending Result { |
36 | | - _lock.lock() |
37 | | - defer { _lock.unlock() } |
38 | | - return try body(&_box.value) |
39 | | - } |
40 | | - |
41 | | - /// Attempts to acquire the lock and then calls the given closure if successful. |
42 | | - borrowing package func withLockIfAvailable<Result: ~Copyable, E: Error>( |
43 | | - _ body: (inout sending Value) throws(E) -> sending Result |
44 | | - ) throws(E) -> sending Result? { |
45 | | - guard _lock.try() else { return nil } |
46 | | - defer { _lock.unlock() } |
47 | | - return try body(&_box.value) |
| 11 | + private final class Box { |
| 12 | + var value: Value |
| 13 | + init(_ initialValue: consuming Value) { |
| 14 | + value = initialValue |
48 | 15 | } |
49 | 16 | } |
50 | | -#else |
51 | | - package struct Mutex<Value> { |
52 | | - private let _lock = NSLock() |
53 | | - private let _box: Box |
| 17 | +} |
54 | 18 |
|
55 | | - package init(_ initialValue: consuming Value) { |
56 | | - _box = Box(initialValue) |
57 | | - } |
| 19 | +extension Mutex: @unchecked Sendable {} |
58 | 20 |
|
59 | | - private final class Box { |
60 | | - var value: Value |
61 | | - init(_ initialValue: consuming Value) { |
62 | | - value = initialValue |
63 | | - } |
64 | | - } |
| 21 | +extension Mutex { |
| 22 | + borrowing package func withLock<Result>( |
| 23 | + _ body: (inout Value) throws -> Result |
| 24 | + ) rethrows -> Result { |
| 25 | + _lock.lock() |
| 26 | + defer { _lock.unlock() } |
| 27 | + return try body(&_box.value) |
65 | 28 | } |
66 | 29 |
|
67 | | - extension Mutex: @unchecked Sendable {} |
68 | | - |
69 | | - extension Mutex { |
70 | | - borrowing package func withLock<Result>( |
71 | | - _ body: (inout Value) throws -> Result |
72 | | - ) rethrows -> Result { |
73 | | - _lock.lock() |
74 | | - defer { _lock.unlock() } |
75 | | - return try body(&_box.value) |
76 | | - } |
77 | | - |
78 | | - borrowing package func withLockIfAvailable<Result>( |
79 | | - _ body: (inout Value) throws -> Result |
80 | | - ) rethrows -> Result? { |
81 | | - guard _lock.try() else { return nil } |
82 | | - defer { _lock.unlock() } |
83 | | - return try body(&_box.value) |
84 | | - } |
| 30 | + borrowing package func withLockIfAvailable<Result>( |
| 31 | + _ body: (inout Value) throws -> Result |
| 32 | + ) rethrows -> Result? { |
| 33 | + guard _lock.try() else { return nil } |
| 34 | + defer { _lock.unlock() } |
| 35 | + return try body(&_box.value) |
85 | 36 | } |
86 | | -#endif |
| 37 | +} |
87 | 38 |
|
88 | 39 | extension Mutex where Value == Void { |
89 | 40 | borrowing package func _unsafeLock() { |
|
0 commit comments