Skip to content

Commit f957158

Browse files
authored
Make DiagnosticEngine thread safe (#243)
This allows consumers to emit diagnostics from multiple threads. Primarily motivated by swiftlang/swift-format#117
1 parent d518259 commit f957158

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

Sources/SwiftSyntax/DiagnosticEngine.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,39 @@ public class DiagnosticEngine {
2121
public init() {
2222
}
2323

24+
private var consumersQueue = DispatchQueue(
25+
label: "com.apple.SwiftSyntax.DiagnosticEngine.consumers", attributes: .concurrent)
26+
private var _consumers = [DiagnosticConsumer]()
2427
/// The list of consumers of the diagnostic passing through this engine.
25-
internal var consumers = [DiagnosticConsumer]()
28+
private var consumers: [DiagnosticConsumer] {
29+
get {
30+
consumersQueue.sync {
31+
_consumers
32+
}
33+
}
2634

27-
public private(set) var diagnostics = [Diagnostic]()
35+
set {
36+
consumersQueue.async(flags: .barrier) {
37+
self._consumers = newValue
38+
}
39+
}
40+
}
41+
42+
private var diagnosticsQueue = DispatchQueue(
43+
label: "com.apple.SwiftSyntax.DiagnosticEngine.diagnostics", attributes: .concurrent)
44+
private var _diagnostics = [Diagnostic]()
45+
public private(set) var diagnostics: [Diagnostic] {
46+
get {
47+
diagnosticsQueue.sync {
48+
_diagnostics
49+
}
50+
}
51+
set {
52+
diagnosticsQueue.async(flags: .barrier) {
53+
self._diagnostics = newValue
54+
}
55+
}
56+
}
2857

2958
internal var needsLineColumn: Bool {
3059
// Check if any consumer is interested in line and column.

0 commit comments

Comments
 (0)