From df69d473d3ab389e915698b8c2c4bb8d22975976 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 22 Oct 2019 14:32:49 +0200 Subject: Implement loaded_modules state locking This would be necessary if someone runs two parallel top-level contexts. --- libbuild2/module.hxx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'libbuild2/module.hxx') diff --git a/libbuild2/module.hxx b/libbuild2/module.hxx index 3aaa1c7..50ca7da 100644 --- a/libbuild2/module.hxx +++ b/libbuild2/module.hxx @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -153,6 +154,38 @@ namespace build2 // found. // using loaded_module_map = std::map; + + // The loaded_modules map is locked per top-level (as opposed to nested) + // context (see context.hxx for details). + // + // Note: should only be constructed during context-wide serial execution. + // + class LIBBUILD2_SYMEXPORT loaded_modules_lock + { + public: + explicit + loaded_modules_lock (context& c) + : ctx_ (c), lock_ (mutex_, defer_lock) + { + if (ctx_.modules_lock == nullptr) + { + lock_.lock (); + ctx_.modules_lock = this; + } + } + + ~loaded_modules_lock () + { + if (ctx_.modules_lock == this) + ctx_.modules_lock = nullptr; + } + + private: + static mutex mutex_; + context& ctx_; + mlock lock_; + }; + LIBBUILD2_SYMEXPORT extern loaded_module_map loaded_modules; // Load a builtin module (i.e., a module linked as a static/shared library -- cgit v1.1