worker_progress_thread.h
1 
5 #pragma once
6 
7 #include <atomic>
8 #include <cstdint>
9 #include <functional>
10 #include <memory>
11 #include <mutex>
12 #include <thread>
13 
14 #include <ucxx/delayed_submission.h>
15 
16 namespace ucxx {
17 
23 typedef std::function<void(void)> SignalWorkerFunction;
24 
31 typedef std::function<void(void*)> ProgressThreadStartCallback;
32 
40 
51  public:
55  struct StopConfig {
56  uint64_t callbackTimeoutNs;
57  uint64_t signalIntervalNs;
58 
67  explicit StopConfig(uint64_t callbackTimeoutNs = 3000000000,
68  uint64_t signalIntervalNs = 100000000)
70  {
71  }
72  };
73 
74  private:
75  std::thread _thread{};
76  std::shared_ptr<std::atomic<bool>> _stop{
77  std::make_shared<std::atomic<bool>>(false)};
78  bool _pollingMode{false};
79  SignalWorkerFunction _signalWorkerFunction{
80  nullptr};
82  ProgressThreadStartCallback _startCallback{
83  nullptr};
84  ProgressThreadStartCallbackArg _startCallbackArg{
85  nullptr};
86  std::shared_ptr<DelayedSubmissionCollection> _delayedSubmissionCollection{
87  nullptr};
88 
110  static void progressUntilSync(
111  std::function<bool(void)> progressFunction,
112  std::shared_ptr<std::atomic<bool>> stop,
113  std::function<void(void)> setThreadId,
114  ProgressThreadStartCallback startCallback,
115  ProgressThreadStartCallbackArg startCallbackArg,
116  std::shared_ptr<DelayedSubmissionCollection> delayedSubmissionCollection);
117 
118  public:
119  WorkerProgressThread() = default;
120  WorkerProgressThread(const WorkerProgressThread&) = delete;
121  WorkerProgressThread& operator=(WorkerProgressThread const&) = delete;
122 
132 
143 
182  std::function<bool(void)> progressFunction,
183  std::function<void(void)> signalWorkerFunction,
184  std::function<void(void)> setThreadId,
185  ProgressThreadStartCallback startCallback,
186  ProgressThreadStartCallbackArg startCallbackArg,
187  std::shared_ptr<DelayedSubmissionCollection> delayedSubmissionCollection);
188 
195 
201  [[nodiscard]] bool pollingMode() const;
202 
208  [[nodiscard]] std::thread::id getId() const;
209 
215  [[nodiscard]] bool isRunning() const;
216 
224  void stop(StopConfig stopConfig = StopConfig{});
225 };
226 
227 } // namespace ucxx
A thread to progress a ucxx::Worker.
Definition: worker_progress_thread.h:50
WorkerProgressThread(const bool pollingMode, std::function< bool(void)> progressFunction, std::function< void(void)> signalWorkerFunction, std::function< void(void)> setThreadId, ProgressThreadStartCallback startCallback, ProgressThreadStartCallbackArg startCallbackArg, std::shared_ptr< DelayedSubmissionCollection > delayedSubmissionCollection)
Constructor of WorkerProgressThread.
std::thread::id getId() const
Returns the ID of the progress thread.
~WorkerProgressThread()
ucxx::WorkerProgressThread destructor.
bool isRunning() const
Returns whether the thread is running.
void stop(StopConfig stopConfig=StopConfig{})
Stop the progress thread.
WorkerProgressThread(WorkerProgressThread &&o)=default
Move constructor of WorkerProgressThread.
WorkerProgressThread & operator=(WorkerProgressThread &&o)=default
Move assignment operator of WorkerProgressThread.
bool pollingMode() const
Returns whether the thread was created for polling progress mode.
Definition: address.h:16
std::function< void(void *)> ProgressThreadStartCallback
A user-defined function to execute at the start of the progress thread.
Definition: worker_progress_thread.h:31
void * ProgressThreadStartCallbackArg
Data for the user-defined function provided to progress thread start callback.
Definition: worker_progress_thread.h:39
std::function< void(void)> SignalWorkerFunction
A user-defined function used to wake the worker.
Definition: worker_progress_thread.h:23
Progress thread stop synchronization configuration.
Definition: worker_progress_thread.h:55
uint64_t signalIntervalNs
Interval for waking the worker while waiting.
Definition: worker_progress_thread.h:57
StopConfig(uint64_t callbackTimeoutNs=3000000000, uint64_t signalIntervalNs=100000000)
Create stop synchronization configuration.
Definition: worker_progress_thread.h:67
uint64_t callbackTimeoutNs
Maximum time to wait for each stop callback.
Definition: worker_progress_thread.h:56