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)
7 // based on boost::interprocess::sync::interprocess_spinlock
9 #ifndef BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H
10 #define BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H
15 #include <boost/config.hpp>
17 #include <boost/assert.hpp>
19 #include <boost/fiber/context.hpp>
20 #include <boost/fiber/detail/config.hpp>
21 #include <boost/fiber/detail/convert.hpp>
22 #include <boost/fiber/detail/spinlock.hpp>
24 #ifdef BOOST_HAS_ABI_HEADERS
25 # include BOOST_ABI_PREFIX
29 # pragma warning(push)
30 # pragma warning(disable:4251)
36 class condition_variable;
38 class BOOST_FIBERS_DECL recursive_timed_mutex {
40 friend class condition_variable;
42 typedef context::wait_queue_t wait_queue_t;
44 context * owner_{ nullptr };
45 std::size_t count_{ 0 };
46 wait_queue_t wait_queue_{};
47 detail::spinlock wait_queue_splk_{};
49 bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept;
52 recursive_timed_mutex() = default;
54 ~recursive_timed_mutex() {
55 BOOST_ASSERT( nullptr == owner_);
56 BOOST_ASSERT( 0 == count_);
57 BOOST_ASSERT( wait_queue_.empty() );
60 recursive_timed_mutex( recursive_timed_mutex const&) = delete;
61 recursive_timed_mutex & operator=( recursive_timed_mutex const&) = delete;
65 bool try_lock() noexcept;
67 template< typename Clock, typename Duration >
68 bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time_) {
69 std::chrono::steady_clock::time_point timeout_time(
70 detail::convert( timeout_time_) );
71 return try_lock_until_( timeout_time);
74 template< typename Rep, typename Period >
75 bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration) {
76 return try_lock_until_( std::chrono::steady_clock::now() + timeout_duration);
88 #ifdef BOOST_HAS_ABI_HEADERS
89 # include BOOST_ABI_SUFFIX
92 #endif // BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H