]> git.sesse.net Git - cubemap/blobdiff - thread.cpp
Use C++11 std::mutex and std::lock_guard instead of our RAII wrapper.
[cubemap] / thread.cpp
index bd4cfd1790288e6dcb712820b3bc6725b9548732..d85be706ddf5e32003bad8105b1121edcf9ab315 100644 (file)
@@ -1,6 +1,5 @@
 #include <assert.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <poll.h>
 #include <signal.h>
 #include <stdio.h>
@@ -8,26 +7,26 @@
 #include <unistd.h>
 
 #include "log.h"
-#include "mutexlock.h"
 #include "thread.h"
 
+using namespace std;
+
 Thread::~Thread() {}
 
 void Thread::run()
 {
-       pthread_mutex_init(&should_stop_mutex, NULL);
        should_stop_status = false;
-       pthread_create(&worker_thread, NULL, &Thread::do_work_thunk, this);
+       pthread_create(&worker_thread, nullptr, &Thread::do_work_thunk, this);
 }
 
 void Thread::stop()
 {
        {
-               MutexLock lock(&should_stop_mutex);
+               lock_guard<mutex> lock(should_stop_mutex);
                should_stop_status = true;
        }
        wakeup();
-       if (pthread_join(worker_thread, NULL) == -1) {
+       if (pthread_join(worker_thread, nullptr) == -1) {
                log_perror("pthread_join");
                exit(1);
        }
@@ -41,8 +40,9 @@ void *Thread::do_work_thunk(void *arg)
        // (This isn't strictly required, but it makes it easier to debug that indeed
        // SIGUSR1 was what woke us up.)
        sigset_t set;
+       sigemptyset(&set);
        sigaddset(&set, SIGHUP);
-       int err = pthread_sigmask(SIG_BLOCK, &set, NULL);
+       int err = pthread_sigmask(SIG_BLOCK, &set, nullptr);
        if (err != 0) {
                errno = err;
                log_perror("pthread_sigmask");
@@ -61,7 +61,7 @@ void *Thread::do_work_thunk(void *arg)
 
        // Call the right thunk.
        thread->do_work();
-       return NULL;
+       return nullptr;
 }
 
 bool Thread::wait_for_activity(int fd, short events, const struct timespec *timeout_ts)
@@ -92,6 +92,6 @@ void Thread::wakeup()
 
 bool Thread::should_stop()
 {
-       MutexLock lock(&should_stop_mutex);
+       lock_guard<mutex> lock(should_stop_mutex);
        return should_stop_status;
 }