Skip to content
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

Use Bionic module from new Android overlay in Swift 6 instead #326

Merged
merged 1 commit into from
Aug 7, 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
12 changes: 7 additions & 5 deletions Sources/AsyncAlgorithms/Locking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import Glibc
import Musl
#elseif canImport(WinSDK)
import WinSDK
#elseif canImport(Bionic)
import Bionic
#else
#error("Unsupported platform")
#endif

internal struct Lock {
#if canImport(Darwin)
typealias Primitive = os_unfair_lock
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
typealias Primitive = pthread_mutex_t
#elseif canImport(WinSDK)
typealias Primitive = SRWLOCK
Expand All @@ -42,7 +44,7 @@ internal struct Lock {
fileprivate static func initialize(_ platformLock: PlatformLock) {
#if canImport(Darwin)
platformLock.initialize(to: os_unfair_lock())
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let result = pthread_mutex_init(platformLock, nil)
precondition(result == 0, "pthread_mutex_init failed")
#elseif canImport(WinSDK)
Expand All @@ -53,7 +55,7 @@ internal struct Lock {
}

fileprivate static func deinitialize(_ platformLock: PlatformLock) {
#if canImport(Glibc) || canImport(Musl)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let result = pthread_mutex_destroy(platformLock)
precondition(result == 0, "pthread_mutex_destroy failed")
#endif
Expand All @@ -63,7 +65,7 @@ internal struct Lock {
fileprivate static func lock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_lock(platformLock)
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_mutex_lock(platformLock)
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
Expand All @@ -75,7 +77,7 @@ internal struct Lock {
fileprivate static func unlock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_unlock(platformLock)
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let result = pthread_mutex_unlock(platformLock)
precondition(result == 0, "pthread_mutex_unlock failed")
#elseif canImport(WinSDK)
Expand Down
15 changes: 11 additions & 4 deletions Sources/AsyncSequenceValidation/TaskDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#else
Expand All @@ -28,11 +30,16 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
#elseif canImport(Glibc) || canImport(Musl)
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two changes get start_thread() working with Swift 5.10 and NDK 26 also, by changing the signature for the nullability annotations. That's why this is the only portion of this pull to check os(Android), because that's available in both Swift 5.10 and 6.

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

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