Skip to content

Commit 4e70d53

Browse files
committed
Add support for Windows
Implement the lock primitive.
1 parent 4ecb8b4 commit 4e70d53

File tree

1 file changed

+23
-0
lines changed
  • Sources/OpenAPIURLSession/BufferedStream

1 file changed

+23
-0
lines changed

Sources/OpenAPIURLSession/BufferedStream/Lock.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@
3030
import Darwin
3131
#elseif canImport(Glibc)
3232
import Glibc
33+
#elseif os(Windows)
34+
import WinSDK
3335
#endif
3436

37+
#if os(Windows)
38+
@usableFromInline
39+
typealias LockPrimitive = SRWLOCK
40+
#else
3541
@usableFromInline
3642
typealias LockPrimitive = pthread_mutex_t
43+
#endif
3744

3845
@usableFromInline
3946
enum LockOperations {}
@@ -43,35 +50,51 @@ extension LockOperations {
4350
static func create(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
4451
mutex.assertValidAlignment()
4552

53+
#if os(Windows)
54+
InitializeSRWLock(mutex)
55+
#else
4656
var attr = pthread_mutexattr_t()
4757
pthread_mutexattr_init(&attr)
4858

4959
let err = pthread_mutex_init(mutex, &attr)
5060
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
61+
#endif
5162
}
5263

5364
@inlinable
5465
static func destroy(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
5566
mutex.assertValidAlignment()
5667

68+
#if os(Windows)
69+
// SRWLOCK does not need to be freed
70+
#else
5771
let err = pthread_mutex_destroy(mutex)
5872
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
73+
#endif
5974
}
6075

6176
@inlinable
6277
static func lock(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
6378
mutex.assertValidAlignment()
6479

80+
#if os(Windows)
81+
AcquireSRWLockExclusive(mutex)
82+
#else
6583
let err = pthread_mutex_lock(mutex)
6684
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
85+
#endif
6786
}
6887

6988
@inlinable
7089
static func unlock(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
7190
mutex.assertValidAlignment()
7291

92+
#if os(Windows)
93+
ReleaseSRWLockExclusive(mutex)
94+
#else
7395
let err = pthread_mutex_unlock(mutex)
7496
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
97+
#endif
7598
}
7699
}
77100

0 commit comments

Comments
 (0)