Skip to content

Commit

Permalink
Use Bionic module from new Android overlay in Swift 6 instead (#326)
Browse files Browse the repository at this point in the history
The new module and overlay were merged into Swift 6 in swiftlang/swift#74758.
  • Loading branch information
finagolfin authored Aug 7, 2024
1 parent e83857c commit 2503842
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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)
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

0 comments on commit 2503842

Please sign in to comment.