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 #include "boost/fiber/recursive_timed_mutex.hpp"
12 #include "boost/fiber/exceptions.hpp"
13 #include "boost/fiber/scheduler.hpp"
15 #ifdef BOOST_HAS_ABI_HEADERS
16 # include BOOST_ABI_PREFIX
23 recursive_timed_mutex::try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept {
24 if ( std::chrono::steady_clock::now() > timeout_time) {
27 context * active_ctx = context::active();
28 // store this fiber in order to be notified later
29 detail::spinlock_lock lk{ wait_queue_splk_ };
30 if ( active_ctx == owner_) {
33 } else if ( nullptr == owner_) {
38 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
39 active_ctx->wait_link( wait_queue_);
40 // suspend this fiber until notified or timed-out
41 if ( ! active_ctx->wait_until( timeout_time, lk) ) {
42 // remove fiber from wait-queue
44 wait_queue_.remove( * active_ctx);
47 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
48 return active_ctx == owner_;
52 recursive_timed_mutex::lock() {
53 context * active_ctx = context::active();
54 // store this fiber in order to be notified later
55 detail::spinlock_lock lk{ wait_queue_splk_ };
56 if ( active_ctx == owner_) {
59 } else if ( nullptr == owner_) {
64 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
65 active_ctx->wait_link( wait_queue_);
67 active_ctx->suspend( lk);
68 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
72 recursive_timed_mutex::try_lock() noexcept {
73 context * active_ctx = context::active();
74 detail::spinlock_lock lk{ wait_queue_splk_ };
75 if ( nullptr == owner_) {
78 } else if ( active_ctx == owner_) {
82 // let other fiber release the lock
84 return active_ctx == owner_;
88 recursive_timed_mutex::unlock() {
89 context * active_ctx = context::active();
90 detail::spinlock_lock lk{ wait_queue_splk_ };
91 if ( active_ctx != owner_) {
93 std::make_error_code( std::errc::operation_not_permitted),
94 "boost fiber: no privilege to perform the operation" };
97 if ( ! wait_queue_.empty() ) {
98 context * ctx = & wait_queue_.front();
99 wait_queue_.pop_front();
102 active_ctx->schedule( ctx);
112 #ifdef BOOST_HAS_ABI_HEADERS
113 # include BOOST_ABI_SUFFIX