diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2020-12-15 09:25:19 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2020-12-15 09:31:59 +0200 |
commit | f7a245b2b6091ef3a5e1193423c7fbbd6fe6a538 (patch) | |
tree | 28b3006840b718084f229820e0408b0712674232 /libbuild2/utility.hxx | |
parent | 8c9b0fb944a60d8193d8ac3dbac4e8e15f81bf57 (diff) |
Cache more results of executing programs (compilers, etc)
Diffstat (limited to 'libbuild2/utility.hxx')
-rw-r--r-- | libbuild2/utility.hxx | 36 |
1 files changed, 34 insertions, 2 deletions
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 <map> #include <tuple> // make_tuple() #include <memory> // make_shared() #include <string> // to_string() @@ -18,8 +19,6 @@ #include <libbutl/utility.mxx> // combine_hash(), reverse_iterate(), etc #include <libbutl/fdstream.mxx> -#include <unordered_set> - #include <libbuild2/types.hxx> #include <libbuild2/forward.hxx> @@ -533,6 +532,39 @@ namespace build2 verbosity, pe, args, forward<F> (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 <typename T> + 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<string, T> cache_; + mutable mutex mutex_; + }; + // File descriptor streams. // fdpipe |