|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/Tooling/DependencyScanning/InProcessModuleCache.h" |
| 10 | + |
| 11 | +#include "clang/Serialization/InMemoryModuleCache.h" |
| 12 | +#include "llvm/Support/AdvisoryLock.h" |
| 13 | + |
| 14 | +#include <mutex> |
| 15 | + |
| 16 | +using namespace clang; |
| 17 | +using namespace tooling; |
| 18 | +using namespace dependencies; |
| 19 | + |
| 20 | +namespace { |
| 21 | +class ReaderWriterLock : public llvm::AdvisoryLock { |
| 22 | + // TODO: Consider using std::atomic::{wait,notify_all} when we move to C++20. |
| 23 | + std::unique_lock<std::shared_mutex> OwningLock; |
| 24 | + |
| 25 | +public: |
| 26 | + ReaderWriterLock(std::shared_mutex &Mutex) |
| 27 | + : OwningLock(Mutex, std::defer_lock) {} |
| 28 | + |
| 29 | + Expected<bool> tryLock() override { return OwningLock.try_lock(); } |
| 30 | + |
| 31 | + llvm::WaitForUnlockResult |
| 32 | + waitForUnlockFor(std::chrono::seconds MaxSeconds) override { |
| 33 | + assert(!OwningLock); |
| 34 | + // We do not respect the timeout here. It's very generous for implicit |
| 35 | + // modules, so we'd typically only reach it if the owner crashed (but so did |
| 36 | + // we, since we run in the same process), or encountered deadlock. |
| 37 | + (void)MaxSeconds; |
| 38 | + std::shared_lock Lock(*OwningLock.mutex()); |
| 39 | + return llvm::WaitForUnlockResult::Success; |
| 40 | + } |
| 41 | + |
| 42 | + std::error_code unsafeMaybeUnlock() override { |
| 43 | + // Unlocking the mutex here would trigger UB and we don't expect this to be |
| 44 | + // actually called when compiling scanning modules due to the no-timeout |
| 45 | + // guarantee above. |
| 46 | + return {}; |
| 47 | + } |
| 48 | + |
| 49 | + ~ReaderWriterLock() override = default; |
| 50 | +}; |
| 51 | + |
| 52 | +class InProcessModuleCache : public ModuleCache { |
| 53 | + ModuleCacheMutexes &Mutexes; |
| 54 | + |
| 55 | + // TODO: If we changed the InMemoryModuleCache API and relied on strict |
| 56 | + // context hash, we could probably create more efficient thread-safe |
| 57 | + // implementation of the InMemoryModuleCache such that it does need to be |
| 58 | + // recreated for each translation unit. |
| 59 | + InMemoryModuleCache InMemory; |
| 60 | + |
| 61 | +public: |
| 62 | + InProcessModuleCache(ModuleCacheMutexes &Mutexes) : Mutexes(Mutexes) {} |
| 63 | + |
| 64 | + void prepareForGetLock(StringRef Filename) override {} |
| 65 | + |
| 66 | + std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename) override { |
| 67 | + auto &Mtx = [&]() -> std::shared_mutex & { |
| 68 | + std::lock_guard Lock(Mutexes.Mutex); |
| 69 | + auto &Mutex = Mutexes.Map[Filename]; |
| 70 | + if (!Mutex) |
| 71 | + Mutex = std::make_unique<std::shared_mutex>(); |
| 72 | + return *Mutex; |
| 73 | + }(); |
| 74 | + return std::make_unique<ReaderWriterLock>(Mtx); |
| 75 | + } |
| 76 | + |
| 77 | + InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; } |
| 78 | + const InMemoryModuleCache &getInMemoryModuleCache() const override { |
| 79 | + return InMemory; |
| 80 | + } |
| 81 | +}; |
| 82 | +} // namespace |
| 83 | + |
| 84 | +IntrusiveRefCntPtr<ModuleCache> |
| 85 | +dependencies::makeInProcessModuleCache(ModuleCacheMutexes &Mutexes) { |
| 86 | + return llvm::makeIntrusiveRefCnt<InProcessModuleCache>(Mutexes); |
| 87 | +} |
0 commit comments