MOTION  0.01
Framework for mixed-protocol multi-party computation
pooled_work_stealing.hpp
Go to the documentation of this file.
1 // This file is adapted from fiber/algo/work_stealing.hpp of Boost 1.71.0.
2 //
3 // Copyright Oliver Kowalke 2015 / Lennart Braun 2019.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file BOOST_SOFTWARE_LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #ifndef POOLED_WORK_STEALING_H
10 #define POOLED_WORK_STEALING_H
11 
12 #include <atomic>
13 #include <chrono>
14 #include <condition_variable>
15 #include <cstddef>
16 #include <cstdint>
17 #include <mutex>
18 #include <vector>
19 
20 #include <boost/config.hpp>
21 #include <boost/intrusive_ptr.hpp>
22 
23 #include <boost/fiber/algo/algorithm.hpp>
24 #include <boost/fiber/context.hpp>
25 #include <boost/fiber/detail/config.hpp>
26 #include <boost/fiber/detail/context_spinlock_queue.hpp>
27 #include <boost/fiber/detail/context_spmc_queue.hpp>
28 #include <boost/fiber/scheduler.hpp>
29 #include <boost/thread/barrier.hpp>
30 
31 // clang-format off
32 
33 struct pool_ctx;
34 
35 class pooled_work_stealing : public boost::fibers::algo::algorithm {
36  std::shared_ptr<pool_ctx> pool_ctx_;
37 
38  std::uint32_t id_;
39  std::uint32_t thread_count_;
40 #ifdef BOOST_FIBERS_USE_SPMC_QUEUE
41  boost::fibers::detail::context_spmc_queue rqueue_ {};
42 #else
43  boost::fibers::detail::context_spinlock_queue rqueue_ {};
44 #endif
45  std::mutex mtx_ {};
46  std::condition_variable cnd_{};
47  bool flag_{false};
48  bool suspend_;
49 
50  static void init_(std::uint32_t, std::vector<boost::intrusive_ptr<pooled_work_stealing>>&);
51 
52 public:
53  static std::shared_ptr<pool_ctx> create_pool_ctx(std::uint32_t, bool = false);
54  pooled_work_stealing(std::shared_ptr<pool_ctx>);
56 
59 
62 
63  virtual void awakened(boost::fibers::context*) noexcept;
64 
65  virtual boost::fibers::context* pick_next() noexcept;
66 
67  virtual boost::fibers::context* steal() noexcept {
68  return rqueue_.steal();
69  }
70 
71  virtual bool has_ready_fibers() const noexcept {
72  return !rqueue_.empty();
73  }
74 
75  virtual void suspend_until(std::chrono::steady_clock::time_point const&) noexcept;
76 
77  virtual void notify() noexcept;
78 };
79 
80 // clang-format on
81 
82 #endif // POOLED_WORK_STEALING_H
pooled_work_stealing::create_pool_ctx
static std::shared_ptr< pool_ctx > create_pool_ctx(std::uint32_t, bool=false)
Definition: pooled_work_stealing.cpp:37
pooled_work_stealing::pick_next
virtual boost::fibers::context * pick_next() noexcept
Definition: pooled_work_stealing.cpp:65
pooled_work_stealing::notify
virtual void notify() noexcept
Definition: pooled_work_stealing.cpp:121
pooled_work_stealing::~pooled_work_stealing
~pooled_work_stealing()
Definition: pooled_work_stealing.cpp:52
pool_ctx::schedulers_
std::vector< pooled_work_stealing * > schedulers_
Definition: pooled_work_stealing.cpp:33
pooled_work_stealing::pooled_work_stealing
pooled_work_stealing(std::shared_ptr< pool_ctx >)
Definition: pooled_work_stealing.cpp:43
pool_ctx::thread_count_
const std::uint32_t thread_count_
Definition: pooled_work_stealing.cpp:30
pool_ctx
Definition: pooled_work_stealing.cpp:21
pool_ctx::pool_ctx
pool_ctx(std::uint32_t thread_count, bool suspend)
Definition: pooled_work_stealing.cpp:22
pooled_work_stealing::awakened
virtual void awakened(boost::fibers::context *) noexcept
Definition: pooled_work_stealing.cpp:58
pooled_work_stealing::suspend_until
virtual void suspend_until(std::chrono::steady_clock::time_point const &) noexcept
Definition: pooled_work_stealing.cpp:101
pooled_work_stealing
Definition: pooled_work_stealing.hpp:35
pool_ctx::counter_
std::atomic< std::uint32_t > counter_
Definition: pooled_work_stealing.cpp:32
pooled_work_stealing.hpp
pool_ctx::suspend_
const bool suspend_
Definition: pooled_work_stealing.cpp:31
pool_ctx::barrier_
boost::barrier barrier_
Definition: pooled_work_stealing.cpp:34
pooled_work_stealing::has_ready_fibers
virtual bool has_ready_fibers() const noexcept
Definition: pooled_work_stealing.hpp:71
pooled_work_stealing::operator=
pooled_work_stealing & operator=(pooled_work_stealing const &)=delete
pooled_work_stealing::steal
virtual boost::fibers::context * steal() noexcept
Definition: pooled_work_stealing.hpp:67