Skip to content

Commit cfd9452

Browse files
committed
[Basic] Make DiagnosticsEngine thread safe
1 parent d01f13b commit cfd9452

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Sources/Basic/DiagnosticsEngine.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
import Dispatch
12+
1113
/// A type which can be used as a diagnostic parameter.
1214
public protocol DiagnosticParameter {
1315

@@ -250,18 +252,28 @@ public protocol DiagnosticsScope {
250252
}
251253

252254
public class DiagnosticsEngine: CustomStringConvertible {
255+
256+
/// Queue to protect concurrent mutations to the diagnositcs engine.
257+
private let queue = DispatchQueue(label: "\(DiagnosticsEngine.self)")
258+
253259
/// The diagnostics produced by the engine.
254-
public var diagnostics: [Diagnostic] = []
260+
public var diagnostics: [Diagnostic] {
261+
return queue.sync { _diagnostics }
262+
}
263+
private var _diagnostics: [Diagnostic] = []
255264

265+
/// Returns true if there is an error diagnostics in the engine.
256266
public var hasErrors: Bool {
257267
return diagnostics.contains(where: { $0.behavior == .error })
258268
}
259-
269+
260270
public init() {
261271
}
262272

263273
public func emit(data: DiagnosticData, location: DiagnosticLocation) {
264-
diagnostics.append(Diagnostic(location: location, data: data))
274+
queue.sync {
275+
_diagnostics.append(Diagnostic(location: location, data: data))
276+
}
265277
}
266278

267279
/// Merges contents of given engine.

0 commit comments

Comments
 (0)