Skip to content

Use libpthread primitives for OpenBSD mutexes. #82392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions stdlib/public/Synchronization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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}
Expand Down
58 changes: 58 additions & 0 deletions stdlib/public/Synchronization/Mutex/OpenBSDImpl.swift
Original file line number Diff line number Diff line change
@@ -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<pthread_mutex_t?>

@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)
}
}