18 should_stop_status = false;
19 pthread_create(&worker_thread, nullptr, &Thread::do_work_thunk, this);
25 lock_guard<mutex> lock(should_stop_mutex);
26 should_stop_status = true;
29 if (pthread_join(worker_thread, nullptr) == -1) {
30 log_perror("pthread_join");
35 void *Thread::do_work_thunk(void *arg)
37 Thread *thread = reinterpret_cast<Thread *>(arg);
39 // Block SIGHUP; only the main thread should see that.
40 // (This isn't strictly required, but it makes it easier to debug that indeed
41 // SIGUSR1 was what woke us up.)
44 sigaddset(&set, SIGHUP);
45 int err = pthread_sigmask(SIG_BLOCK, &set, nullptr);
48 log_perror("pthread_sigmask");
52 // Block SIGUSR1, and store the old signal mask.
54 sigaddset(&set, SIGUSR1);
55 err = pthread_sigmask(SIG_BLOCK, &set, &thread->sigset_without_usr1_block);
58 log_perror("pthread_sigmask");
62 // Call the right thunk.
67 bool Thread::wait_for_activity(int fd, short events, const struct timespec *timeout_ts)
74 int nfds = ppoll(&pfd, (fd == -1) ? 0 : 1, timeout_ts, &sigset_without_usr1_block);
75 if (nfds == -1 && errno == EINTR) {
90 pthread_kill(worker_thread, SIGUSR1);
93 bool Thread::should_stop()
95 lock_guard<mutex> lock(should_stop_mutex);
96 return should_stop_status;