11 #include "mutexlock.h"
18 pthread_mutex_init(&should_stop_mutex, NULL);
19 should_stop_status = false;
20 pthread_create(&worker_thread, NULL, &Thread::do_work_thunk, this);
26 MutexLock lock(&should_stop_mutex);
27 should_stop_status = true;
30 if (pthread_join(worker_thread, NULL) == -1) {
31 log_perror("pthread_join");
36 void *Thread::do_work_thunk(void *arg)
38 Thread *thread = reinterpret_cast<Thread *>(arg);
40 // Block SIGHUP; only the main thread should see that.
41 // (This isn't strictly required, but it makes it easier to debug that indeed
42 // SIGUSR1 was what woke us up.)
45 sigaddset(&set, SIGHUP);
46 int err = pthread_sigmask(SIG_BLOCK, &set, NULL);
49 log_perror("pthread_sigmask");
53 // Block SIGUSR1, and store the old signal mask.
55 sigaddset(&set, SIGUSR1);
56 err = pthread_sigmask(SIG_BLOCK, &set, &thread->sigset_without_usr1_block);
59 log_perror("pthread_sigmask");
63 // Call the right thunk.
68 bool Thread::wait_for_activity(int fd, short events, const struct timespec *timeout_ts)
75 int nfds = ppoll(&pfd, (fd == -1) ? 0 : 1, timeout_ts, &sigset_without_usr1_block);
76 if (nfds == -1 && errno == EINTR) {
91 pthread_kill(worker_thread, SIGUSR1);
94 bool Thread::should_stop()
96 MutexLock lock(&should_stop_mutex);
97 return should_stop_status;