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
{
25 if ( std::chrono::steady_clock::now() > timeout_time
) {
28 context
* active_ctx
= context::active();
29 detail::spinlock_lock lk
{ wait_queue_splk_
};
30 if ( nullptr == owner_
) {
34 if ( ! wait_queue_
.suspend_and_wait_until( lk
, active_ctx
, timeout_time
)) {
43 context
* active_ctx
= context::active();
44 // store this fiber in order to be notified later
45 detail::spinlock_lock lk
{ wait_queue_splk_
};
46 if ( BOOST_UNLIKELY( active_ctx
== owner_
) ) {
48 std::make_error_code( std::errc::resource_deadlock_would_occur
),
49 "boost fiber: a deadlock is detected" };
51 if ( nullptr == owner_
) {
55 wait_queue_
.suspend_and_wait( lk
, active_ctx
);
60 timed_mutex::try_lock() {
61 context
* active_ctx
= context::active();
62 detail::spinlock_lock lk
{ wait_queue_splk_
};
63 if ( BOOST_UNLIKELY( active_ctx
== owner_
) ) {
65 std::make_error_code( std::errc::resource_deadlock_would_occur
),
66 "boost fiber: a deadlock is detected" };
68 if ( nullptr == owner_
) {
72 // let other fiber release the lock
74 return active_ctx
== owner_
;
78 timed_mutex::unlock() {
79 context
* active_ctx
= context::active();
80 detail::spinlock_lock lk
{ wait_queue_splk_
};
81 if ( BOOST_UNLIKELY( active_ctx
!= owner_
) ) {
83 std::make_error_code( std::errc::operation_not_permitted
),
84 "boost fiber: no privilege to perform the operation" };
88 wait_queue_
.notify_one();
93 #ifdef BOOST_HAS_ABI_HEADERS
94 # include BOOST_ABI_SUFFIX