From 88f0780e34116c0441a8d8c58b8a8fd9fde4b1f5 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 25 Jan 2017 15:41:44 +0200 Subject: Add model mutex, make var_pool const by default --- build2/types | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'build2/types') diff --git a/build2/types b/build2/types index f8abef2..1e7a347 100644 --- a/build2/types +++ b/build2/types @@ -9,7 +9,6 @@ #include #include #include -#include #include // unique_ptr, shared_ptr #include // pair, move() #include // size_t, nullptr_t @@ -19,6 +18,11 @@ #include // function, reference_wrapper #include +#include +#include +#include +#include + #include // ios_base::failure #include // exception #include // logic_error, invalid_argument, runtime_error @@ -69,10 +73,56 @@ namespace build2 using std::istream; using std::ostream; + // Concurrency. + // using std::future; - // Exceptions. While is included, there is no using for - // std::exception -- use qualified. +#ifdef __cpp_lib_shared_mutex + using shared_mutex = std::shared_mutex; +#else + using shared_mutex = std::shared_timed_mutex; +#endif + + using slock = std::shared_lock; + using ulock = std::unique_lock; + + // Re-lock shared to exclusive for the lifetime or rlock. + // + struct rlock + { + explicit + rlock (slock* sl) + : sl_ (sl) + { + if (sl_ != nullptr) + { + sl_->unlock (); + ul_ = ulock (*sl_->mutex ()); + } + } + + ~rlock () + { + if (sl_ != nullptr) + { + ul_.unlock (); + sl_->lock (); + } + } + + // Can be treated as const ulock. + // + operator const ulock& () const {return ul_;} + + private: + slock* sl_; + ulock ul_; + }; + + // Exceptions. + // + // While is included, there is no using for std::exception -- + // use qualified. // using std::logic_error; using std::invalid_argument; -- cgit v1.1