From f7a245b2b6091ef3a5e1193423c7fbbd6fe6a538 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 15 Dec 2020 09:25:19 +0200 Subject: Cache more results of executing programs (compilers, etc) --- libbuild2/utility.hxx | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'libbuild2/utility.hxx') diff --git a/libbuild2/utility.hxx b/libbuild2/utility.hxx index 44819b5..a07d928 100644 --- a/libbuild2/utility.hxx +++ b/libbuild2/utility.hxx @@ -4,6 +4,7 @@ #ifndef LIBBUILD2_UTILITY_HXX #define LIBBUILD2_UTILITY_HXX +#include #include // make_tuple() #include // make_shared() #include // to_string() @@ -18,8 +19,6 @@ #include // combine_hash(), reverse_iterate(), etc #include -#include - #include #include @@ -533,6 +532,39 @@ namespace build2 verbosity, pe, args, forward (f), error, ignore_exit, checksum); } + // Global, MT-safe information cache. Normally used for caching information + // (versions, targets, 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 string& k) const + { + mlock l (mutex_); + auto i (cache_.find (k)); + return i != cache_.end () ? &i->second : nullptr; + } + + const T& + insert (string k, T v) + { + mlock l (mutex_); + return cache_.insert (make_pair (move (k), move (v))).first->second; + } + + private: + std::map cache_; + mutable mutex mutex_; + }; + // File descriptor streams. // fdpipe -- cgit v1.1