1 // Copyright (c) 2007-2012 Hartmut Kaiser 2 // Copyright (c) 2013-2015 Agustin Berge 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 7 #ifndef HPX_LCOS_LOCAL_MUTEX_HPP 8 #define HPX_LCOS_LOCAL_MUTEX_HPP 9 10 #include <hpx/config.hpp> 11 #include <hpx/error_code.hpp> 12 #include <hpx/lcos/local/detail/condition_variable.hpp> 13 #include <hpx/lcos/local/spinlock.hpp> 14 #include <hpx/runtime/threads/thread_data_fwd.hpp> 15 #include <hpx/util/steady_clock.hpp> 16 17 namespace hpx { namespace lcos { namespace local 18 { 19 /////////////////////////////////////////////////////////////////////////// 20 class mutex 21 { 22 public: 23 HPX_NON_COPYABLE(mutex); 24 25 protected: 26 typedef lcos::local::spinlock mutex_type; 27 28 public: 29 HPX_EXPORT mutex(char const* const description = ""); 30 31 HPX_EXPORT ~mutex(); 32 33 HPX_EXPORT void lock(char const* description, error_code& ec = throws); 34 lock(error_code & ec=throws)35 void lock(error_code& ec = throws) 36 { 37 return lock("mutex::lock", ec); 38 } 39 40 HPX_EXPORT bool try_lock(char const* description, error_code& ec = throws); 41 try_lock(error_code & ec=throws)42 bool try_lock(error_code& ec = throws) 43 { 44 return try_lock("mutex::try_lock", ec); 45 } 46 47 HPX_EXPORT void unlock(error_code& ec = throws); 48 49 protected: 50 mutable mutex_type mtx_; 51 threads::thread_id_type owner_id_; 52 detail::condition_variable cond_; 53 }; 54 55 /////////////////////////////////////////////////////////////////////////// 56 class timed_mutex : private mutex 57 { 58 public: 59 HPX_NON_COPYABLE(timed_mutex); 60 61 public: 62 HPX_EXPORT timed_mutex(char const* const description = ""); 63 64 HPX_EXPORT ~timed_mutex(); 65 66 using mutex::lock; 67 using mutex::try_lock; 68 using mutex::unlock; 69 70 HPX_EXPORT bool try_lock_until(util::steady_time_point const& abs_time, 71 char const* description, error_code& ec = throws); 72 try_lock_until(util::steady_time_point const & abs_time,error_code & ec=throws)73 bool try_lock_until(util::steady_time_point const& abs_time, 74 error_code& ec = throws) 75 { 76 return try_lock_until(abs_time, "mutex::try_lock_until", ec); 77 } 78 try_lock_for(util::steady_duration const & rel_time,char const * description,error_code & ec=throws)79 bool try_lock_for(util::steady_duration const& rel_time, 80 char const* description, error_code& ec = throws) 81 { 82 return try_lock_until(rel_time.from_now(), description, ec); 83 } 84 try_lock_for(util::steady_duration const & rel_time,error_code & ec=throws)85 bool try_lock_for(util::steady_duration const& rel_time, 86 error_code& ec = throws) 87 { 88 return try_lock_for(rel_time, "mutex::try_lock_for", ec); 89 } 90 }; 91 }}} 92 93 #endif /*HPX_LCOS_LOCAL_MUTEX_HPP*/ 94