Skip to content

Commit

Permalink
Use Bionic module from new Android overlay in Swift 6 instead
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 committed Aug 6, 2024
1 parent 6ae9a05 commit 3c67f9e
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 @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions Sources/AsyncSequenceValidation/TaskDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,11 +26,16 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
#elseif canImport(Glibc)
#elseif canImport(Glibc) && !os(Android)
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 @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 3c67f9e

Please sign in to comment.