From 61ac67105ef31e1ea31014e50e5b6deb74674594 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 13 Aug 2021 08:09:04 +0200 Subject: Cache git status results in version module --- libbuild2/types.hxx | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'libbuild2/types.hxx') diff --git a/libbuild2/types.hxx b/libbuild2/types.hxx index 0005e75..e3cacce 100644 --- a/libbuild2/types.hxx +++ b/libbuild2/types.hxx @@ -221,6 +221,40 @@ namespace build2 using std::thread; namespace this_thread = std::this_thread; + // Global, MT-safe information cache. Normally used for caching information + // (versions, target triplets, search paths, etc) extracted from other + // programs (compilers, etc). + // + // The key is normally a hash of all the inputs that can affect the output. + // + // Note that insertion is racy and it's possible the cache entry already + // exists, in which case we ignore our value assuming it is the same. + // + template + class global_cache + { + public: + const T* + find (const K& k) const + { + mlock l (mutex_); + auto i (cache_.find (k)); + return i != cache_.end () ? &i->second : nullptr; + } + + const T& + insert (K k, T v) + { + mlock l (mutex_); + return cache_.insert (std::make_pair (std::move (k), + std::move (v))).first->second; + } + + private: + map cache_; + mutable mutex mutex_; + }; + // Exceptions. // // While is included, there is no using for std::exception -- -- cgit v1.1