Skip to content

Commit

Permalink
Avoid using NSLock.withLock because Linux doesn't support it before…
Browse files Browse the repository at this point in the history
… Swift 6.0 (#93)
  • Loading branch information
d-ronnqvist authored Aug 23, 2024
1 parent a255bbd commit 0510d91
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PackagePlugin
/// Generating symbol graphs is the only task that can't run in parallel.
///
/// We serialize its execution to support build concurrency for the other tasks.
private let symbolGraphGenerationLock = NSLock()
private let symbolGraphGenerationLock = Lock()

extension PackageManager {
struct DocCSymbolGraphResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct DocumentationBuildGraphRunner<Target: DocumentationBuildGraphTarget> {

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

Expand Down
24 changes: 24 additions & 0 deletions Sources/SwiftDocCPluginUtilities/Lock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors

import Foundation

/// A wrapper around NSLock.
///
/// This type exist to offer an alternative to `NSLock.withLock` on Linux before Swift 6.0.
struct Lock {
private let innerLock = NSLock()

func withLock<Result>(_ body: () throws -> Result) rethrows -> Result {
// Use `lock()` and `unlock()` because Linux doesn't support `NSLock.withLock` before Swift 6.0
innerLock.lock()
defer { innerLock.unlock() }

return try body()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private extension DocumentationBuildGraphRunner {
performing work: @escaping Work<TaskResult>
) -> (processedTargets: [TaskStatus], result: Result<[TaskResult], any Error>) {
var processedTargets: [TaskStatus] = []
let lock = NSLock()
let lock = Lock()

let result = Swift.Result(catching: {
try self.perform { task in
Expand Down

0 comments on commit 0510d91

Please sign in to comment.