12#include "llvm/Support/AdvisoryLock.h"
13#include "llvm/Support/Chrono.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/IOSandbox.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/Path.h"
24 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
25 for (
auto &[Path, Entry] :
Map) {
27 assert(Entry->WrittenBuffer &&
"Wrote PCM with no contents");
34 (void)
writeImpl(Path, *Entry->WrittenBuffer, Size, ModTime);
40class ReaderWriterLock :
public llvm::AdvisoryLock {
42 std::optional<unsigned> OwnedGeneration;
48 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
56 llvm::WaitForUnlockResult
57 waitForUnlockFor(std::chrono::seconds MaxSeconds)
override {
58 assert(!OwnedGeneration);
59 std::unique_lock<std::mutex> Lock(Entry.
Mutex);
66 return Success ? llvm::WaitForUnlockResult::Success
67 : llvm::WaitForUnlockResult::Timeout;
70 std::error_code unsafeUnlock()
override {
72 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
80 ~ReaderWriterLock()
override {
81 if (OwnedGeneration) {
83 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
94class InProcessModuleCache :
public ModuleCache {
95 ModuleCacheEntries &Entries;
101 InMemoryModuleCache InMemory;
103 ModuleCacheEntry &getOrCreateEntry(StringRef Filename) {
104 std::lock_guard<std::mutex> Lock(Entries.
Mutex);
105 auto &Entry = Entries.
Map[Filename];
107 Entry = std::make_unique<ModuleCacheEntry>();
112 InProcessModuleCache(ModuleCacheEntries &Entries) : Entries(Entries) {}
114 std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename)
override {
115 auto &Entry = getOrCreateEntry(Filename);
116 return std::make_unique<ReaderWriterLock>(Entry);
119 std::time_t getModuleTimestamp(StringRef Filename)
override {
120 auto &Timestamp = getOrCreateEntry(Filename).Timestamp;
122 return Timestamp.load();
125 void updateModuleTimestamp(StringRef Filename)
override {
127 auto &Timestamp = getOrCreateEntry(Filename).Timestamp;
129 Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
132 void maybePrune(StringRef Path, time_t PruneInterval,
133 time_t PruneAfter)
override {
139 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
140 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
144 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
145 off_t &Size, time_t &ModTime)
override {
146 ModuleCacheEntry &Entry = getOrCreateEntry(Path);
147 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
150 assert(Entry.
WrittenBuffer->getBuffer() == Buffer.getBuffer() &&
151 "Wrote the same PCM with different contents");
157 llvm::MemoryBuffer::getMemBufferCopy(Buffer.getBuffer(), Path);
158 Entry.
ModTime = llvm::sys::toTimeT(std::chrono::system_clock::now());
165 Expected<std::unique_ptr<llvm::MemoryBuffer>>
166 read(StringRef
FileName, off_t &Size, time_t &ModTime)
override {
167 ModuleCacheEntry &Entry = getOrCreateEntry(
FileName);
168 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
171 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
176 return ReadBuffer.takeError();
184 Size = Buffer->getBufferSize();
187 return llvm::MemoryBuffer::getMemBuffer(*Buffer,
193std::shared_ptr<ModuleCache>
195 return std::make_shared<InProcessModuleCache>(Entries);
std::shared_ptr< ModuleCache > makeInProcessModuleCache(ModuleCacheEntries &Entries)
The JSON file list parser is used to communicate input to InstallAPI.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
@ Success
Annotation was successful.
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter, bool PruneTopLevel=false)
Shared implementation of ModuleCache::maybePrune().
std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::write().
llvm::StringMap< std::unique_ptr< ModuleCacheEntry > > Map
void flush()
Flushes all PCMs built in-process to disk.
enum clang::dependencies::ModuleCacheEntry::@010254116217305143125116322142262271134241253225 State
std::condition_variable CondVar
time_t ModTime
The modification time of the entry.
std::unique_ptr< llvm::MemoryBuffer > WrittenBuffer
The buffer we've written to module cache, if any.
std::unique_ptr< llvm::MemoryBuffer > ReadBuffer
The buffer we've read from disk, if any.