13
13
import Darwin
14
14
#elseif canImport(Glibc)
15
15
import Glibc
16
+ #elseif canImport(Musl)
17
+ import Musl
16
18
#elseif canImport(WinSDK)
17
19
import WinSDK
20
+ #else
21
+ #error("Unsupported platform")
18
22
#endif
19
23
20
24
internal struct Lock {
21
25
#if canImport(Darwin)
22
26
typealias Primitive = os_unfair_lock
23
- #elseif canImport(Glibc)
27
+ #elseif canImport(Glibc) || canImport(Musl)
24
28
typealias Primitive = pthread_mutex_t
25
29
#elseif canImport(WinSDK)
26
30
typealias Primitive = SRWLOCK
27
31
#else
28
- typealias Primitive = Int
32
+ #error("Unsupported platform")
29
33
#endif
30
34
31
35
typealias PlatformLock = UnsafeMutablePointer < Primitive >
@@ -38,16 +42,18 @@ internal struct Lock {
38
42
fileprivate static func initialize( _ platformLock: PlatformLock ) {
39
43
#if canImport(Darwin)
40
44
platformLock. initialize ( to: os_unfair_lock ( ) )
41
- #elseif canImport(Glibc)
45
+ #elseif canImport(Glibc) || canImport(Musl)
42
46
let result = pthread_mutex_init ( platformLock, nil )
43
47
precondition ( result == 0 , " pthread_mutex_init failed " )
44
48
#elseif canImport(WinSDK)
45
49
InitializeSRWLock ( platformLock)
50
+ #else
51
+ #error("Unsupported platform")
46
52
#endif
47
53
}
48
54
49
55
fileprivate static func deinitialize( _ platformLock: PlatformLock ) {
50
- #if canImport(Glibc)
56
+ #if canImport(Glibc) || canImport(Musl)
51
57
let result = pthread_mutex_destroy ( platformLock)
52
58
precondition ( result == 0 , " pthread_mutex_destroy failed " )
53
59
#endif
@@ -57,21 +63,25 @@ internal struct Lock {
57
63
fileprivate static func lock( _ platformLock: PlatformLock ) {
58
64
#if canImport(Darwin)
59
65
os_unfair_lock_lock ( platformLock)
60
- #elseif canImport(Glibc)
66
+ #elseif canImport(Glibc) || canImport(Musl)
61
67
pthread_mutex_lock ( platformLock)
62
68
#elseif canImport(WinSDK)
63
69
AcquireSRWLockExclusive ( platformLock)
70
+ #else
71
+ #error("Unsupported platform")
64
72
#endif
65
73
}
66
74
67
75
fileprivate static func unlock( _ platformLock: PlatformLock ) {
68
76
#if canImport(Darwin)
69
77
os_unfair_lock_unlock ( platformLock)
70
- #elseif canImport(Glibc)
78
+ #elseif canImport(Glibc) || canImport(Musl)
71
79
let result = pthread_mutex_unlock ( platformLock)
72
80
precondition ( result == 0 , " pthread_mutex_unlock failed " )
73
81
#elseif canImport(WinSDK)
74
82
ReleaseSRWLockExclusive ( platformLock)
83
+ #else
84
+ #error("Unsupported platform")
75
85
#endif
76
86
}
77
87
0 commit comments