mirror of
https://github.com/znc/znc.git
synced 2026-03-28 17:42:41 +01:00
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:
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
#include <znc/Threads.h>
|
||||
#include <znc/ZNCDebug.h>
|
||||
|
||||
#ifdef HAVE_PTHREAD
|
||||
|
||||
@@ -23,6 +24,36 @@ CThreadPool& CThreadPool::Get() {
|
||||
return pool;
|
||||
}
|
||||
|
||||
CThreadPool::CThreadPool() : m_done(false), m_num_threads(0), m_num_idle(0) {
|
||||
if (pipe(m_iJobPipe)) {
|
||||
DEBUG("Ouch, can't open pipe for thread pool: " << strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void CThreadPool::jobDone(const CJob* job) const {
|
||||
// This write() must succeed because POSIX guarantees that writes of
|
||||
// less than PIPE_BUF are atomic (and PIPE_BUF is at least 512).
|
||||
// (Yes, this really wants to write a pointer(!) to the pipe.
|
||||
size_t w = write(m_iJobPipe[1], &job, sizeof(job));
|
||||
if (w != sizeof(job)) {
|
||||
DEBUG("Something bad happened during write() to a pipe for thread pool, wrote " << w << " bytes: " << strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void CThreadPool::handlePipeReadable() const {
|
||||
CJob* a = NULL;
|
||||
ssize_t need = sizeof(a);
|
||||
ssize_t r = read(m_iJobPipe[0], &a, need);
|
||||
if (r != need) {
|
||||
DEBUG("Something bad happened during read() from a pipe for thread pool: " << strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
a->runMain();
|
||||
delete a;
|
||||
}
|
||||
|
||||
CThreadPool::~CThreadPool() {
|
||||
/* Anyone has an idea how this can be done less ugly? */
|
||||
CMutexLocker guard(m_mutex);
|
||||
@@ -64,7 +95,8 @@ void CThreadPool::threadFunc() {
|
||||
m_num_idle--;
|
||||
guard.unlock();
|
||||
|
||||
job->run();
|
||||
job->runThread();
|
||||
jobDone(job);
|
||||
|
||||
guard.lock();
|
||||
m_num_idle++;
|
||||
|
||||
Reference in New Issue
Block a user