1 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) 6 7 #ifndef BOOST_FIBERS_ALGO_SHARED_WORK_H 8 #define BOOST_FIBERS_ALGO_SHARED_WORK_H 9 10 #include <condition_variable> 11 #include <chrono> 12 #include <deque> 13 #include <mutex> 14 15 #include <boost/config.hpp> 16 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> 21 22 #ifdef BOOST_HAS_ABI_HEADERS 23 # include BOOST_ABI_PREFIX 24 #endif 25 26 #ifdef _MSC_VER 27 # pragma warning(push) 28 # pragma warning(disable:4251) 29 #endif 30 31 namespace boost { 32 namespace fibers { 33 namespace algo { 34 35 class BOOST_FIBERS_DECL shared_work : public algorithm { 36 private: 37 typedef std::deque< context * > rqueue_type; 38 typedef scheduler::ready_queue_type lqueue_type; 39 40 static rqueue_type rqueue_; 41 static std::mutex rqueue_mtx_; 42 43 lqueue_type lqueue_{}; 44 std::mutex mtx_{}; 45 std::condition_variable cnd_{}; 46 bool flag_{ false }; 47 bool suspend_{ false }; 48 49 public: 50 shared_work() = default; 51 shared_work(bool suspend)52 shared_work( bool suspend) : 53 suspend_{ suspend } { 54 } 55 56 shared_work( shared_work const&) = delete; 57 shared_work( shared_work &&) = delete; 58 59 shared_work & operator=( shared_work const&) = delete; 60 shared_work & operator=( shared_work &&) = delete; 61 62 void awakened( context * ctx) noexcept; 63 64 context * pick_next() noexcept; 65 has_ready_fibers() const66 bool has_ready_fibers() const noexcept { 67 std::unique_lock< std::mutex > lock{ rqueue_mtx_ }; 68 return ! rqueue_.empty() || ! lqueue_.empty(); 69 } 70 71 void suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept; 72 73 void notify() noexcept; 74 }; 75 76 }}} 77 78 #ifdef _MSC_VER 79 # pragma warning(pop) 80 #endif 81 82 #ifdef BOOST_HAS_ABI_HEADERS 83 # include BOOST_ABI_SUFFIX 84 #endif 85 86 #endif // BOOST_FIBERS_ALGO_SHARED_WORK_H 87