]> git.sesse.net Git - cubemap/blobdiff - thread.cpp
Identify UDPInput error messages by the stream, too.
[cubemap] / thread.cpp
index 7aded3bb8cda25790550a3818a4b3a40ff205496..f719eacb95c5379a2d664f23c49c04e0b50b767b 100644 (file)
@@ -1,7 +1,11 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
 #include <signal.h>
+#include <errno.h>
 
+#include "log.h"
 #include "thread.h"
        
 Thread::~Thread() {}
@@ -9,17 +13,53 @@ Thread::~Thread() {}
 void Thread::run()
 {
        should_stop = false;
+       int pipefd[2];
+       if (pipe2(pipefd, O_CLOEXEC) == -1) {
+               log_perror("pipe");
+               exit(1);
+       }
+       stop_fd_read = pipefd[0];
+       stop_fd_write = pipefd[1];
        pthread_create(&worker_thread, NULL, &Thread::do_work_thunk, this);
 }
 
 void Thread::stop()
 {
        should_stop = true;
+       char ch = 0;
+       int err;
+       do {
+               err = write(stop_fd_write, &ch, 1);
+       } while (err == -1 && errno == EINTR);
+
+       if (err == -1) {
+               log_perror("write");
+               exit(1);
+       }
+
+       do {
+               err = close(stop_fd_write);
+       } while (err == -1 && errno == EINTR);
+
+       if (err == -1) {
+               log_perror("close");
+               // Can continue (we have close-on-exec).
+       }
+
        pthread_kill(worker_thread, SIGHUP);
        if (pthread_join(worker_thread, NULL) == -1) {
-               perror("pthread_join");
+               log_perror("pthread_join");
                exit(1);
        }
+       
+       do {
+               err = close(stop_fd_read);
+       } while (err == -1 && errno == EINTR);
+
+       if (err == -1) {
+               log_perror("close");
+               // Can continue (we have close-on-exec).
+       }
 }
 
 void *Thread::do_work_thunk(void *arg)