2 // Copyright Nat Goodspeed + Oliver Kowalke 2015.
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 #ifndef BOOST_FIBERS_ALGO_SHARED_WORK_H
8 #define BOOST_FIBERS_ALGO_SHARED_WORK_H
10 #include <condition_variable>
15 #include <boost/config.hpp>
17 #include <boost/fiber/algo/algorithm.hpp>
18 #include <boost/fiber/context.hpp>
19 #include <boost/fiber/detail/config.hpp>
20 #include <boost/fiber/scheduler.hpp>
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
27 # pragma warning(push)
28 # pragma warning(disable:4251)
35 class BOOST_FIBERS_DECL shared_work : public algorithm {
37 typedef std::deque< context * > rqueue_type;
38 typedef scheduler::ready_queue_type lqueue_type;
40 static rqueue_type rqueue_;
41 static std::mutex rqueue_mtx_;
43 lqueue_type lqueue_{};
45 std::condition_variable cnd_{};
47 bool suspend_{ false };
50 shared_work() = default;
52 shared_work( bool suspend) :
56 shared_work( shared_work const&) = delete;
57 shared_work( shared_work &&) = delete;
59 shared_work & operator=( shared_work const&) = delete;
60 shared_work & operator=( shared_work &&) = delete;
62 void awakened( context * ctx) noexcept;
64 context * pick_next() noexcept;
66 bool has_ready_fibers() const noexcept {
67 std::unique_lock< std::mutex > lock{ rqueue_mtx_ };
68 return ! rqueue_.empty() || ! lqueue_.empty();
71 void suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept;
73 void notify() noexcept;
82 #ifdef BOOST_HAS_ABI_HEADERS
83 # include BOOST_ABI_SUFFIX
86 #endif // BOOST_FIBERS_ALGO_SHARED_WORK_H