From 3c67f9e0e9d89221530df4d60326552f41f428e5 Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Tue, 6 Aug 2024 15:51:58 +0530 Subject: [PATCH] Use Bionic module from new Android overlay in Swift 6 instead The new module and overlay were merged into Swift 6 in swiftlang/swift#74758. --- Sources/AsyncAlgorithms/Locking.swift | 12 +++++++----- Sources/AsyncSequenceValidation/TaskDriver.swift | 15 +++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Sources/AsyncAlgorithms/Locking.swift b/Sources/AsyncAlgorithms/Locking.swift index 952b13c8..2bcd077e 100644 --- a/Sources/AsyncAlgorithms/Locking.swift +++ b/Sources/AsyncAlgorithms/Locking.swift @@ -15,12 +15,14 @@ import Darwin import Glibc #elseif canImport(WinSDK) import WinSDK +#elseif canImport(Bionic) +import Bionic #endif internal struct Lock { #if canImport(Darwin) typealias Primitive = os_unfair_lock -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Bionic) typealias Primitive = pthread_mutex_t #elseif canImport(WinSDK) typealias Primitive = SRWLOCK @@ -38,7 +40,7 @@ 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(Bionic) let result = pthread_mutex_init(platformLock, nil) precondition(result == 0, "pthread_mutex_init failed") #elseif canImport(WinSDK) @@ -47,7 +49,7 @@ internal struct Lock { } fileprivate static func deinitialize(_ platformLock: PlatformLock) { -#if canImport(Glibc) +#if canImport(Glibc) || canImport(Bionic) let result = pthread_mutex_destroy(platformLock) precondition(result == 0, "pthread_mutex_destroy failed") #endif @@ -57,7 +59,7 @@ internal struct Lock { fileprivate static func lock(_ platformLock: PlatformLock) { #if canImport(Darwin) os_unfair_lock_lock(platformLock) -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Bionic) pthread_mutex_lock(platformLock) #elseif canImport(WinSDK) AcquireSRWLockExclusive(platformLock) @@ -67,7 +69,7 @@ internal struct Lock { fileprivate static func unlock(_ platformLock: PlatformLock) { #if canImport(Darwin) os_unfair_lock_unlock(platformLock) -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Bionic) let result = pthread_mutex_unlock(platformLock) precondition(result == 0, "pthread_mutex_unlock failed") #elseif canImport(WinSDK) diff --git a/Sources/AsyncSequenceValidation/TaskDriver.swift b/Sources/AsyncSequenceValidation/TaskDriver.swift index ed128193..cb3f4e9c 100644 --- a/Sources/AsyncSequenceValidation/TaskDriver.swift +++ b/Sources/AsyncSequenceValidation/TaskDriver.swift @@ -15,6 +15,8 @@ import _CAsyncSequenceValidationSupport import Darwin #elseif canImport(Glibc) import Glibc +#elseif canImport(Bionic) +import Bionic #elseif canImport(WinSDK) #error("TODO: Port TaskDriver threading to windows") #endif @@ -24,11 +26,16 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? { Unmanaged.fromOpaque(raw).takeRetainedValue().run() return nil } -#elseif canImport(Glibc) +#elseif canImport(Glibc) && !os(Android) func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? { Unmanaged.fromOpaque(raw!).takeRetainedValue().run() return nil } +#elseif os(Android) +func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer { + Unmanaged.fromOpaque(raw).takeRetainedValue().run() + return UnsafeMutableRawPointer(bitPattern: 0xdeadbee)! +} #elseif canImport(WinSDK) #error("TODO: Port TaskDriver threading to windows") #endif @@ -38,7 +45,7 @@ final class TaskDriver { let queue: WorkQueue #if canImport(Darwin) var thread: pthread_t? -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Bionic) var thread = pthread_t() #elseif canImport(WinSDK) #error("TODO: Port TaskDriver threading to windows") @@ -50,7 +57,7 @@ final class TaskDriver { } func start() { -#if canImport(Darwin) || canImport(Glibc) +#if canImport(Darwin) || canImport(Glibc) || canImport(Bionic) pthread_create(&thread, nil, start_thread, Unmanaged.passRetained(self).toOpaque()) #elseif canImport(WinSDK) @@ -68,7 +75,7 @@ final class TaskDriver { func join() { #if canImport(Darwin) pthread_join(thread!, nil) -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Bionic) pthread_join(thread, nil) #elseif canImport(WinSDK) #error("TODO: Port TaskDriver threading to windows")