12#include "llvm/Support/AdvisoryLock.h"
13#include "llvm/Support/Chrono.h"
19class ReaderWriterLock :
public llvm::AdvisoryLock {
20 ModuleCacheEntry &Entry;
21 std::optional<unsigned> OwnedGeneration;
24 ReaderWriterLock(ModuleCacheEntry &Entry) : Entry(Entry) {}
26 Expected<bool> tryLock()
override {
27 std::lock_guard<std::mutex> Lock(Entry.Mutex);
31 OwnedGeneration = Entry.Generation;
35 llvm::WaitForUnlockResult
36 waitForUnlockFor(std::chrono::seconds MaxSeconds)
override {
37 assert(!OwnedGeneration);
38 std::unique_lock<std::mutex> Lock(Entry.Mutex);
39 unsigned CurrentGeneration = Entry.Generation;
40 bool Success = Entry.CondVar.wait_for(Lock, MaxSeconds, [&] {
43 return !Entry.Locked || Entry.Generation != CurrentGeneration;
45 return Success ? llvm::WaitForUnlockResult::Success
46 : llvm::WaitForUnlockResult::Timeout;
49 std::error_code unsafeUnlock()
override {
51 std::lock_guard<std::mutex> Lock(Entry.Mutex);
52 Entry.Generation += 1;
55 Entry.CondVar.notify_all();
59 ~ReaderWriterLock()
override {
60 if (OwnedGeneration) {
62 std::lock_guard<std::mutex> Lock(Entry.Mutex);
65 if (*OwnedGeneration == Entry.Generation)
68 Entry.CondVar.notify_all();
74 ModuleCacheEntries &Entries;
80 InMemoryModuleCache InMemory;
83 InProcessModuleCache(ModuleCacheEntries &Entries) : Entries(Entries) {}
85 void prepareForGetLock(StringRef Filename)
override {}
87 std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename)
override {
88 auto &Entry = [&]() -> ModuleCacheEntry & {
89 std::lock_guard<std::mutex> Lock(Entries.Mutex);
90 auto &Entry = Entries.Map[Filename];
92 Entry = std::make_unique<ModuleCacheEntry>();
95 return std::make_unique<ReaderWriterLock>(Entry);
98 std::time_t getModuleTimestamp(StringRef Filename)
override {
99 auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
100 std::lock_guard<std::mutex> Lock(Entries.Mutex);
101 auto &Entry = Entries.Map[Filename];
103 Entry = std::make_unique<ModuleCacheEntry>();
107 return Timestamp.load();
110 void updateModuleTimestamp(StringRef Filename)
override {
112 auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
113 std::lock_guard<std::mutex> Lock(Entries.Mutex);
114 auto &Entry = Entries.Map[Filename];
116 Entry = std::make_unique<ModuleCacheEntry>();
120 Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
123 void maybePrune(StringRef Path, time_t PruneInterval,
124 time_t PruneAfter)
override {
130 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
131 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
137std::shared_ptr<ModuleCache>
139 return std::make_shared<InProcessModuleCache>(Entries);
The module cache used for compiling modules implicitly.
std::shared_ptr< ModuleCache > makeInProcessModuleCache(ModuleCacheEntries &Entries)
The JSON file list parser is used to communicate input to InstallAPI.
@ Success
Annotation was successful.
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter)
Shared implementation of ModuleCache::maybePrune().
std::atomic< std::time_t > Timestamp