Skip to content

Commit e83857c

Browse files
authored
Add Musl import, error if unrecognised platform (apple#325)
* Add Musl import, error if unrecognised platform * Add #else #error for all platform checks
1 parent 6ae9a05 commit e83857c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Sources/AsyncAlgorithms/Locking.swift

+16-6
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@
1313
import Darwin
1414
#elseif canImport(Glibc)
1515
import Glibc
16+
#elseif canImport(Musl)
17+
import Musl
1618
#elseif canImport(WinSDK)
1719
import WinSDK
20+
#else
21+
#error("Unsupported platform")
1822
#endif
1923

2024
internal struct Lock {
2125
#if canImport(Darwin)
2226
typealias Primitive = os_unfair_lock
23-
#elseif canImport(Glibc)
27+
#elseif canImport(Glibc) || canImport(Musl)
2428
typealias Primitive = pthread_mutex_t
2529
#elseif canImport(WinSDK)
2630
typealias Primitive = SRWLOCK
2731
#else
28-
typealias Primitive = Int
32+
#error("Unsupported platform")
2933
#endif
3034

3135
typealias PlatformLock = UnsafeMutablePointer<Primitive>
@@ -38,16 +42,18 @@ internal struct Lock {
3842
fileprivate static func initialize(_ platformLock: PlatformLock) {
3943
#if canImport(Darwin)
4044
platformLock.initialize(to: os_unfair_lock())
41-
#elseif canImport(Glibc)
45+
#elseif canImport(Glibc) || canImport(Musl)
4246
let result = pthread_mutex_init(platformLock, nil)
4347
precondition(result == 0, "pthread_mutex_init failed")
4448
#elseif canImport(WinSDK)
4549
InitializeSRWLock(platformLock)
50+
#else
51+
#error("Unsupported platform")
4652
#endif
4753
}
4854

4955
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
50-
#if canImport(Glibc)
56+
#if canImport(Glibc) || canImport(Musl)
5157
let result = pthread_mutex_destroy(platformLock)
5258
precondition(result == 0, "pthread_mutex_destroy failed")
5359
#endif
@@ -57,21 +63,25 @@ internal struct Lock {
5763
fileprivate static func lock(_ platformLock: PlatformLock) {
5864
#if canImport(Darwin)
5965
os_unfair_lock_lock(platformLock)
60-
#elseif canImport(Glibc)
66+
#elseif canImport(Glibc) || canImport(Musl)
6167
pthread_mutex_lock(platformLock)
6268
#elseif canImport(WinSDK)
6369
AcquireSRWLockExclusive(platformLock)
70+
#else
71+
#error("Unsupported platform")
6472
#endif
6573
}
6674

6775
fileprivate static func unlock(_ platformLock: PlatformLock) {
6876
#if canImport(Darwin)
6977
os_unfair_lock_unlock(platformLock)
70-
#elseif canImport(Glibc)
78+
#elseif canImport(Glibc) || canImport(Musl)
7179
let result = pthread_mutex_unlock(platformLock)
7280
precondition(result == 0, "pthread_mutex_unlock failed")
7381
#elseif canImport(WinSDK)
7482
ReleaseSRWLockExclusive(platformLock)
83+
#else
84+
#error("Unsupported platform")
7585
#endif
7686
}
7787

Sources/AsyncSequenceValidation/TaskDriver.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ import _CAsyncSequenceValidationSupport
1515
import Darwin
1616
#elseif canImport(Glibc)
1717
import Glibc
18+
#elseif canImport(Musl)
19+
import Musl
1820
#elseif canImport(WinSDK)
1921
#error("TODO: Port TaskDriver threading to windows")
22+
#else
23+
#error("Unsupported platform")
2024
#endif
2125

2226
#if canImport(Darwin)
2327
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
2428
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
2529
return nil
2630
}
27-
#elseif canImport(Glibc)
31+
#elseif canImport(Glibc) || canImport(Musl)
2832
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
2933
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
3034
return nil
@@ -38,7 +42,7 @@ final class TaskDriver {
3842
let queue: WorkQueue
3943
#if canImport(Darwin)
4044
var thread: pthread_t?
41-
#elseif canImport(Glibc)
45+
#elseif canImport(Glibc) || canImport(Musl)
4246
var thread = pthread_t()
4347
#elseif canImport(WinSDK)
4448
#error("TODO: Port TaskDriver threading to windows")
@@ -50,7 +54,7 @@ final class TaskDriver {
5054
}
5155

5256
func start() {
53-
#if canImport(Darwin) || canImport(Glibc)
57+
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl)
5458
pthread_create(&thread, nil, start_thread,
5559
Unmanaged.passRetained(self).toOpaque())
5660
#elseif canImport(WinSDK)
@@ -68,7 +72,7 @@ final class TaskDriver {
6872
func join() {
6973
#if canImport(Darwin)
7074
pthread_join(thread!, nil)
71-
#elseif canImport(Glibc)
75+
#elseif canImport(Glibc) || canImport(Musl)
7276
pthread_join(thread, nil)
7377
#elseif canImport(WinSDK)
7478
#error("TODO: Port TaskDriver threading to windows")

0 commit comments

Comments
 (0)