Threaded DNS: Use a thread pool

When a DNS thread is done with its lookup, instead of existing immediately, it
now waits for another DNS lookup to do instead. This avoids the cost of
starting/stopping threads all the time.

To make sure that (for whatever reason) the number of waiting threads doesn't
get too high, threads exit if there are more than two DNS threads idling around
with nothing to do.

Fixes #132.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter
2012-02-14 20:28:28 +01:00
parent ec1202daf3
commit 0bdb18a427
2 changed files with 104 additions and 8 deletions
+17
View File
@@ -129,10 +129,27 @@ private:
int iRes;
addrinfo* aiResult;
};
struct TDNSStatus {
/* mutex which protects this whole struct */
pthread_mutex_t mutex;
/* condition variable for idle threads */
pthread_cond_t cond;
/* When this is true, all threads should exit */
bool done;
/* Total number of running DNS threads */
size_t num_threads;
/* Number of DNS threads which don't have any work */
size_t num_idle;
/* List of pending DNS jobs */
std::list<TDNSArg *> jobs;
};
void StartTDNSThread(TDNSTask* task, bool bBind);
void SetTDNSThreadFinished(TDNSTask* task, bool bBind, addrinfo* aiResult);
void RetrieveTDNSResult();
static void* TDNSThread(void* argument);
static void DoDNS(TDNSArg *arg);
TDNSStatus m_threadStatus;
#endif
protected:
};