Skip to content

Add Musl import, error if unrecognised platform #325

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

Merged
merged 2 commits into from
Aug 6, 2024
Merged
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
22 changes: 16 additions & 6 deletions Sources/AsyncAlgorithms/Locking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(WinSDK)
import WinSDK
#else
#error("Unsupported platform")
#endif

internal struct Lock {
#if canImport(Darwin)
typealias Primitive = os_unfair_lock
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
typealias Primitive = pthread_mutex_t
#elseif canImport(WinSDK)
typealias Primitive = SRWLOCK
#else
typealias Primitive = Int
#error("Unsupported platform")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From gitblame it seem like this was added for web assembly:
https://github.com/apple/swift-async-algorithms/pull/273/files
Which seems that this can be checked for with arch(wasm32) or os(WASI)
There is also an underscore check for multithreading: #if _runtime(_multithreaded)
This was added in swiftlang/swift#72649

swift-testing uses a combination of os(WASI) and _runtime(_multithreaded) combined with compiler(>=6.1)
https://github.com/swiftlang/swift-testing/pull/322/files

FWIW Mutex has just landed in Swift 6.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @0xpablo

#endif

typealias PlatformLock = UnsafeMutablePointer<Primitive>
Expand All @@ -38,16 +42,18 @@ internal struct Lock {
fileprivate static func initialize(_ platformLock: PlatformLock) {
#if canImport(Darwin)
platformLock.initialize(to: os_unfair_lock())
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
let result = pthread_mutex_init(platformLock, nil)
precondition(result == 0, "pthread_mutex_init failed")
#elseif canImport(WinSDK)
InitializeSRWLock(platformLock)
#else
#error("Unsupported platform")
#endif
}

fileprivate static func deinitialize(_ platformLock: PlatformLock) {
#if canImport(Glibc)
#if canImport(Glibc) || canImport(Musl)
let result = pthread_mutex_destroy(platformLock)
precondition(result == 0, "pthread_mutex_destroy failed")
#endif
Expand All @@ -57,21 +63,25 @@ internal struct Lock {
fileprivate static func lock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_lock(platformLock)
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
pthread_mutex_lock(platformLock)
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
#else
#error("Unsupported platform")
#endif
}

fileprivate static func unlock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_unlock(platformLock)
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
let result = pthread_mutex_unlock(platformLock)
precondition(result == 0, "pthread_mutex_unlock failed")
#elseif canImport(WinSDK)
ReleaseSRWLockExclusive(platformLock)
#else
#error("Unsupported platform")
#endif
}

Expand Down
12 changes: 8 additions & 4 deletions Sources/AsyncSequenceValidation/TaskDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ import _CAsyncSequenceValidationSupport
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#else
#error("Unsupported platform")
#endif

#if canImport(Darwin)
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
return nil
Expand All @@ -38,7 +42,7 @@ final class TaskDriver {
let queue: WorkQueue
#if canImport(Darwin)
var thread: pthread_t?
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
var thread = pthread_t()
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
Expand All @@ -50,7 +54,7 @@ final class TaskDriver {
}

func start() {
#if canImport(Darwin) || canImport(Glibc)
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl)
pthread_create(&thread, nil, start_thread,
Unmanaged.passRetained(self).toOpaque())
#elseif canImport(WinSDK)
Expand All @@ -68,7 +72,7 @@ final class TaskDriver {
func join() {
#if canImport(Darwin)
pthread_join(thread!, nil)
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl)
pthread_join(thread, nil)
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
Expand Down