diff --git a/stdlib/public/Synchronization/CMakeLists.txt b/stdlib/public/Synchronization/CMakeLists.txt index df593d4933e7d..357aef5546974 100644 --- a/stdlib/public/Synchronization/CMakeLists.txt +++ b/stdlib/public/Synchronization/CMakeLists.txt @@ -80,6 +80,13 @@ set(SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES Mutex/WindowsImpl.swift ) +# OpenBSD sources + +set(SWIFT_SYNCHRONIZATION_OPENBSD_SOURCES + Mutex/Mutex.swift + Mutex/OpenBSDImpl.swift +) + set(SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} "-enable-builtin-module" @@ -115,6 +122,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES ${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES} SWIFT_SOURCES_DEPENDS_FREEBSD ${SWIFT_SYNCHRONIZATION_FREEBSD_SOURCES} + SWIFT_SOURCES_DEPENDS_OPENBSD + ${SWIFT_SYNCHRONIZATION_OPENBSD_SOURCES} SWIFT_SOURCES_DEPENDS_FREESTANDING Mutex/MutexUnavailable.swift @@ -140,6 +149,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES WinSDK SWIFT_MODULE_DEPENDS_FREEBSD Glibc + SWIFT_MODULE_DEPENDS_OPENBSD + Glibc SWIFT_COMPILE_FLAGS ${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS} diff --git a/stdlib/public/Synchronization/Mutex/OpenBSDImpl.swift b/stdlib/public/Synchronization/Mutex/OpenBSDImpl.swift new file mode 100644 index 0000000000000..ad4de24cf28b6 --- /dev/null +++ b/stdlib/public/Synchronization/Mutex/OpenBSDImpl.swift @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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 + + @available(SwiftStdlib 6.0, *) + @_alwaysEmitIntoClient + @_transparent + public init() { + var mx = pthread_mutex_t(bitPattern: 0) + pthread_mutex_init(&mx, nil) + value = _Cell(mx) + } + + @available(SwiftStdlib 6.0, *) + @_alwaysEmitIntoClient + @_transparent + internal borrowing func _lock() { + pthread_mutex_lock(value._address) + } + + @available(SwiftStdlib 6.0, *) + @_alwaysEmitIntoClient + @_transparent + internal borrowing func _tryLock() -> Bool { + pthread_mutex_trylock(value._address) != 0 + } + + @available(SwiftStdlib 6.0, *) + @_alwaysEmitIntoClient + @_transparent + internal borrowing func _unlock() { + pthread_mutex_unlock(value._address) + } + + @available(SwiftStdlib 6.0, *) + @_alwaysEmitIntoClient + @_transparent + deinit { + pthread_mutex_destroy(value._address) + } +}