| 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 | // based on boost::interprocess::sync::interprocess_spinlock |
| 8 | |
| 9 | #ifndef BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H |
| 10 | #define BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H |
| 11 | |
| 12 | #include <chrono> |
| 13 | #include <cstddef> |
| 14 | |
| 15 | #include <boost/config.hpp> |
| 16 | |
| 17 | #include <boost/assert.hpp> |
| 18 | |
| 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> |
| 24 | |
| 25 | #ifdef BOOST_HAS_ABI_HEADERS |
| 26 | # include BOOST_ABI_PREFIX |
| 27 | #endif |
| 28 | |
| 29 | #ifdef _MSC_VER |
| 30 | # pragma warning(push) |
| 31 | # pragma warning(disable:4251) |
| 32 | #endif |
| 33 | |
| 34 | namespace boost { |
| 35 | namespace fibers { |
| 36 | |
| 37 | class condition_variable; |
| 38 | |
| 39 | class BOOST_FIBERS_DECL recursive_timed_mutex { |
| 40 | private: |
| 41 | friend class condition_variable; |
| 42 | |
| 43 | detail::spinlock wait_queue_splk_{}; |
| 44 | wait_queue wait_queue_{}; |
| 45 | context * owner_{ nullptr }; |
| 46 | std::size_t count_{ 0 }; |
| 47 | |
| 48 | bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept; |
| 49 | |
| 50 | public: |
| 51 | recursive_timed_mutex() = default; |
| 52 | |
| 53 | ~recursive_timed_mutex() { |
| 54 | BOOST_ASSERT( nullptr == owner_); |
| 55 | BOOST_ASSERT( 0 == count_); |
| 56 | BOOST_ASSERT( wait_queue_.empty() ); |
| 57 | } |
| 58 | |
| 59 | recursive_timed_mutex( recursive_timed_mutex const&) = delete; |
| 60 | recursive_timed_mutex & operator=( recursive_timed_mutex const&) = delete; |
| 61 | |
| 62 | void lock(); |
| 63 | |
| 64 | bool try_lock() noexcept; |
| 65 | |
| 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); |
| 70 | } |
| 71 | |
| 72 | template< typename Rep, typename Period > |
| 73 | bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration) { |
| 74 | return try_lock_until_( timeout_time: std::chrono::steady_clock::now() + timeout_duration); |
| 75 | } |
| 76 | |
| 77 | void unlock(); |
| 78 | }; |
| 79 | |
| 80 | }} |
| 81 | |
| 82 | #ifdef _MSC_VER |
| 83 | # pragma warning(pop) |
| 84 | #endif |
| 85 | |
| 86 | #ifdef BOOST_HAS_ABI_HEADERS |
| 87 | # include BOOST_ABI_SUFFIX |
| 88 | #endif |
| 89 | |
| 90 | #endif // BOOST_FIBERS_RECURSIVE_TIMED_MUTEX_H |
| 91 |
