]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/algo/round_robin.hpp" | |
8 | ||
9 | #include <boost/assert.hpp> | |
b32b8144 | 10 | #include <boost/context/detail/prefetch.hpp> |
7c673cae FG |
11 | |
12 | #ifdef BOOST_HAS_ABI_HEADERS | |
13 | # include BOOST_ABI_PREFIX | |
14 | #endif | |
15 | ||
16 | namespace boost { | |
17 | namespace fibers { | |
18 | namespace algo { | |
19 | ||
20 | void | |
21 | round_robin::awakened( context * ctx) noexcept { | |
22 | BOOST_ASSERT( nullptr != ctx); | |
7c673cae | 23 | BOOST_ASSERT( ! ctx->ready_is_linked() ); |
b32b8144 | 24 | BOOST_ASSERT( ctx->is_resumable() ); |
7c673cae FG |
25 | ctx->ready_link( rqueue_); |
26 | } | |
27 | ||
28 | context * | |
29 | round_robin::pick_next() noexcept { | |
b32b8144 | 30 | context * victim = nullptr; |
7c673cae FG |
31 | if ( ! rqueue_.empty() ) { |
32 | victim = & rqueue_.front(); | |
33 | rqueue_.pop_front(); | |
b32b8144 | 34 | boost::context::detail::prefetch_range( victim, sizeof( context) ); |
7c673cae FG |
35 | BOOST_ASSERT( nullptr != victim); |
36 | BOOST_ASSERT( ! victim->ready_is_linked() ); | |
b32b8144 | 37 | BOOST_ASSERT( victim->is_resumable() ); |
7c673cae FG |
38 | } |
39 | return victim; | |
40 | } | |
41 | ||
42 | bool | |
43 | round_robin::has_ready_fibers() const noexcept { | |
44 | return ! rqueue_.empty(); | |
45 | } | |
46 | ||
47 | void | |
48 | round_robin::suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept { | |
49 | if ( (std::chrono::steady_clock::time_point::max)() == time_point) { | |
b32b8144 | 50 | std::unique_lock< std::mutex > lk{ mtx_ }; |
7c673cae FG |
51 | cnd_.wait( lk, [&](){ return flag_; }); |
52 | flag_ = false; | |
53 | } else { | |
b32b8144 | 54 | std::unique_lock< std::mutex > lk{ mtx_ }; |
7c673cae FG |
55 | cnd_.wait_until( lk, time_point, [&](){ return flag_; }); |
56 | flag_ = false; | |
57 | } | |
58 | } | |
59 | ||
60 | void | |
61 | round_robin::notify() noexcept { | |
b32b8144 | 62 | std::unique_lock< std::mutex > lk{ mtx_ }; |
7c673cae FG |
63 | flag_ = true; |
64 | lk.unlock(); | |
65 | cnd_.notify_all(); | |
66 | } | |
67 | ||
68 | }}} | |
69 | ||
70 | #ifdef BOOST_HAS_ABI_HEADERS | |
71 | # include BOOST_ABI_SUFFIX | |
72 | #endif |