Skip to content

Commit a5ade00

Browse files
committed
Adding FreeBSD support
This commit adds required conditional compilation blocks to enable bulding on FreeBSD (tested on x86_64 FreeBSD 14.1-RELEASE-p6). Also implements FreeBSD synchronization shims using `_umtx_op(2)`
1 parent 6221b29 commit a5ade00

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed

stdlib/public/Platform/Platform.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ public var SIG_DFL: sig_t? { return nil }
338338
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
339339
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
340340
public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) }
341-
#elseif os(OpenBSD)
341+
#elseif os(OpenBSD) || os(FreeBSD)
342342
public var SIG_DFL: sig_t? { return nil }
343343
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
344344
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
345345
public var SIG_HOLD: sig_t { return unsafeBitCast(3, to: sig_t.self) }
346-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku)
346+
#elseif os(Linux) || os(PS4) || os(Android) || os(Haiku)
347347
#if !canImport(SwiftMusl)
348348
public typealias sighandler_t = __sighandler_t
349349
#endif
@@ -495,3 +495,7 @@ public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
495495
}
496496
#endif
497497
#endif // SWIFT_STDLIB_HAS_ENVIRON
498+
499+
#if os(FreeBSD)
500+
public let inet_pton = __inet_pton
501+
#endif

stdlib/public/Platform/SwiftGlibc.h.gyb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ headers = [
6565
'spawn.h',
6666
'strings.h',
6767
'sys/event.h',
68+
'sys/extattr.h',
6869
'sys/file.h',
6970
'sys/inotify.h',
7071
'sys/ioctl.h',
@@ -84,6 +85,7 @@ headers = [
8485
'sys/times.h',
8586
'sys/types.h',
8687
'sys/uio.h',
88+
'sys/umtx.h',
8789
'sys/un.h',
8890
'sys/user.h',
8991
'sys/utsname.h',

stdlib/public/Platform/glibc.modulemap.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/// It's not named just Glibc so that it doesn't conflict in the event of a
2020
/// future official glibc modulemap.
2121
module SwiftGlibc [system] {
22-
% if CMAKE_SDK in ["LINUX", "OPENBSD"]:
22+
% if CMAKE_SDK in ["LINUX", "OPENBSD", "FREEBSD"]:
2323
link "m"
2424
% end
2525
% if CMAKE_SDK in ["LINUX", "FREEBSD", "OPENBSD", "CYGWIN"]:

stdlib/public/SwiftShims/swift/shims/_SynchronizationShims.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ static inline __swift_uint32_t _swift_stdlib_futex_unlock(__swift_uint32_t *lock
6666

6767
#endif // defined(__linux__)
6868

69+
#if defined(__FreeBSD__)
70+
#include <sys/types.h>
71+
#include <sys/umtx.h>
72+
#endif
73+
6974
#endif // SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES
5454
Mutex/SpinLoopHint.swift
5555
)
5656

57+
# FreeBSD sources
58+
59+
set(SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES
60+
Mutex/FreeBSDImpl.swift
61+
Mutex/Mutex.swift
62+
)
63+
5764
# Wasm sources
5865

5966
set(SWIFT_SYNCHRONIZATION_WASM_SOURCES
@@ -100,6 +107,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
100107
${SWIFT_SYNCHRONIZATION_WASM_SOURCES}
101108
SWIFT_SOURCES_DEPENDS_WINDOWS
102109
${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES}
110+
SWIFT_SOURCES_DEPENDS_FREEBSD
111+
${SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES}
103112
SWIFT_SOURCES_DEPENDS_FREESTANDING
104113
Mutex/MutexUnavailable.swift
105114

@@ -123,6 +132,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
123132
Android
124133
SWIFT_MODULE_DEPENDS_WINDOWS
125134
WinSDK
135+
SWIFT_MODULE_DEPENDS_FREEBSD
136+
Glibc
126137

127138
SWIFT_COMPILE_FLAGS
128139
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Atomics open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Glibc
14+
15+
@available(SwiftStdlib 6.0, *)
16+
@frozen
17+
@_staticExclusiveOnly
18+
public struct _MutexHandle: ~Copyable {
19+
@usableFromInline
20+
let value: _Cell<umutex>
21+
22+
@available(SwiftStdlib 6.0, *)
23+
@_alwaysEmitIntoClient
24+
@_transparent
25+
public init() {
26+
value = _Cell(umutex())
27+
}
28+
29+
@available(SwiftStdlib 6.0, *)
30+
@_alwaysEmitIntoClient
31+
@_transparent
32+
internal borrowing func _lock() {
33+
_umtx_op(value._address, UMTX_OP_MUTEX_LOCK, 0, nil, nil)
34+
}
35+
36+
@available(SwiftStdlib 6.0, *)
37+
@_alwaysEmitIntoClient
38+
@_transparent
39+
internal borrowing func _tryLock() -> Bool {
40+
_umtx_op(value._address, UMTX_OP_MUTEX_TRYLOCK, 0, nil, nil) != -1
41+
}
42+
43+
@available(SwiftStdlib 6.0, *)
44+
@_alwaysEmitIntoClient
45+
@_transparent
46+
internal borrowing func _unlock() {
47+
_umtx_op(value._address, UMTX_OP_MUTEX_UNLOCK, 0, nil, nil)
48+
}
49+
}

0 commit comments

Comments
 (0)