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/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 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 ( nullptr == owner_) {
34 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
35 active_ctx->wait_link( wait_queue_);
36 // suspend this fiber until notified or timed-out
37 if ( ! active_ctx->wait_until( timeout_time, lk) ) {
38 // remove fiber from wait-queue
40 wait_queue_.remove( * active_ctx);
43 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
49 context * active_ctx = context::active();
50 // store this fiber in order to be notified later
51 detail::spinlock_lock lk{ wait_queue_splk_ };
52 if ( active_ctx == owner_) {
54 std::make_error_code( std::errc::resource_deadlock_would_occur),
55 "boost fiber: a deadlock is detected" };
56 } else if ( nullptr == owner_) {
60 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
61 active_ctx->wait_link( wait_queue_);
63 active_ctx->suspend( lk);
64 BOOST_ASSERT( ! active_ctx->wait_is_linked() );
68 timed_mutex::try_lock() {
69 context * active_ctx = context::active();
70 detail::spinlock_lock lk{ wait_queue_splk_ };
71 if ( active_ctx == owner_) {
73 std::make_error_code( std::errc::resource_deadlock_would_occur),
74 "boost fiber: a deadlock is detected" };
75 } else if ( nullptr == owner_) {
79 // let other fiber release the lock
81 return active_ctx == owner_;
85 timed_mutex::unlock() {
86 context * active_ctx = context::active();
87 detail::spinlock_lock lk{ wait_queue_splk_ };
88 if ( active_ctx != owner_) {
90 std::make_error_code( std::errc::operation_not_permitted),
91 "boost fiber: no privilege to perform the operation" };
93 if ( ! wait_queue_.empty() ) {
94 context * ctx = & wait_queue_.front();
95 wait_queue_.pop_front();
97 active_ctx->schedule( ctx);
106 #ifdef BOOST_HAS_ABI_HEADERS
107 # include BOOST_ABI_SUFFIX