Skip to content

Commit 6753c35

Browse files
committed
add FreeBSD support
1 parent 1df9545 commit 6753c35

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

Sources/Testing/ExitTests/ExitTest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extension ExitTest {
114114
// As with Linux, disable the generation core files. FreeBSD does not, as
115115
// far as I can tell, special-case RLIMIT_CORE=1.
116116
var rl = rlimit(rlim_cur: 0, rlim_max: 0)
117-
_ = setrlimit(CInt(RLIMIT_CORE.rawValue), &rl)
117+
_ = setrlimit(RLIMIT_CORE, &rl)
118118
#elseif os(Windows)
119119
// On Windows, similarly disable Windows Error Reporting and the Windows
120120
// Error Reporting UI. Note we expect to be the first component to call

Sources/Testing/ExitTests/SpawnProcess.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,19 @@ func spawnExecutable(
143143
#if SWT_TARGET_OS_APPLE
144144
// Close all other file descriptors open in the parent.
145145
flags |= CShort(POSIX_SPAWN_CLOEXEC_DEFAULT)
146-
#elseif os(Linux) || os(FreeBSD)
146+
#elseif os(Linux)
147147
// This platform doesn't have POSIX_SPAWN_CLOEXEC_DEFAULT, but we can at
148148
// least close all file descriptors higher than the highest inherited one.
149149
// We are assuming here that the caller didn't set FD_CLOEXEC on any of
150150
// these file descriptors.
151151
_ = swt_posix_spawn_file_actions_addclosefrom_np(fileActions, highestFD + 1)
152+
#elseif os(FreeBSD)
153+
// Like Linux, this platfrom doesn't have POSIX_SPAWN_CLOEXEC_DEFAULT;
154+
// However; unlike Linux, all non-EOL FreeBSD (>= 13.1) supports
155+
// `posix_spawn_file_actions_addclosefrom_np` and therefore we don't need
156+
// need `swt_posix_spawn_file_actions_addclosefrom_np` to guard the availability
157+
// of this api.
158+
_ = posix_spawn_file_actions_addclosefrom_np(fileActions, highestFD + 1)
152159
#else
153160
#warning("Platform-specific implementation missing: cannot close unused file descriptors")
154161
#endif

Sources/Testing/ExitTests/WaitFor.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ private let _childProcessContinuations = Locked<[pid_t: CheckedContinuation<Exit
8585
/// A condition variable used to suspend the waiter thread created by
8686
/// `_createWaitThread()` when there are no child processes to await.
8787
private nonisolated(unsafe) let _waitThreadNoChildrenCondition = {
88+
#if os(FreeBSD)
89+
let result = UnsafeMutablePointer<pthread_cond_t?>.allocate(capacity: 1)
90+
#else
8891
let result = UnsafeMutablePointer<pthread_cond_t>.allocate(capacity: 1)
92+
#endif
8993
_ = pthread_cond_init(result, nil)
9094
return result
9195
}()
@@ -132,7 +136,7 @@ private let _createWaitThread: Void = {
132136

133137
// Create the thread. It will run immediately; because it runs in an infinite
134138
// loop, we aren't worried about detaching or joining it.
135-
#if SWT_TARGET_OS_APPLE
139+
#if SWT_TARGET_OS_APPLE || os(FreeBSD)
136140
var thread: pthread_t?
137141
#else
138142
var thread = pthread_t()

Sources/Testing/Support/Locked.swift

+32-2
Original file line numberDiff line numberDiff line change
@@ -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

Sources/_TestingInternals/include/Includes.h

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
#include <pthread.h>
8181
#endif
8282

83+
#if __has_include(<pthread_np.h>)
84+
#include <pthread_np.h>
85+
#endif
86+
8387
#if __has_include(<pty.h>)
8488
#include <pty.h>
8589
#endif

0 commit comments

Comments
 (0)