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#include "boost/fiber/recursive_mutex.hpp"
8
9#include <algorithm>
10#include <functional>
11
12#include "boost/fiber/exceptions.hpp"
13#include "boost/fiber/scheduler.hpp"
14
15#ifdef BOOST_HAS_ABI_HEADERS
16# include BOOST_ABI_PREFIX
17#endif
18
19namespace boost {
20namespace fibers {
21
22void
23recursive_mutex::lock() {
24 while ( true) {
25 context * active_ctx = context::active();
26 // store this fiber in order to be notified later
27 detail::spinlock_lock lk{ wait_queue_splk_ };
28 if ( active_ctx == owner_) {
29 ++count_;
30 return;
31 }
32 if ( nullptr == owner_) {
33 owner_ = active_ctx;
34 count_ = 1;
35 return;
36 }
37
38 wait_queue_.suspend_and_wait( lk, active_ctx);
39 }
40}
41
42bool
43recursive_mutex::try_lock() noexcept {
44 context * active_ctx = context::active();
45 detail::spinlock_lock lk{ wait_queue_splk_ };
46 if ( nullptr == owner_) {
47 owner_ = active_ctx;
48 count_ = 1;
49 } else if ( active_ctx == owner_) {
50 ++count_;
51 }
52 lk.unlock();
53 // let other fiber release the lock
54 context::active()->yield();
55 return active_ctx == owner_;
56}
57
58void
59recursive_mutex::unlock() {
60 context * active_ctx = context::active();
61 detail::spinlock_lock lk( wait_queue_splk_);
62 if ( BOOST_UNLIKELY( active_ctx != owner_) ) {
63 throw lock_error(
64 std::make_error_code( e: std::errc::operation_not_permitted),
65 "boost fiber: no privilege to perform the operation");
66 }
67 if ( 0 == --count_) {
68 owner_ = nullptr;
69 wait_queue_.notify_one();
70 }
71}
72
73}}
74
75#ifdef BOOST_HAS_ABI_HEADERS
76# include BOOST_ABI_SUFFIX
77#endif
78

source code of boost/libs/fiber/src/recursive_mutex.cpp