1 2 // Copyright Oliver Kowalke 2013. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #ifndef BOOST_FIBERS_TIMED_MUTEX_H 8 #define BOOST_FIBERS_TIMED_MUTEX_H 9 10 #include <chrono> 11 12 #include <boost/assert.hpp> 13 #include <boost/config.hpp> 14 15 #include <boost/fiber/context.hpp> 16 #include <boost/fiber/detail/config.hpp> 17 #include <boost/fiber/detail/convert.hpp> 18 #include <boost/fiber/detail/spinlock.hpp> 19 20 #ifdef BOOST_HAS_ABI_HEADERS 21 # include BOOST_ABI_PREFIX 22 #endif 23 24 #ifdef _MSC_VER 25 # pragma warning(push) 26 # pragma warning(disable:4251) 27 #endif 28 29 namespace boost { 30 namespace fibers { 31 32 class condition_variable; 33 34 class BOOST_FIBERS_DECL timed_mutex { 35 private: 36 friend class condition_variable; 37 38 using wait_queue_type = context::wait_queue_t; 39 40 detail::spinlock wait_queue_splk_{}; 41 wait_queue_type wait_queue_{}; 42 context * owner_{ nullptr }; 43 44 bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept; 45 46 public: 47 timed_mutex() = default; 48 ~timed_mutex()49 ~timed_mutex() { 50 BOOST_ASSERT( nullptr == owner_); 51 BOOST_ASSERT( wait_queue_.empty() ); 52 } 53 54 timed_mutex( timed_mutex const&) = delete; 55 timed_mutex & operator=( timed_mutex const&) = delete; 56 57 void lock(); 58 59 bool try_lock(); 60 61 template< typename Clock, typename Duration > try_lock_until(std::chrono::time_point<Clock,Duration> const & timeout_time_)62 bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time_) { 63 std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); 64 return try_lock_until_( timeout_time); 65 } 66 67 template< typename Rep, typename Period > try_lock_for(std::chrono::duration<Rep,Period> const & timeout_duration)68 bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration) { 69 return try_lock_until_( std::chrono::steady_clock::now() + timeout_duration); 70 } 71 72 void unlock(); 73 }; 74 75 }} 76 77 #ifdef _MSC_VER 78 # pragma warning(pop) 79 #endif 80 81 #ifdef BOOST_HAS_ABI_HEADERS 82 # include BOOST_ABI_SUFFIX 83 #endif 84 85 #endif // BOOST_FIBERS_TIMED_MUTEX_H 86