Skip to content

Commit 6fac6f7

Browse files
Add support for Windows (#71)
Implement the lock primitive.
1 parent ea1f9d3 commit 6fac6f7

File tree

8 files changed

+68
-19
lines changed

8 files changed

+68
-19
lines changed

.github/workflows/main.yml

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ jobs:
2020
linux_6_0_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
2121
linux_nightly_next_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
2222
linux_nightly_main_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
23+
windows_6_0_enabled: true
24+
windows_nightly_6_1_enabled: true
25+
windows_nightly_main_enabled: true
26+
windows_6_0_arguments_override: "--explicit-target-dependency-import-check error"
27+
windows_nightly_6_1_arguments_override: "--explicit-target-dependency-import-check error"
28+
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"
2329

2430
macos-tests:
2531
name: macOS tests

.github/workflows/pull_request.yml

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ jobs:
2424
linux_6_0_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
2525
linux_nightly_next_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
2626
linux_nightly_main_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
27+
windows_6_0_enabled: true
28+
windows_nightly_6_1_enabled: true
29+
windows_nightly_main_enabled: true
30+
windows_6_0_arguments_override: "--explicit-target-dependency-import-check error"
31+
windows_nightly_6_1_arguments_override: "--explicit-target-dependency-import-check error"
32+
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"
2733

2834
cxx-interop:
2935
name: Cxx interop

Package.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let package = Package(
3434
platforms: [.macOS(.v10_15), .macCatalyst(.v13), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
3535
products: [.library(name: "OpenAPIURLSession", targets: ["OpenAPIURLSession"])],
3636
dependencies: [
37-
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
37+
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.8.2"),
3838
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"),
3939
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
4040
],
@@ -50,14 +50,21 @@ let package = Package(
5050
),
5151
.testTarget(
5252
name: "OpenAPIURLSessionTests",
53-
dependencies: ["OpenAPIURLSession", .product(name: "NIOTestUtils", package: "swift-nio")],
53+
dependencies: ["OpenAPIURLSession"],
5454
swiftSettings: swiftSettings
5555
),
5656
]
5757
)
5858

59+
#if !os(Windows) // NIO not yet supported on Windows
5960
// Test-only dependencies.
6061
package.dependencies += [.package(url: "https://github.com/apple/swift-nio", from: "2.62.0")]
62+
package.targets.forEach { target in
63+
if target.name == "OpenAPIURLSessionTests" {
64+
target.dependencies += [.product(name: "NIOTestUtils", package: "swift-nio")]
65+
}
66+
}
67+
#endif
6168

6269
// --- STANDARD CROSS-REPO SETTINGS DO NOT EDIT --- //
6370
for target in package.targets {

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Use the transport with client code generated by [Swift OpenAPI Generator](https:
1111

1212
## Supported platforms and minimum versions
1313

14-
| macOS | Linux | iOS | tvOS | watchOS | visionOS |
14+
| macOS | Linux, Windows | iOS | tvOS | watchOS | visionOS |
1515
| :-: | :-: | :-: | :-: | :-: | :-: |
1616
| ✅ 10.15+ || ✅ 13+ | ✅ 13+ | ✅ 6+ | ✅ 1+ |
1717

Sources/OpenAPIURLSession/BufferedStream/Lock.swift

+23
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

Tests/OpenAPIURLSessionTests/NIOAsyncHTTP1TestServer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if !os(Windows) // NIO not yet supported on Windows
1415
import NIOCore
1516
import NIOPosix
1617
import NIOHTTP1
@@ -93,3 +94,4 @@ extension AsyncTestHTTP1Server {
9394
}
9495

9596
}
97+
#endif

Tests/OpenAPIURLSessionTests/TaskCancellationTests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import Foundation
1717
import HTTPTypes
1818
import NIO
19+
import NIOHTTP1
1920
import OpenAPIRuntime
2021
import XCTest
2122
@testable import OpenAPIURLSession

Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift

+20-16
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import Foundation
1616
import FoundationNetworking
1717
#endif
1818
import HTTPTypes
19+
#if !os(Windows) // NIO not yet supported on Windows
1920
import NIO
2021
import NIOHTTP1
22+
#endif
2123
import OpenAPIRuntime
2224
import XCTest
2325
@testable import OpenAPIURLSession
@@ -59,6 +61,7 @@ class URLSessionTransportConverterTests: XCTestCase {
5961
}
6062
}
6163

64+
#if !os(Windows) // NIO not yet supported on Windows
6265
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
6366
class URLSessionTransportBufferedTests: XCTestCase {
6467
var transport: URLSessionTransport!
@@ -133,22 +136,6 @@ class URLSessionTransportStreamingTests: XCTestCase {
133136
#endif
134137
}
135138

136-
class URLSessionTransportPlatformSupportTests: XCTestCase {
137-
func testDefaultsToStreamingIfSupported() {
138-
if URLSessionTransport.Configuration.Implementation.platformSupportsStreaming {
139-
guard case .streaming = URLSessionTransport.Configuration.Implementation.platformDefault else {
140-
XCTFail()
141-
return
142-
}
143-
} else {
144-
guard case .buffering = URLSessionTransport.Configuration.Implementation.platformDefault else {
145-
XCTFail()
146-
return
147-
}
148-
}
149-
}
150-
}
151-
152139
func testHTTPRedirect(
153140
transport: any ClientTransport,
154141
requestBodyIterationBehavior: IterationBehavior,
@@ -315,6 +302,23 @@ func testHTTPBasicPost(transport: any ClientTransport) async throws {
315302
group.cancelAll()
316303
}
317304
}
305+
#endif
306+
307+
class URLSessionTransportPlatformSupportTests: XCTestCase {
308+
func testDefaultsToStreamingIfSupported() {
309+
if URLSessionTransport.Configuration.Implementation.platformSupportsStreaming {
310+
guard case .streaming = URLSessionTransport.Configuration.Implementation.platformDefault else {
311+
XCTFail()
312+
return
313+
}
314+
} else {
315+
guard case .buffering = URLSessionTransport.Configuration.Implementation.platformDefault else {
316+
XCTFail()
317+
return
318+
}
319+
}
320+
}
321+
}
318322

319323
class URLSessionTransportDebugLoggingTests: XCTestCase {
320324
func testDebugLoggingEnabled() {

0 commit comments

Comments
 (0)