|
29 | 29 | // SOFTWARE. |
30 | 30 | // |
31 | 31 |
|
32 | | -#if compiler(>=6) |
| 32 | +#if canImport(Darwin) |
33 | 33 |
|
34 | | -#if !canImport(WinSDK) |
| 34 | +import struct os.os_unfair_lock_t |
| 35 | +import struct os.os_unfair_lock |
| 36 | +import func os.os_unfair_lock_lock |
| 37 | +import func os.os_unfair_lock_unlock |
| 38 | +import func os.os_unfair_lock_trylock |
35 | 39 |
|
36 | 40 | // Backports the Swift 6 type Mutex<Value> to all Darwin platforms |
37 | 41 | struct Mutex<Value: ~Copyable>: ~Copyable { |
@@ -60,44 +64,6 @@ struct Mutex<Value: ~Copyable>: ~Copyable { |
60 | 64 |
|
61 | 65 | extension Mutex: @unchecked Sendable where Value: ~Copyable { } |
62 | 66 |
|
63 | | -#else |
64 | | - |
65 | | -// Windows doesn't support ~Copyable yet |
66 | | - |
67 | | -struct Mutex<Value>: @unchecked Sendable { |
68 | | - let storage: Storage<Value> |
69 | | - |
70 | | - init(_ initialValue: consuming sending Value) { |
71 | | - self.storage = Storage(initialValue) |
72 | | - } |
73 | | - |
74 | | - borrowing func withLock<Result, E: Error>( |
75 | | - _ body: (inout sending Value) throws(E) -> sending Result |
76 | | - ) throws(E) -> sending Result { |
77 | | - storage.lock() |
78 | | - defer { storage.unlock() } |
79 | | - return try body(&storage.value) |
80 | | - } |
81 | | - |
82 | | - borrowing func withLockIfAvailable<Result, E: Error>( |
83 | | - _ body: (inout sending Value) throws(E) -> sending Result |
84 | | - ) throws(E) -> sending Result? { |
85 | | - guard storage.tryLock() else { return nil } |
86 | | - defer { storage.unlock() } |
87 | | - return try body(&storage.value) |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -#endif |
92 | | - |
93 | | -#if canImport(Darwin) |
94 | | - |
95 | | -import struct os.os_unfair_lock_t |
96 | | -import struct os.os_unfair_lock |
97 | | -import func os.os_unfair_lock_lock |
98 | | -import func os.os_unfair_lock_unlock |
99 | | -import func os.os_unfair_lock_trylock |
100 | | - |
101 | 67 | final class Storage<Value: ~Copyable> { |
102 | 68 | private let _lock: os_unfair_lock_t |
103 | 69 | var value: Value |
@@ -126,80 +92,4 @@ final class Storage<Value: ~Copyable> { |
126 | 92 | } |
127 | 93 | } |
128 | 94 |
|
129 | | -#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) |
130 | | - |
131 | | -#if canImport(Musl) |
132 | | -import Musl |
133 | | -#elseif canImport(Bionic) |
134 | | -import Android |
135 | | -#else |
136 | | -import Glibc |
137 | | -#endif |
138 | | - |
139 | | -final class Storage<Value: ~Copyable> { |
140 | | - private let _lock: UnsafeMutablePointer<pthread_mutex_t> |
141 | | - |
142 | | - var value: Value |
143 | | - |
144 | | - init(_ initialValue: consuming Value) { |
145 | | - var attr = pthread_mutexattr_t() |
146 | | - pthread_mutexattr_init(&attr) |
147 | | - self._lock = .allocate(capacity: 1) |
148 | | - let err = pthread_mutex_init(self._lock, &attr) |
149 | | - precondition(err == 0, "pthread_mutex_init error: \(err)") |
150 | | - self.value = initialValue |
151 | | - } |
152 | | - |
153 | | - func lock() { |
154 | | - let err = pthread_mutex_lock(_lock) |
155 | | - precondition(err == 0, "pthread_mutex_lock error: \(err)") |
156 | | - } |
157 | | - |
158 | | - func unlock() { |
159 | | - let err = pthread_mutex_unlock(_lock) |
160 | | - precondition(err == 0, "pthread_mutex_unlock error: \(err)") |
161 | | - } |
162 | | - |
163 | | - func tryLock() -> Bool { |
164 | | - pthread_mutex_trylock(_lock) == 0 |
165 | | - } |
166 | | - |
167 | | - deinit { |
168 | | - let err = pthread_mutex_destroy(self._lock) |
169 | | - precondition(err == 0, "pthread_mutex_destroy error: \(err)") |
170 | | - self._lock.deallocate() |
171 | | - } |
172 | | -} |
173 | | - |
174 | | -#elseif canImport(WinSDK) |
175 | | - |
176 | | -import ucrt |
177 | | -import WinSDK |
178 | | - |
179 | | -final class Storage<Value> { |
180 | | - private let _lock: UnsafeMutablePointer<SRWLOCK> |
181 | | - |
182 | | - var value: Value |
183 | | - |
184 | | - init(_ initialValue: Value) { |
185 | | - self._lock = .allocate(capacity: 1) |
186 | | - InitializeSRWLock(self._lock) |
187 | | - self.value = initialValue |
188 | | - } |
189 | | - |
190 | | - func lock() { |
191 | | - AcquireSRWLockExclusive(_lock) |
192 | | - } |
193 | | - |
194 | | - func unlock() { |
195 | | - ReleaseSRWLockExclusive(_lock) |
196 | | - } |
197 | | - |
198 | | - func tryLock() -> Bool { |
199 | | - TryAcquireSRWLockExclusive(_lock) != 0 |
200 | | - } |
201 | | -} |
202 | | - |
203 | | -#endif |
204 | | - |
205 | 95 | #endif |
0 commit comments