2 // Copyright 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)
8 #ifndef BOOST_FIBERS_ALGO_WORK_STEALING_H
9 #define BOOST_FIBERS_ALGO_WORK_STEALING_H
12 #include <condition_variable>
19 #include <boost/config.hpp>
20 #include <boost/intrusive_ptr.hpp>
22 #include <boost/fiber/algo/algorithm.hpp>
23 #include <boost/fiber/context.hpp>
24 #include <boost/fiber/detail/config.hpp>
25 #include <boost/fiber/detail/context_spinlock_queue.hpp>
26 #include <boost/fiber/detail/context_spmc_queue.hpp>
27 #include <boost/fiber/scheduler.hpp>
29 #ifdef BOOST_HAS_ABI_HEADERS
30 # include BOOST_ABI_PREFIX
37 class work_stealing : public algorithm {
39 static std::atomic< std::uint32_t > counter_;
40 static std::vector< intrusive_ptr< work_stealing > > schedulers_;
43 std::uint32_t thread_count_;
44 #ifdef BOOST_FIBERS_USE_SPMC_QUEUE
45 detail::context_spmc_queue rqueue_{};
47 detail::context_spinlock_queue rqueue_{};
50 std::condition_variable cnd_{};
54 static void init_( std::uint32_t, std::vector< intrusive_ptr< work_stealing > > &);
57 work_stealing( std::uint32_t, bool = false);
59 work_stealing( work_stealing const&) = delete;
60 work_stealing( work_stealing &&) = delete;
62 work_stealing & operator=( work_stealing const&) = delete;
63 work_stealing & operator=( work_stealing &&) = delete;
65 virtual void awakened( context *) noexcept;
67 virtual context * pick_next() noexcept;
69 virtual context * steal() noexcept {
70 return rqueue_.steal();
73 virtual bool has_ready_fibers() const noexcept {
74 return ! rqueue_.empty();
77 virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept;
79 virtual void notify() noexcept;
84 #ifdef BOOST_HAS_ABI_HEADERS
85 # include BOOST_ABI_SUFFIX
88 #endif // BOOST_FIBERS_ALGO_WORK_STEALING_H