Skip to content

Commit 21d230e

Browse files
committed
Add support for FreeBSD
1 parent dea0abb commit 21d230e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Sources/Logging/Locks.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ internal final class Lock: @unchecked Sendable {
5454
#elseif os(Windows)
5555
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
5656
UnsafeMutablePointer.allocate(capacity: 1)
57+
#elseif os(FreeBSD) || os(OpenBSD)
58+
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t?> =
59+
UnsafeMutablePointer.allocate(capacity: 1)
5760
#else
5861
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t> =
5962
UnsafeMutablePointer.allocate(capacity: 1)
@@ -66,9 +69,17 @@ internal final class Lock: @unchecked Sendable {
6669
#elseif os(Windows)
6770
InitializeSRWLock(self.mutex)
6871
#else
72+
#if os(FreeBSD) || os(OpenBSD)
73+
var attr = pthread_mutexattr_t(bitPattern: 0)
74+
#else
6975
var attr = pthread_mutexattr_t()
76+
#endif
7077
pthread_mutexattr_init(&attr)
78+
#if os(FreeBSD) || os(OpenBSD)
79+
pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK.rawValue))
80+
#else
7181
pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK))
82+
#endif
7283

7384
let err = pthread_mutex_init(self.mutex, &attr)
7485
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
@@ -157,6 +168,9 @@ internal final class ReadWriteLock: @unchecked Sendable {
157168
fileprivate let rwlock: UnsafeMutablePointer<SRWLOCK> =
158169
UnsafeMutablePointer.allocate(capacity: 1)
159170
fileprivate var shared: Bool = true
171+
#elseif os(FreeBSD) || os(OpenBSD)
172+
fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t?> =
173+
UnsafeMutablePointer.allocate(capacity: 1)
160174
#else
161175
fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t> =
162176
UnsafeMutablePointer.allocate(capacity: 1)

Sources/Logging/Logging.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,11 @@ internal struct StdioOutputStream: TextOutputStream, @unchecked Sendable {
13551355
#elseif os(Windows)
13561356
let systemStderr = CRT.stderr
13571357
#elseif canImport(Glibc)
1358+
#if os(FreeBSD) || os(OpenBSD)
1359+
let systemStderr = Glibc.stderr
1360+
#else
13581361
let systemStderr = Glibc.stderr!
1362+
#endif
13591363
#elseif canImport(Android)
13601364
let systemStderr = Android.stderr
13611365
#elseif canImport(Musl)
@@ -1375,7 +1379,11 @@ internal struct StdioOutputStream: TextOutputStream, @unchecked Sendable {
13751379
#elseif os(Windows)
13761380
let systemStdout = CRT.stdout
13771381
#elseif canImport(Glibc)
1382+
#if os(FreeBSD) || os(OpenBSD)
1383+
let systemStdout = Glibc.stdout
1384+
#else
13781385
let systemStdout = Glibc.stdout!
1386+
#endif
13791387
#elseif canImport(Android)
13801388
let systemStdout = Android.stdout
13811389
#elseif canImport(Musl)

Tests/LoggingTests/TestLogger.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ public class MDC {
375375
return Int(pthread_mach_thread_np(pthread_self()))
376376
#elseif os(Windows)
377377
return Int(GetCurrentThreadId())
378+
#elseif os(FreeBSD) || os(OpenBSD)
379+
return Int(pthread_getthreadid_np())
378380
#else
379381
return Int(pthread_self())
380382
#endif

0 commit comments

Comments
 (0)