Skip to content

Avoid using NSLock.withLock because Linux doesn't support it before Swift 6.0 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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