Itoyori  v0.0.1
thread.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ityr/common/util.hpp"
5 #include "ityr/ito/worker.hpp"
6 #include "ityr/ito/scheduler.hpp"
7 
8 namespace ityr::ito {
9 
10 struct with_callback_t {};
11 inline constexpr with_callback_t with_callback;
12 
13 template <typename W>
14 struct workhint {
15  constexpr explicit workhint(W w1, W w2)
16  : work_new(w1), work_rest(w2) {}
17 
20 };
21 
22 template <typename T>
23 class thread {
24  // If the return value is void, set `no_retval_t` as the return type for the internal of the scheduler
25  using sched_retval_t = std::conditional_t<std::is_void_v<T>, no_retval_t, T>;
26 
27 public:
28  thread() {}
29 
30  template <typename Fn, typename... Args>
31  explicit thread(Fn&& fn, Args&&... args) {
32  fork(std::forward<Fn>(fn), std::forward<Args>(args)...);
33  }
34 
35  template <typename OnDriftForkCallback, typename OnDriftDieCallback, typename Fn, typename... Args>
36  thread(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb,
37  Fn&& fn, Args&&... args) {
38  fork(with_callback, on_drift_fork_cb, on_drift_die_cb,
39  std::forward<Fn>(fn), std::forward<Args>(args)...);
40  }
41 
42  template <typename W, typename Fn, typename... Args>
43  thread(workhint<W> wh, Fn&& fn, Args&&... args) {
44  fork(wh, std::forward<Fn>(fn), std::forward<Args>(args)...);
45  }
46 
47  template <typename OnDriftForkCallback, typename OnDriftDieCallback,
48  typename W, typename Fn, typename... Args>
49  thread(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb,
50  workhint<W> wh, Fn&& fn, Args&&... args) {
51  fork(with_callback, on_drift_fork_cb, on_drift_die_cb,
52  wh, std::forward<Fn>(fn), std::forward<Args>(args)...);
53  }
54 
55  thread(const thread&) = delete;
56  thread& operator=(const thread&) = delete;
57 
58  thread(thread&& th) = default;
59  thread& operator=(thread&& th) = default;
60 
61  template <typename Fn, typename... Args>
62  void fork(Fn&& fn, Args&&... args) {
63  auto& w = worker::instance::get();
64  ITYR_CHECK(!w.is_spmd());
65  w.sched().fork(handler_, nullptr, nullptr,
66  1, 1, std::forward<Fn>(fn), std::forward<Args>(args)...);
67  }
68 
69  template <typename OnDriftForkCallback, typename OnDriftDieCallback, typename Fn, typename... Args>
70  void fork(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb,
71  Fn&& fn, Args&&... args) {
72  auto& w = worker::instance::get();
73  ITYR_CHECK(!w.is_spmd());
74  w.sched().fork(handler_,
75  on_drift_fork_cb, on_drift_die_cb,
76  1, 1, std::forward<Fn>(fn), std::forward<Args>(args)...);
77  }
78 
79  template <typename W, typename Fn, typename... Args>
80  void fork(workhint<W> wh, Fn&& fn, Args&&... args) {
81  auto& w = worker::instance::get();
82  ITYR_CHECK(!w.is_spmd());
83  w.sched().fork(handler_, nullptr, nullptr,
84  wh.work_new, wh.work_rest, std::forward<Fn>(fn), std::forward<Args>(args)...);
85  }
86 
87  template <typename OnDriftForkCallback, typename OnDriftDieCallback,
88  typename W, typename Fn, typename... Args>
89  void fork(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb,
90  workhint<W> wh, Fn&& fn, Args&&... args) {
91  auto& w = worker::instance::get();
92  ITYR_CHECK(!w.is_spmd());
93  w.sched().fork(handler_,
94  on_drift_fork_cb, on_drift_die_cb,
95  wh.work_new, wh.work_rest, std::forward<Fn>(fn), std::forward<Args>(args)...);
96  }
97 
98  T join() {
99  auto& w = worker::instance::get();
100  ITYR_CHECK(!w.is_spmd());
101  if constexpr (std::is_void_v<T>) {
102  w.sched().join(handler_);
103  } else {
104  return w.sched().join(handler_);
105  }
106  }
107 
108  bool serialized() {
109  return scheduler::is_serialized(handler_);
110  }
111 
112 private:
113  scheduler::thread_handler<sched_retval_t> handler_;
114 };
115 
116 }
static auto & get()
Definition: util.hpp:180
Definition: thread.hpp:23
void fork(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb, workhint< W > wh, Fn &&fn, Args &&... args)
Definition: thread.hpp:89
thread(thread &&th)=default
thread(Fn &&fn, Args &&... args)
Definition: thread.hpp:31
bool serialized()
Definition: thread.hpp:108
thread()
Definition: thread.hpp:28
void fork(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb, Fn &&fn, Args &&... args)
Definition: thread.hpp:70
thread(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb, Fn &&fn, Args &&... args)
Definition: thread.hpp:36
thread(const thread &)=delete
void fork(Fn &&fn, Args &&... args)
Definition: thread.hpp:62
T join()
Definition: thread.hpp:98
thread & operator=(const thread &)=delete
thread & operator=(thread &&th)=default
thread(with_callback_t, OnDriftForkCallback on_drift_fork_cb, OnDriftDieCallback on_drift_die_cb, workhint< W > wh, Fn &&fn, Args &&... args)
Definition: thread.hpp:49
void fork(workhint< W > wh, Fn &&fn, Args &&... args)
Definition: thread.hpp:80
thread(workhint< W > wh, Fn &&fn, Args &&... args)
Definition: thread.hpp:43
#define ITYR_CHECK(cond)
Definition: util.hpp:48
va_list args
Definition: util.hpp:76
Definition: aarch64.hpp:5
constexpr with_callback_t with_callback
Definition: thread.hpp:11
Definition: util.hpp:126
Definition: thread.hpp:10
Definition: thread.hpp:14
W work_new
Definition: thread.hpp:18
W work_rest
Definition: thread.hpp:19
constexpr workhint(W w1, W w2)
Definition: thread.hpp:15