X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=acceptor.cpp;h=776f39b6c3f54e7e2ad0377aeb7d28c27bfb0d74;hp=0b8de507af4d0b5576cb2f9aa309ba6e68519a62;hb=156897c8a6c7b86cb244d74911528de5c28f2134;hpb=3f82e5f8ede16a712aa0bcd0913d2d8e3689742c diff --git a/acceptor.cpp b/acceptor.cpp index 0b8de50..776f39b 100644 --- a/acceptor.cpp +++ b/acceptor.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -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(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; }