@@ -36,8 +36,10 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
3636 /// To keep the implementation of this type as simple as possible,
3737 /// `pthread_mutex_t` is used on Apple platforms instead of `os_unfair_lock`
3838 /// 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))
4040 private typealias _Lock = pthread_mutex_t
41+ #elseif os(FreeBSD)
42+ private typealias _Lock = pthread_mutex_t ?
4143#elseif os(Windows)
4244 private typealias _Lock = SRWLOCK
4345#elseif os(WASI)
@@ -121,7 +123,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
121123 }
122124 }
123125
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))
125127 /// Acquire the lock and invoke a function while it is held, yielding both the
126128 /// protected value and a reference to the lock itself.
127129 ///
@@ -149,6 +151,34 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
149151 }
150152 }
151153 }
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+ }
152182#endif
153183}
154184
0 commit comments