Fixed 1 second delay for child processes.

This commit is contained in:
Gunnar Beutner 2013-02-10 01:35:40 +01:00
parent ee37e0cace
commit fc6df0ecbd
2 changed files with 89 additions and 66 deletions

View File

@ -24,7 +24,7 @@ using namespace icinga;
bool Process::m_ThreadCreated = false;
boost::mutex Process::m_Mutex;
deque<Process::Ptr> Process::m_Tasks;
condition_variable Process::m_TasksCV;
int Process::m_TaskFd;
Process::Process(const String& command, const Dictionary::Ptr& environment)
: AsyncTask<Process, ProcessResult>(), m_Command(command),
@ -33,7 +33,14 @@ Process::Process(const String& command, const Dictionary::Ptr& environment)
assert(Application::IsMainThread());
if (!m_ThreadCreated) {
thread t(&Process::WorkerThreadProc);
int fds[2];
if (pipe(fds) < 0)
BOOST_THROW_EXCEPTION(PosixException("pipe() failed.", errno));
m_TaskFd = fds[1];
thread t(&Process::WorkerThreadProc, fds[0]);
t.detach();
m_ThreadCreated = true;
@ -42,21 +49,24 @@ Process::Process(const String& command, const Dictionary::Ptr& environment)
void Process::Run(void)
{
boost::mutex::scoped_lock lock(m_Mutex);
m_Tasks.push_back(GetSelf());
m_TasksCV.notify_all();
}
void Process::WorkerThreadProc(void)
{
boost::mutex::scoped_lock lock(m_Mutex);
m_Tasks.push_back(GetSelf());
}
/**
* This little gem which is commonly known as the "self-pipe trick"
* takes care of waking up the select() call in the worker thread.
*/
if (write(m_TaskFd, "T", 1) < 0)
BOOST_THROW_EXCEPTION(PosixException("write() failed.", errno));
}
void Process::WorkerThreadProc(int taskFd)
{
map<int, Process::Ptr> tasks;
for (;;) {
while (m_Tasks.empty() || tasks.size() >= MaxTasksPerThread) {
lock.unlock();
map<int, Process::Ptr>::iterator it, prev;
#ifndef _MSC_VER
@ -64,6 +74,10 @@ void Process::WorkerThreadProc(void)
int nfds = 0;
FD_ZERO(&readfds);
FD_SET(taskFd, &readfds);
if (taskFd > nfds)
nfds = taskFd;
int fd;
BOOST_FOREACH(tie(fd, tuples::ignore), tasks) {
@ -81,6 +95,37 @@ void Process::WorkerThreadProc(void)
Utility::Sleep(1);
#endif /* _MSC_VER */
if (FD_ISSET(taskFd, &readfds)) {
/* clear pipe */
char buffer[512];
int rc = read(taskFd, buffer, sizeof(buffer));
assert(rc >= 1);
while (tasks.size() < MaxTasksPerThread) {
Process::Ptr task;
{
boost::mutex::scoped_lock lock(m_Mutex);
if (m_Tasks.empty())
break;
task = m_Tasks.front();
m_Tasks.pop_front();
}
try {
task->InitTask();
int fd = task->GetFD();
if (fd >= 0)
tasks[fd] = task;
} catch (...) {
Event::Post(boost::bind(&Process::FinishException, task, boost::current_exception()));
}
}
}
for (it = tasks.begin(); it != tasks.end(); ) {
int fd = it->first;
Process::Ptr task = it->second;
@ -102,28 +147,6 @@ void Process::WorkerThreadProc(void)
it++;
}
}
lock.lock();
}
while (!m_Tasks.empty() && tasks.size() < MaxTasksPerThread) {
Process::Ptr task = m_Tasks.front();
m_Tasks.pop_front();
lock.unlock();
try {
task->InitTask();
int fd = task->GetFD();
if (fd >= 0)
tasks[fd] = task;
} catch (...) {
Event::Post(boost::bind(&Process::FinishException, task, boost::current_exception()));
}
lock.lock();
}
}
}

View File

@ -71,9 +71,9 @@ private:
static boost::mutex m_Mutex;
static deque<Process::Ptr> m_Tasks;
static condition_variable m_TasksCV;
static int m_TaskFd;
static void WorkerThreadProc(void);
static void WorkerThreadProc(int taskFd);
void InitTask(void);
bool RunTask(void);