#include #include #include #include "TaskMan.h" TaskMan::TaskList_t TaskMan::TaskList; TaskMan::TaskList_t TaskMan::Zombies; vector TaskMan::w4ha; vector TaskMan::w4pr; vector TaskMan::w4to; int TaskMan::number = 0; bool TaskMan::inited = false; static int got_sigchild = 0; static vector process; static int nbprocess = 0; void taskman_sigchild(int sig) { got_sigchild = 1; process.push_back(wait(0)); signal(SIGCHLD, taskman_sigchild); nbprocess++; } void TaskMan::Init() throw (GeneralException) { if (inited) { throw GeneralException("Task Manager already initialised."); } signal(SIGCHLD, taskman_sigchild); inited = true; number = 0; } int TaskMan::AddTask(Task * t) { if (!inited) { Init(); } TaskList.push_back(t); number++; return 0; } void TaskMan::WaitFor(Handle * h, Task * t, int flags) { w4ha.push_back(w4ha_t(h, flags, t)); } void TaskMan::WaitFor(pid_t p, Task * t) { w4pr.push_back(w4pr_t(p, t)); } void TaskMan::WaitFor(timeval t, Task * T, int flags) { w4to.push_back(w4to_t(t, flags, T)); } void TaskMan::MainLoop() throw (GeneralException) { TaskList_t::iterator p; Task * t; int no_burst; if (!inited) { Init(); } while (1) { cerr << "TaskMan: Begin of main loop with " << number << " task to handle.\n"; if (number == 0) { throw GeneralException("TaskMan: No more task to manage."); } no_burst = 0; while (!no_burst) { no_burst = 1; /* First, we will check for any burning task and run 'em */ for (p = TaskList.begin(); p && (p != TaskList.end()); p++) { t = *p; if (t->GetState() == TASK_BURST) { t->Run(); } if (t->GetState() == TASK_BURST) { no_burst = 0; } else if (t->GetState() == TASK_DONE) { TaskList.erase(p); number--; p--; if (t->HasToClean()) { delete t; } else { Zombies.push_back(t); } } } } } #ifdef HAVE_POLL #else #endif }