Zero  0.1.0
worker_thread.h
Go to the documentation of this file.
1 #ifndef __WORKER_THREAD_H
2 #define __WORKER_THREAD_H
3 
4 #include "thread_wrapper.h"
5 
6 #include <algorithm>
7 #include <chrono>
8 #include <mutex>
9 #include <condition_variable>
10 #include <atomic>
11 
13 public:
14  worker_thread_t(int inverval_ms = -1);
15 
16  virtual ~worker_thread_t();
17 
30  void wakeup(bool wait = false, int rounds_to_wait = -1);
31 
35  void stop();
36 
42  void wait_for_round(long round = 0);
43 
44  long get_rounds_completed() const {
45  return rounds_completed;
46  };
47 
48  bool is_busy() const {
49  return worker_busy;
50  }
51 
52 protected:
53 
57  virtual void do_work() = 0;
58 
63  bool should_exit() const {
64  return stop_requested;
65  }
66 
71  void notify_one();
72 
73  void notify_all();
74 
75  void quit();
76 
77 private:
78  virtual void run();
79 
80  std::mutex cond_mutex;
81 
82  std::condition_variable wakeup_condvar;
83 
84  std::condition_variable done_condvar;
85 
93 
95  std::atomic<bool> stop_requested;
96 
99 
102 
105 };
106 
111 public:
112  log_worker_thread_t(int interval_ms = -1)
113  : worker_thread_t(interval_ms),
114  endLSN(lsn_t::null) {}
115 
116  virtual ~log_worker_thread_t() {}
117 
118  void wakeup_until_lsn(lsn_t lsn, bool wait = false, int rounds_to_wait = -1);
119 
121  return endLSN;
122  }
123 
124 private:
125  std::atomic<lsn_t> endLSN;
126 };
127 
128 #endif // __WORKER_THREAD_H
Definition: worker_thread.h:12
bool is_busy() const
Definition: worker_thread.h:48
virtual ~worker_thread_t()
Definition: worker_thread.cpp:11
Definition: thread_wrapper.h:16
std::mutex cond_mutex
Definition: worker_thread.h:80
int interval_msec
Definition: worker_thread.h:92
bool worker_busy
Definition: worker_thread.h:101
long get_rounds_completed() const
Definition: worker_thread.h:44
void notify_one()
Definition: worker_thread.cpp:101
virtual ~log_worker_thread_t()
Definition: worker_thread.h:116
void notify_all()
Definition: worker_thread.cpp:106
bool wakeup_requested
Definition: worker_thread.h:98
virtual void do_work()=0
Log Sequence Number. See Log Sequence Numbers (LSN).
Definition: lsn.h:243
std::condition_variable wakeup_condvar
Definition: worker_thread.h:82
std::atomic< bool > stop_requested
Definition: worker_thread.h:95
long rounds_completed
Definition: worker_thread.h:104
std::atomic< lsn_t > endLSN
Definition: worker_thread.h:125
void wait_for_round(long round=0)
Definition: worker_thread.cpp:36
void wakeup(bool wait=false, int rounds_to_wait=-1)
Definition: worker_thread.cpp:13
lsn_t getEndLSN()
Definition: worker_thread.h:120
std::condition_variable done_condvar
Definition: worker_thread.h:84
void stop()
Definition: worker_thread.cpp:50
log_worker_thread_t(int interval_ms=-1)
Definition: worker_thread.h:112
worker_thread_t(int inverval_ms=-1)
Definition: worker_thread.cpp:3
virtual void run()
Definition: worker_thread.cpp:60
void quit()
Definition: worker_thread.cpp:56
Definition: worker_thread.h:110
bool should_exit() const
Definition: worker_thread.h:63