CJob: Add a way to do stuff on the main thread

This just moves the pipe from the socket code to the thread pool. However, now
all CJobs can use this and there is a single place for them to get deleted.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter
2012-11-16 17:54:32 +01:00
parent 75f2e3fa41
commit 53c579b296
4 changed files with 77 additions and 60 deletions
+9 -11
View File
@@ -102,11 +102,9 @@ public:
private:
void FinishConnect(const CString& sHostname, u_short iPort, const CString& sSockName, int iTimeout, bool bSSL, const CString& sBindHost, CZNCSock *pcSock);
#ifdef HAVE_THREADED_DNS
int m_iTDNSpipe[2];
class CTDNSMonitorFD;
friend class CTDNSMonitorFD;
#ifdef HAVE_THREADED_DNS
struct TDNSTask {
CString sHostname;
u_short iPort;
@@ -123,19 +121,19 @@ private:
};
class CDNSJob : public CJob {
public:
CString sHostname;
TDNSTask* task;
int fd;
bool bBind;
CString sHostname;
TDNSTask* task;
CSockManager* pManager;
bool bBind;
int iRes;
addrinfo* aiResult;
int iRes;
addrinfo* aiResult;
void run();
void runThread();
void runMain();
};
void StartTDNSThread(TDNSTask* task, bool bBind);
void SetTDNSThreadFinished(TDNSTask* task, bool bBind, addrinfo* aiResult);
void RetrieveTDNSResult();
static void* TDNSThread(void* argument);
#endif
protected:
+15 -4
View File
@@ -182,14 +182,13 @@ public:
class CJob {
public:
virtual ~CJob() {}
virtual void run() = 0;
virtual void runThread() = 0;
virtual void runMain() = 0;
};
class CThreadPool {
private:
CThreadPool() : m_done(false), m_num_threads(0), m_num_idle(0) {
}
CThreadPool();
~CThreadPool();
public:
@@ -197,7 +196,15 @@ public:
void addJob(CJob *job);
int getReadFD() const {
return m_iJobPipe[0];
}
void handlePipeReadable() const;
private:
void jobDone(const CJob* pJob) const;
// Check if the calling thread is still needed, must be called with m_mutex held
bool threadNeeded() const;
@@ -223,6 +230,10 @@ private:
// number of idle threads waiting on the condition variable
size_t m_num_idle;
// pipe for waking up the main thread
int m_iJobPipe[2];
// list of pending jobs
std::list<CJob *> m_jobs;
};