From 7562311d9e90b036e7b19cf5a5ad2399de0d0a24 Mon Sep 17 00:00:00 2001 From: "Maxim V. Potapov" Date: Fri, 22 May 2026 22:57:17 +0200 Subject: [PATCH] feat: add FreeBSD pthread support to NIOLock ### Motivation: NIOLock's pthread fallback branch types LockPrimitive as pthread_mutex_t and initializes pthread_mutexattr_t with a default initializer; both compile on Linux but fail on FreeBSD/OpenBSD where those types are opaque-pointer typedefs imported as Optional. Additionally, PTHREAD_MUTEX_ERRORCHECK is imported as a pthread_mutextype enum case on FreeBSD whose .rawValue must be used to construct the CInt that pthread_mutexattr_settype expects. ### Modifications: Mirror the pattern landed in apple/swift-log#387: - alias LockPrimitive to pthread_mutex_t? on FreeBSD/OpenBSD - initialize pthread_mutexattr_t via the bitPattern overload - on FreeBSD/OpenBSD, access PTHREAD_MUTEX_ERRORCHECK via .rawValue before passing to pthread_mutexattr_settype ### Result: swift-prometheus compiles on FreeBSD with the official Swift FreeBSD preview toolchain. --- Sources/Prometheus/NIOLock.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sources/Prometheus/NIOLock.swift b/Sources/Prometheus/NIOLock.swift index 69bb58f..3064360 100644 --- a/Sources/Prometheus/NIOLock.swift +++ b/Sources/Prometheus/NIOLock.swift @@ -48,6 +48,9 @@ import wasi_pthread #if os(Windows) @usableFromInline typealias LockPrimitive = SRWLOCK +#elseif os(FreeBSD) || os(OpenBSD) +@usableFromInline +typealias LockPrimitive = pthread_mutex_t? #else @usableFromInline typealias LockPrimitive = pthread_mutex_t @@ -64,11 +67,19 @@ extension LockOperations { #if os(Windows) InitializeSRWLock(mutex) #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded)) + #if os(FreeBSD) || os(OpenBSD) + var attr = pthread_mutexattr_t(bitPattern: 0) + #else var attr = pthread_mutexattr_t() + #endif pthread_mutexattr_init(&attr) assert( { + #if os(FreeBSD) || os(OpenBSD) + pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK.rawValue)) + #else pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) + #endif return true }() )