Skip to content

Commit d5cab75

Browse files
committed
Add Windows support by fixing Locks.swift to support windows
**Motivation:** Swift is cross platform, including Windows, we should support distributed tracing on any platform **Modifications:** Add windows CI and make necessary changes **Result:** Windows support <!-- After your change, what will change. -->
1 parent d9c3031 commit d5cab75

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

.github/workflows/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ jobs:
1818
linux_6_2_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
1919
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2020
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
21+
windows_6_0_enabled: true
22+
windows_6_1_enabled: true
23+
windows_nightly_next_enabled: true
24+
windows_nightly_main_enabled: true
25+
windows_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
26+
windows_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
27+
windows_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
28+
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2129

2230
benchmarks:
2331
name: Benchmarks

.github/workflows/pull_request.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ jobs:
4040
linux_6_2_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
4141
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
4242
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
43+
windows_6_0_enabled: true
44+
windows_6_1_enabled: true
45+
windows_nightly_next_enabled: true
46+
windows_nightly_main_enabled: true
47+
windows_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
48+
windows_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
49+
windows_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
50+
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
4351

4452
benchmarks:
4553
name: Benchmarks

Sources/Instrumentation/Locks.swift

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29-
#if canImport(Darwin)
29+
#if canImport(WASILibc)
30+
// No locking on WASILibc
31+
#elseif canImport(Darwin)
3032
import Darwin
33+
#elseif os(Windows)
34+
import WinSDK
3135
#elseif canImport(Glibc)
3236
import Glibc
3337
#elseif canImport(Android)
@@ -50,45 +54,89 @@ import wasi_pthread
5054
/// one used by NIO.
5155
@_spi(Locking) // Use the `package` access modifier once min Swift version is increased.
5256
public final class ReadWriteLock {
53-
private let rwlock: UnsafeMutablePointer<pthread_rwlock_t> = UnsafeMutablePointer.allocate(capacity: 1)
57+
#if canImport(WASILibc)
58+
// WASILibc is single threaded, provides no locks
59+
#elseif os(Windows)
60+
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
61+
UnsafeMutablePointer.allocate(capacity: 1)
62+
#else
63+
fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t> = UnsafeMutablePointer.allocate(capacity: 1)
64+
#endif
5465

5566
/// Create a new lock.
5667
public init() {
68+
#if canImport(WASILibc)
69+
// WASILibc is single threaded, provides no locks
70+
#elseif os(Windows)
71+
InitializeSRWLock(self.rwlock)
72+
#else
5773
let err = pthread_rwlock_init(self.rwlock, nil)
5874
precondition(err == 0, "pthread_rwlock_init failed with error \(err)")
75+
#endif
5976
}
6077

6178
deinit {
79+
#if canImport(WASILibc)
80+
// WASILibc is single threaded, provides no locks
81+
#elseif os(Windows)
82+
// SRWLOCK does not need to be free'd
83+
self.rwlock.deallocate()
84+
#else
6285
let err = pthread_rwlock_destroy(self.rwlock)
6386
precondition(err == 0, "pthread_rwlock_destroy failed with error \(err)")
6487
self.rwlock.deallocate()
88+
#endif
6589
}
6690

6791
/// Acquire a reader lock.
6892
///
6993
/// Whenever possible, consider using `withLock` instead of this method and
7094
/// `unlock`, to simplify lock handling.
7195
public func lockRead() {
96+
#if canImport(WASILibc)
97+
// WASILibc is single threaded, provides no locks
98+
#elseif os(Windows)
99+
AcquireSRWLockShared(self.rwlock)
100+
self.shared = true
101+
#else
72102
let err = pthread_rwlock_rdlock(self.rwlock)
73103
precondition(err == 0, "pthread_rwlock_rdlock failed with error \(err)")
104+
#endif
74105
}
75106

76107
/// Acquire a writer lock.
77108
///
78109
/// Whenever possible, consider using `withLock` instead of this method and
79110
/// `unlock`, to simplify lock handling.
80111
public func lockWrite() {
112+
#if canImport(WASILibc)
113+
// WASILibc is single threaded, provides no locks
114+
#elseif os(Windows)
115+
AcquireSRWLockExclusive(self.rwlock)
116+
self.shared = false
117+
#else
81118
let err = pthread_rwlock_wrlock(self.rwlock)
82119
precondition(err == 0, "pthread_rwlock_wrlock failed with error \(err)")
120+
#endif
83121
}
84122

85123
/// Release the lock.
86124
///
87125
/// Whenever possible, consider using `withLock` instead of this method and
88126
/// `lock`, to simplify lock handling.
89127
public func unlock() {
128+
#if canImport(WASILibc)
129+
// WASILibc is single threaded, provides no locks
130+
#elseif os(Windows)
131+
if self.shared {
132+
ReleaseSRWLockShared(self.rwlock)
133+
} else {
134+
ReleaseSRWLockExclusive(self.rwlock)
135+
}
136+
#else
90137
let err = pthread_rwlock_unlock(self.rwlock)
91138
precondition(err == 0, "pthread_rwlock_unlock failed with error \(err)")
139+
#endif
92140
}
93141
}
94142

0 commit comments

Comments
 (0)