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)`
This commit is contained in:
Michael Chiu
2024-11-25 17:41:32 -05:00
parent da813458a6
commit 0c47c45303
6 changed files with 74 additions and 3 deletions

View File

@@ -338,12 +338,12 @@ public var SIG_DFL: sig_t? { return nil }
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) }
#elseif os(OpenBSD)
#elseif os(OpenBSD) || os(FreeBSD)
public var SIG_DFL: sig_t? { return nil }
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
public var SIG_HOLD: sig_t { return unsafeBitCast(3, to: sig_t.self) }
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku)
#elseif os(Linux) || os(PS4) || os(Android) || os(Haiku)
#if !canImport(SwiftMusl)
public typealias sighandler_t = __sighandler_t
#endif
@@ -495,3 +495,7 @@ public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
}
#endif
#endif // SWIFT_STDLIB_HAS_ENVIRON
#if os(FreeBSD)
public let inet_pton = __inet_pton
#endif

View File

@@ -65,6 +65,7 @@ headers = [
'spawn.h',
'strings.h',
'sys/event.h',
'sys/extattr.h',
'sys/file.h',
'sys/inotify.h',
'sys/ioctl.h',
@@ -84,6 +85,7 @@ headers = [
'sys/times.h',
'sys/types.h',
'sys/uio.h',
'sys/umtx.h',
'sys/un.h',
'sys/user.h',
'sys/utsname.h',

View File

@@ -19,7 +19,7 @@
/// It's not named just Glibc so that it doesn't conflict in the event of a
/// future official glibc modulemap.
module SwiftGlibc [system] {
% if CMAKE_SDK in ["LINUX", "OPENBSD"]:
% if CMAKE_SDK in ["LINUX", "OPENBSD", "FREEBSD"]:
link "m"
% end
% if CMAKE_SDK in ["LINUX", "FREEBSD", "OPENBSD", "CYGWIN"]:

View File

@@ -66,4 +66,9 @@ static inline __swift_uint32_t _swift_stdlib_futex_unlock(__swift_uint32_t *lock
#endif // defined(__linux__)
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/umtx.h>
#endif
#endif // SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H

View File

@@ -54,6 +54,13 @@ set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES
Mutex/SpinLoopHint.swift
)
# FreeBSD sources
set(SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES
Mutex/FreeBSDImpl.swift
Mutex/Mutex.swift
)
# Wasm sources
set(SWIFT_SYNCHRONIZATION_WASM_SOURCES
@@ -101,6 +108,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
${SWIFT_SYNCHRONIZATION_WASM_SOURCES}
SWIFT_SOURCES_DEPENDS_WINDOWS
${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES}
SWIFT_SOURCES_DEPENDS_FREEBSD
${SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES}
SWIFT_SOURCES_DEPENDS_FREESTANDING
Mutex/MutexUnavailable.swift
@@ -124,6 +133,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
Android
SWIFT_MODULE_DEPENDS_WINDOWS
WinSDK
SWIFT_MODULE_DEPENDS_FREEBSD
Glibc
SWIFT_COMPILE_FLAGS
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}

View File

@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Glibc
@available(SwiftStdlib 6.0, *)
@frozen
@_staticExclusiveOnly
public struct _MutexHandle: ~Copyable {
@usableFromInline
let value: _Cell<umutex>
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
public init() {
value = _Cell(umutex())
}
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
internal borrowing func _lock() {
_umtx_op(value._address, UMTX_OP_MUTEX_LOCK, 0, nil, nil)
}
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
internal borrowing func _tryLock() -> Bool {
_umtx_op(value._address, UMTX_OP_MUTEX_TRYLOCK, 0, nil, nil) != -1
}
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
internal borrowing func _unlock() {
_umtx_op(value._address, UMTX_OP_MUTEX_UNLOCK, 0, nil, nil)
}
}