@@ -36,8 +36,10 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
36
36
/// To keep the implementation of this type as simple as possible,
37
37
/// `pthread_mutex_t` is used on Apple platforms instead of `os_unfair_lock`
38
38
/// or `OSAllocatedUnfairLock`.
39
- #if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os( Android) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
39
+ #if SWT_TARGET_OS_APPLE || os(Linux) || os(Android) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
40
40
private typealias _Lock = pthread_mutex_t
41
+ #elseif os(FreeBSD)
42
+ private typealias _Lock = pthread_mutex_t ?
41
43
#elseif os(Windows)
42
44
private typealias _Lock = SRWLOCK
43
45
#elseif os(WASI)
@@ -121,7 +123,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
121
123
}
122
124
}
123
125
124
- #if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os( Android) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
126
+ #if SWT_TARGET_OS_APPLE || os(Linux) || os(Android) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
125
127
/// Acquire the lock and invoke a function while it is held, yielding both the
126
128
/// protected value and a reference to the lock itself.
127
129
///
@@ -149,6 +151,34 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
149
151
}
150
152
}
151
153
}
154
+ #elseif os(FreeBSD)
155
+ /// Acquire the lock and invoke a function while it is held, yielding both the
156
+ /// protected value and a reference to the lock itself.
157
+ ///
158
+ /// - Parameters:
159
+ /// - body: A closure to invoke while the lock is held.
160
+ ///
161
+ /// - Returns: Whatever is returned by `body`.
162
+ ///
163
+ /// - Throws: Whatever is thrown by `body`.
164
+ ///
165
+ /// This function is equivalent to ``withLock(_:)`` except that the closure
166
+ /// passed to it also takes a reference to the underlying platform lock. This
167
+ /// function can be used when platform-specific functionality such as a
168
+ /// `pthread_cond_t` is needed. Because the caller has direct access to the
169
+ /// lock and is able to unlock and re-lock it, it is unsafe to modify the
170
+ /// protected value.
171
+ ///
172
+ /// - Warning: Callers that unlock the lock _must_ lock it again before the
173
+ /// closure returns. If the lock is not acquired when `body` returns, the
174
+ /// effect is undefined.
175
+ nonmutating func withUnsafeUnderlyingLock< R> ( _ body: ( UnsafeMutablePointer < pthread_mutex_t ? > , T ) throws -> R ) rethrows -> R {
176
+ try withLock { value in
177
+ try _storage. withUnsafeMutablePointerToElements { lock in
178
+ try body ( lock, value)
179
+ }
180
+ }
181
+ }
152
182
#endif
153
183
}
154
184
0 commit comments