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>
23 #include <boost/fiber/waker.hpp>
25 #ifdef BOOST_HAS_ABI_HEADERS
26 # include BOOST_ABI_PREFIX
30 # pragma warning(push)
31 # pragma warning(disable:4251)
37 class condition_variable;
39 class BOOST_FIBERS_DECL recursive_timed_mutex {
41 friend class condition_variable;
43 detail::spinlock wait_queue_splk_{};
44 wait_queue wait_queue_{};
45 context * owner_{ nullptr };
46 std::size_t count_{ 0 };
48 bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept;
51 recursive_timed_mutex() = default;
53 ~recursive_timed_mutex() {
54 BOOST_ASSERT( nullptr == owner_);
55 BOOST_ASSERT( 0 == count_);
56 BOOST_ASSERT( wait_queue_.empty() );
59 recursive_timed_mutex( recursive_timed_mutex const&) = delete;
60 recursive_timed_mutex & operator=( recursive_timed_mutex const&) = delete;
64 bool try_lock() noexcept;
66 template< typename Clock, typename Duration >
67 bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time_) {
68 std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_);
69 return try_lock_until_( timeout_time);
72 template< typename Rep, typename Period >
73 bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration) {
74 return try_lock_until_( std::chrono::steady_clock::now() + timeout_duration);
86 #ifdef BOOST_HAS_ABI_HEADERS
87 # include BOOST_ABI_SUFFIX
90 #endif // BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H