#include #include #include #include "TaskMan.h" 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(NULL)); signal(SIGCHLD, taskman_sigchild); nbprocess++; } TaskMan::TaskMan() throw (GeneralException) { throw GeneralException("You can't instanciate a Task Manager."); } 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) { TaskList.push_back(t); number++; return 0; } int TaskMan::RemoveTask(Task * t) { int i; for (i = 0; i < number; i++) { if (TaskList[i] == t) { TaskList.erase(&TaskList[i]); number--; return 0; } } return -1; } void TaskMan::MainLoop() throw (GeneralException) { Task ** p, * t; while (1) { if (number == 0) { throw GeneralException("TaskMan: No more task to manage."); } p = TaskList.begin(); while (1) { t = *p; #ifdef HAVE_POLL #else #endif try { t->Do(); } catch (TaskSwitch) { continue; } if (t->GetState() == TASK_DONE) { TaskList.erase(p); } if (p == TaskList.end()) { break; } p++; } } }