]> git.sesse.net Git - cubemap/commitdiff
Wrap the acceptor into the same thread logic as everything else.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Apr 2013 21:45:20 +0000 (23:45 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Apr 2013 21:45:20 +0000 (23:45 +0200)
acceptor.cpp
acceptor.h
main.cpp

index 0b8de507af4d0b5576cb2f9aa309ba6e68519a62..776f39b6c3f54e7e2ad0377aeb7d28c27bfb0d74 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <signal.h>
 #include <errno.h>
 #include <arpa/inet.h>
 #include <sys/ioctl.h>
@@ -59,10 +60,37 @@ int create_server_socket(int port)
 
        return server_sock;
 }
+       
+AcceptorThread::AcceptorThread(int server_sock)
+       : server_sock(server_sock)
+{
+}
+
+void AcceptorThread::run()
+{
+       should_stop = false;
+       pthread_create(&worker_thread, NULL, &AcceptorThread::do_work_thunk, this);
+}
 
-void *acceptor_thread_run(void *arg)
+void AcceptorThread::stop()
+{
+       should_stop = true;
+       pthread_kill(worker_thread, SIGHUP);
+       if (pthread_join(worker_thread, NULL) == -1) {
+               perror("pthread_join");
+               exit(1);
+       }
+}
+
+void *AcceptorThread::do_work_thunk(void *arg)
+{
+       AcceptorThread *acceptor_thread = reinterpret_cast<AcceptorThread *>(arg);
+       acceptor_thread->do_work();
+       return NULL;
+}
+
+void AcceptorThread::do_work()
 {
-       int server_sock = int(intptr_t(arg));
        while (!hupped) {
                // Since we are non-blocking, we need to wait for the right state first.
                // Wait up to 50 ms, then check hupped.
@@ -104,5 +132,4 @@ void *acceptor_thread_run(void *arg)
                // Pick a server, round-robin, and hand over the socket to it.
                servers->add_client(sock);
        }
-       return NULL;
 }
index f173e5b647e68d9c4d8cad2ac64e99b7c4523dd0..5cd579da11381f731aa9d5d46dcda3a3f9113301 100644 (file)
@@ -5,6 +5,22 @@ int create_server_socket(int port);
 
 // A thread that accepts new connections on a given socket,
 // and hands them off to the server pool.
-void *acceptor_thread_run(void *arg);
+class AcceptorThread {
+public:
+       AcceptorThread(int server_sock);
+       void run();
+       void stop();
+
+private:
+       // Recovers the this pointer, and hands over control to do_work().
+       static void *do_work_thunk(void *arg);
+
+       void do_work();
+
+       int server_sock;
+
+       pthread_t worker_thread;
+       volatile bool should_stop;
+};
 
 #endif  // !defined(_ACCEPTOR_H)
index e270141e62941f6c33202d34f15786d62fca9200..79d98394eed49c56dc37b5a7812b039250f60233 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -265,8 +265,8 @@ int main(int argc, char **argv)
 
        servers->run();
 
-       pthread_t acceptor_thread;
-       pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
+       AcceptorThread acceptor_thread(server_sock);
+       acceptor_thread.run();
 
        // Find all streams in the configuration file, and create inputs for them.
        vector<Input *> inputs;
@@ -342,11 +342,7 @@ int main(int argc, char **argv)
        if (stats_thread != NULL) {
                stats_thread->stop();
        }
-       pthread_kill(acceptor_thread, SIGHUP);
-       if (pthread_join(acceptor_thread, NULL) == -1) {
-               perror("pthread_join");
-               exit(1);
-       }
+       acceptor_thread.stop();
 
        CubemapStateProto state;
        state.set_serialize_start_sec(serialize_start.tv_sec);