11 // A thread class with start/stop and signal functionality.
13 // SIGUSR1 is blocked during execution of do_work(), so that you are guaranteed
14 // to receive it when doing wait_for_activity(), and never elsewhere. This means
15 // that you can test whatever status flags you'd want before calling
16 // wait_for_activity(), and then be sure that it actually returns immediately
17 // if a SIGUSR1 (ie., wakeup()) happened, even if it were sent between your test
18 // and the wait_for_activity() call.
27 // Recovers the this pointer, blocks SIGUSR1, and calls do_work().
28 static void *do_work_thunk(void *arg);
30 virtual void do_work() = 0;
32 // Waits until there is activity of the given type on <fd> (or an error),
33 // or until a wakeup. Returns true if there was actually activity on
34 // the file descriptor.
36 // If fd is -1, wait until a wakeup or timeout.
37 // if timeout_ts is nullptr, there is no timeout.
38 bool wait_for_activity(int fd, short events, const timespec *timeout_ts);
40 // Wait until a wakeup.
41 void wait_for_wakeup(const timespec *timeout_ts) { wait_for_activity(-1, 0, timeout_ts); }
43 // Make wait_for_activity() return. Note that this is a relatively expensive
49 // The signal set as it were before we blocked SIGUSR1.
50 sigset_t sigset_without_usr1_block;
53 pthread_t worker_thread;
55 // Protects should_stop_status.
56 std::mutex should_stop_mutex;
58 // If this is set, the thread should return as soon as possible from do_work().
59 bool should_stop_status;
62 #endif // !defined(_THREAD_H)