summaryrefslogtreecommitdiff
path: root/lib/TaskMan.cc
blob: 1af92348048c76e424a0c70a984e18727208a674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <signal.h>
#include <wait.h>
#include <vector.h>
#include "TaskMan.h"

vector<Task *> TaskMan::TaskList;
vector<Task *> TaskMan::Zombies;
int TaskMan::number = 0;
bool TaskMan::inited = false;

static int got_sigchild = 0;
static vector<pid_t> 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::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

}