10 #include "mutexlock.h"
17 pthread_mutex_init(&should_stop_mutex, NULL);
18 should_stop_status = false;
19 pthread_create(&worker_thread, NULL, &Thread::do_work_thunk, this);
25 MutexLock lock(&should_stop_mutex);
26 should_stop_status = true;
29 if (pthread_join(worker_thread, NULL) == -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.)
43 sigaddset(&set, SIGHUP);
44 int err = pthread_sigmask(SIG_BLOCK, &set, NULL);
47 log_perror("pthread_sigmask");
51 // Block SIGUSR1, and store the old signal mask.
53 sigaddset(&set, SIGUSR1);
54 err = pthread_sigmask(SIG_BLOCK, &set, &thread->sigset_without_usr1_block);
57 log_perror("pthread_sigmask");
61 // Call the right thunk.
66 bool Thread::wait_for_activity(int fd, short events, const struct timespec *timeout_ts)
73 int nfds = ppoll(&pfd, (fd == -1) ? 0 : 1, timeout_ts, &sigset_without_usr1_block);
74 if (nfds == -1 && errno == EINTR) {
89 pthread_kill(worker_thread, SIGUSR1);
92 bool Thread::should_stop()
94 MutexLock lock(&should_stop_mutex);
95 return should_stop_status;