From d3856992710194330bae59210c4ca6154259ecdc Mon Sep 17 00:00:00 2001 From: Treeki Date: Wed, 12 Feb 2014 13:27:17 +0100 Subject: stop DNS thread cleanly when exiting --- bouncer/dns.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'bouncer/dns.cpp') diff --git a/bouncer/dns.cpp b/bouncer/dns.cpp index 056bb09..f5bb75b 100644 --- a/bouncer/dns.cpp +++ b/bouncer/dns.cpp @@ -22,6 +22,7 @@ struct DNSQuery { }; static DNSQuery dnsQueue[DNS_QUERY_COUNT]; +static bool dnsQuit = false; static pthread_t dnsThread; static pthread_mutex_t dnsQueueMutex; static pthread_cond_t dnsQueueCond; @@ -41,6 +42,16 @@ void DNS::start() { } } +void DNS::stop() { + pthread_mutex_lock(&dnsQueueMutex); + dnsQuit = true; + pthread_mutex_unlock(&dnsQueueMutex); + pthread_cond_signal(&dnsQueueCond); + + pthread_join(dnsThread, NULL); + +} + int DNS::makeQuery(const char *name) { int id = -1; @@ -102,7 +113,9 @@ bool DNS::checkQuery(int id, in_addr *pResult, bool *pIsError) { void *dnsThreadProc(void *) { pthread_mutex_lock(&dnsQueueMutex); - for (;;) { + dnsQuit = false; + + while (!dnsQuit) { for (int i = 0; i < DNS_QUERY_COUNT; i++) { if (dnsQueue[i].status == DQS_WAITING) { char nameCopy[DNS_QUERY_NAME_SIZE]; -- cgit v1.2.3