uring_fiber_algo.h
1 // Copyright 2020, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 #include <boost/fiber/scheduler.hpp>
5 #include <string>
6 
7 namespace util {
8 namespace uring {
9 class Proactor;
10 
11 class UringFiberProps : public ::boost::fibers::fiber_properties {
12  public:
13  UringFiberProps(::boost::fibers::context* ctx) : fiber_properties(ctx) {
14  }
15 
16  void set_name(std::string nm) {
17  name_ = std::move(nm);
18  }
19 
20  const std::string& name() const {
21  return name_;
22  }
23 
24  private:
25  std::string name_;
26 };
27 
28 class UringFiberAlgo : public ::boost::fibers::algo::algorithm_with_properties<UringFiberProps> {
29  using ready_queue_type = ::boost::fibers::scheduler::ready_queue_type;
30 
31  public:
32  using FiberContext = ::boost::fibers::context;
33  using time_point = std::chrono::steady_clock::time_point;
34 
35  explicit UringFiberAlgo(Proactor* proactor);
36  ~UringFiberAlgo();
37 
38  void awakened(FiberContext* ctx, UringFiberProps& props) noexcept override;
39 
40  FiberContext* pick_next() noexcept override;
41 
42  void property_change(FiberContext* ctx, UringFiberProps& props) noexcept final;
43 
44  bool has_ready_fibers() const noexcept final;
45 
46  // suspend_until halts the thread in case there are no active fibers to run on it.
47  // This is done by dispatcher fiber.
48  void suspend_until(time_point const& abs_time) noexcept final;
49  //]
50 
51  // This function is called from remote threads, to wake this thread in case it's sleeping.
52  // In our case, "sleeping" means - might stuck the wait function waiting for completion events.
53  void notify() noexcept final;
54 
55  private:
56  ready_queue_type rqueue_;
57  Proactor* proactor_;
58  FiberContext* main_cntx_;
59  timespec ts_;
60  uint32_t ready_cnt_ = 0;
61 };
62 
63 } // namespace uring
64 } // namespace util