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