Fixed deadlock in Process::WorkerThreadProc.

This commit is contained in:
Gunnar Beutner 2013-02-10 21:53:08 +01:00
parent 67aef452e5
commit aa2322abbb
1 changed files with 25 additions and 10 deletions

View File

@ -136,7 +136,9 @@ void Process::WorkerThreadProc(int taskFd)
int nfds = 0; int nfds = 0;
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(taskFd, &readfds);
if (tasks.size() < MaxTasksPerThread)
FD_SET(taskFd, &readfds);
if (taskFd > nfds) if (taskFd > nfds)
nfds = taskFd; nfds = taskFd;
@ -149,23 +151,36 @@ void Process::WorkerThreadProc(int taskFd)
FD_SET(fd, &readfds); FD_SET(fd, &readfds);
} }
timeval tv; int rc = select(nfds + 1, &readfds, NULL, NULL, NULL);
tv.tv_sec = 1;
tv.tv_usec = 0; if (rc < 0 && errno != EINTR)
select(nfds + 1, &readfds, NULL, NULL, &tv); BOOST_THROW_EXCEPTION(PosixException("select() failed.", errno));
if (rc == 0)
continue;
#else /* _MSC_VER */ #else /* _MSC_VER */
Utility::Sleep(1); Utility::Sleep(1);
#endif /* _MSC_VER */ #endif /* _MSC_VER */
#ifndef _MSC_VER #ifndef _MSC_VER
if (FD_ISSET(taskFd, &readfds)) { if (FD_ISSET(taskFd, &readfds)) {
/* clear pipe */ #endif /* _MSC_VER */
char buffer[512]; /* Figure out how many tasks we'd ideally want. */
int rc = read(taskFd, buffer, sizeof(buffer)); int tasknum = MaxTasksPerThread - tasks.size();
assert(rc >= 1);
#ifndef _MSC_VER
/* Read one byte for every task we take from the pending tasks list. */
char buffer[MaxTasksPerThread];
tasknum = read(taskFd, &buffer, tasknum);
if (tasknum < 0)
BOOST_THROW_EXCEPTION(PosixException("read() failed.", errno));
assert(tasknum >= 1);
#endif /* _MSC_VER */ #endif /* _MSC_VER */
while (tasks.size() < MaxTasksPerThread) { for (int i = 0; i < tasknum; i++) {
Process::Ptr task; Process::Ptr task;
{ {