Skip to content

Commit 0510d91

Browse files
authored
Avoid using NSLock.withLock because Linux doesn't support it before Swift 6.0 (#93)
1 parent a255bbd commit 0510d91

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

Plugins/SharedPackagePluginExtensions/PackageManager+getSymbolGraphsForDocC.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import PackagePlugin
1212
/// Generating symbol graphs is the only task that can't run in parallel.
1313
///
1414
/// We serialize its execution to support build concurrency for the other tasks.
15-
private let symbolGraphGenerationLock = NSLock()
15+
private let symbolGraphGenerationLock = Lock()
1616

1717
extension PackageManager {
1818
struct DocCSymbolGraphResult {

Sources/SwiftDocCPluginUtilities/BuildGraph/DocumentationBuildGraphRunner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct DocumentationBuildGraphRunner<Target: DocumentationBuildGraphTarget> {
2222

2323
// Operations can't raise errors. Instead we catch the error from 'performBuildTask(_:)'
2424
// and cancel the remaining tasks.
25-
let resultLock = NSLock()
25+
let resultLock = Lock()
2626
var caughtError: Error?
2727
var results: [Result] = []
2828

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
import Foundation
10+
11+
/// A wrapper around NSLock.
12+
///
13+
/// This type exist to offer an alternative to `NSLock.withLock` on Linux before Swift 6.0.
14+
struct Lock {
15+
private let innerLock = NSLock()
16+
17+
func withLock<Result>(_ body: () throws -> Result) rethrows -> Result {
18+
// Use `lock()` and `unlock()` because Linux doesn't support `NSLock.withLock` before Swift 6.0
19+
innerLock.lock()
20+
defer { innerLock.unlock() }
21+
22+
return try body()
23+
}
24+
}

Tests/SwiftDocCPluginUtilitiesTests/DocumentationBuildGraphRunnerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private extension DocumentationBuildGraphRunner {
233233
performing work: @escaping Work<TaskResult>
234234
) -> (processedTargets: [TaskStatus], result: Result<[TaskResult], any Error>) {
235235
var processedTargets: [TaskStatus] = []
236-
let lock = NSLock()
236+
let lock = Lock()
237237

238238
let result = Swift.Result(catching: {
239239
try self.perform { task in

0 commit comments

Comments
 (0)