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
* 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( ! ctx
->wait_is_linked() );
35 ctx
->wait_link( wait_queue_
);
36 // suspend this fiber until notified or timed-out
37 if ( ! context::active()->wait_until( timeout_time
, lk
) ) {
38 // remove fiber from wait-queue
43 BOOST_ASSERT( ! ctx
->wait_is_linked() );
49 context
* ctx
= context::active();
50 // store this fiber in order to be notified later
51 detail::spinlock_lock
lk( wait_queue_splk_
);
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( ! ctx
->wait_is_linked() );
61 ctx
->wait_link( wait_queue_
);
64 BOOST_ASSERT( ! ctx
->wait_is_linked() );
68 timed_mutex::try_lock() {
69 context
* ctx
= context::active();
70 detail::spinlock_lock
lk( wait_queue_splk_
);
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
80 context::active()->yield();
85 timed_mutex::unlock() {
86 context
* ctx
= context::active();
87 detail::spinlock_lock
lk( wait_queue_splk_
);
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 context::active()->set_ready( ctx
);
106 #ifdef BOOST_HAS_ABI_HEADERS
107 # include BOOST_ABI_SUFFIX