| 305 | } |
| 306 | |
| 307 | Ref<Bundle> ResourceManager::getBundle(const StringBox& bundleName) { |
| 308 | std::unique_lock<Mutex> lock(_mutex); |
| 309 | const auto& it = _bundleByName.find(bundleName); |
| 310 | if (it != _bundleByName.end()) { |
| 311 | return it->second; |
| 312 | } |
| 313 | |
| 314 | VALDI_TRACE_META("Valdi.loadBundle", bundleName); |
| 315 | snap::utils::time::StopWatch sw; |
| 316 | sw.start(); |
| 317 | |
| 318 | auto bundleInitializer = registerBundle(bundleName); |
| 319 | auto inlineAssetsEnabled = _inlineAssetsEnabled; |
| 320 | |
| 321 | // Snapshot the mmap/metrics settings while we still hold _mutex. getArchiveForModule runs below |
| 322 | // while the BundleInitializer holds the Bundle's mutex; it must not take _mutex itself, or it |
| 323 | // would invert the cleanup path's (_mutex -> Bundle mutex) order and can deadlock. |
| 324 | bool useMmap = false; |
| 325 | if (_runtimeTweaks != nullptr) { |
| 326 | useMmap = _runtimeTweaks->enableMmapModuleArchives() && !_mmapCacheDirectory.empty(); |
| 327 | } |
| 328 | auto mmapCacheDir = _mmapCacheDirectory; |
| 329 | auto metrics = _metrics; |
| 330 | |
| 331 | // We now have a lock on the Bundle itself. Release our lock so that |
| 332 | // other threads can query the ResourceManager on other bundles. |
| 333 | lock.unlock(); |
| 334 | |
| 335 | auto assetPackageKey = STRING_LITERAL("res.assetpackage"); |
| 336 | auto hasAssetPackage = false; |
| 337 | |
| 338 | auto archiveResult = getArchiveForModule(bundleName, useMmap, mmapCacheDir, metrics); |
| 339 | if (!archiveResult) { |
| 340 | if (!_hotReloaderEnabled) { |
| 341 | VALDI_ERROR(_logger, "Failed to load archive of Module '{}': {}", bundleName, archiveResult.error()); |
| 342 | } |
| 343 | initializeBundle(bundleInitializer, makeShared<ValdiModuleArchive>()); |
| 344 | } else { |
| 345 | hasAssetPackage = inlineAssetsEnabled && archiveResult.value()->containsEntry(assetPackageKey); |
| 346 | initializeBundle(bundleInitializer, archiveResult.value()); |
| 347 | } |
| 348 | |
| 349 | populateSourceMap(bundleName, *bundleInitializer.getBundle()); |
| 350 | |
| 351 | if (hasAssetPackage) { |
| 352 | insertAssetPackageInBundle(bundleInitializer.getBundle(), |
| 353 | bundleInitializer.getBundle()->getEntry(assetPackageKey).value()); |
| 354 | } |
| 355 | |
| 356 | if (Valdi::traceLoadModules) { |
| 357 | VALDI_INFO(_logger, "Loaded bundle '{}' in {}", bundleName, sw.elapsed()); |
| 358 | } |
| 359 | |
| 360 | return bundleInitializer.getBundle(); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Returns the list of all loaded bundle names. |