Skip to content

Commit cc413d8

Browse files
committed
[Explicit Module Builds] Add basic thread-safety to InterModuleDependencyOracle
1 parent abc4959 commit cc413d8

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

Sources/SwiftDriver/Explicit Module Builds/Inter Module Dependencies/CommonDependencyOperations.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414
/// An API to allow clients to accumulate InterModuleDependencyGraphs across mutiple main modules/targets
1515
/// into a single collection of discovered modules.
1616
func mergeModules(from dependencyGraph: InterModuleDependencyGraph) throws {
17-
for (moduleId, moduleInfo) in dependencyGraph.modules {
18-
try InterModuleDependencyGraph.mergeModule(moduleId, moduleInfo, into: &modules)
17+
try self.lock.withLock {
18+
for (moduleId, moduleInfo) in dependencyGraph.modules {
19+
try InterModuleDependencyGraph.mergeModule(moduleId, moduleInfo, into: &modules)
20+
}
1921
}
2022
}
2123

2224
// This is a backwards-compatibility shim to handle existing ModuleInfoMap-based API
2325
// used by SwiftPM
2426
func mergeModules(from moduleInfoMap: ModuleInfoMap) throws {
25-
for (moduleId, moduleInfo) in moduleInfoMap {
26-
try InterModuleDependencyGraph.mergeModule(moduleId, moduleInfo, into: &modules)
27+
try self.lock.withLock {
28+
for (moduleId, moduleInfo) in moduleInfoMap {
29+
try InterModuleDependencyGraph.mergeModule(moduleId, moduleInfo, into: &modules)
30+
}
2731
}
2832
}
2933
}

Sources/SwiftDriver/Explicit Module Builds/Inter Module Dependencies/InterModuleDependencyOracle.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import TSCBasic
14+
1315
// An inter-module dependency oracle, responsible for responding to queries about
1416
// dependencies of a given module, caching already-discovered dependencies along the way.
1517
//
@@ -26,13 +28,15 @@
2628
public class InterModuleDependencyOracle {
2729
/// Query the ModuleInfo of a module with a given ID
2830
@_spi(Testing) public func getModuleInfo(of moduleId: ModuleDependencyId) -> ModuleInfo? {
29-
return modules[moduleId]
31+
self.lock.withLock {
32+
return modules[moduleId]
33+
}
3034
}
3135

3236
/// Query the direct dependencies of a module with a given ID
3337
@_spi(Testing) public func getDependencies(of moduleId: ModuleDependencyId)
3438
-> [ModuleDependencyId]? {
35-
return modules[moduleId]?.directDependencies
39+
return getModuleInfo(of: moduleId)?.directDependencies
3640
}
3741

3842
// TODO: This will require a SwiftDriver entry-point for scanning a module given
@@ -44,6 +48,8 @@ public class InterModuleDependencyOracle {
4448
// commandLine: [Job.ArgTemplate]) -> InterModuleDependencyGraph {}
4549
//
4650

51+
internal let lock = Lock()
52+
4753
/// The complete set of modules discovered so far, spanning potentially multiple targets
4854
internal var modules: ModuleInfoMap = [:]
4955

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ extension Driver {
407407
}
408408

409409
// Re-scan Clang modules at all the targets they will be built against.
410+
// TODO: Should be deprecated once switched over to libSwiftScan
410411
try resolveVersionedClangDependencies(dependencyGraph: &dependencyGraph)
411412

412413
// Set dependency modules' paths to be saved in the module cache.

0 commit comments

Comments
 (0)