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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/*
* active port forwarder - software for secure forwarding
* Copyright (C) 2003-2007 jeremian <jeremian [at] poczta.fm>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <config.h>
#include <stdlib.h>
#include <assert.h>
#include "timeval_functions.h"
#include "task_scheduler_struct.h"
/*
* Function name: TaskScheduler_new
* Description: Creates and initializes task scheduler.
* Returns: Pointer to newly created TaskScheduler structure.
*/
TaskScheduler*
TaskScheduler_new()
{
TaskScheduler* tmp = calloc(1, sizeof(TaskScheduler));
assert(tmp != NULL);
if (tmp == NULL) {
return NULL;
}
return tmp;
}
/*
* Function name: TaskScheduler_free
* Description: Frees the memory allocated for TaskScheduler structure.
* Arguments: scheduler - pointer to pointer to TaskScheduler structure
*/
void
TaskScheduler_free(TaskScheduler** scheduler)
{
assert(scheduler != NULL);
if (scheduler == NULL) {
return;
}
assert((*scheduler) != NULL);
if ((*scheduler) == NULL) {
return;
}
while ((*scheduler)->head) {
TaskScheduler_removeTask((*scheduler), TaskListNode_get_task((*scheduler)->head));
}
free((*scheduler));
(*scheduler) = NULL;
}
/*
* Function name: TaskScheduler_get_actualTimer
* Description: Returns the smallest timeval structure in the scheduler.
* Arguments: scheduler - pointer to TaskScheduler structure
* Returns: The smallest timeval structure in the scheduler.
*/
struct timeval*
TaskScheduler_get_actualTimer(TaskScheduler* scheduler)
{
assert(scheduler != NULL);
if (scheduler == NULL) {
return NULL;
}
return Task_get_timer(TaskListNode_get_task(scheduler->actualTask));
}
/*
* Function name: TaskScheduler_hasMoreTasks
* Description: Checks if there is more tasks in the scheduler.
* Arguments: scheduler - pointer to TaskScheduler structure
* Returns: 0 - there is no more tasks in the scheduler,
* n>0 - there is more (n) tasks in the scheduler.
*/
int
TaskScheduler_hasMoreTasks(TaskScheduler* scheduler)
{
assert(scheduler != NULL);
if (scheduler == NULL) {
return 0;
}
return scheduler->numberOfTasks;
}
/*
* Function name: TaskScheduler_addTask
* Description: Adds the task to the scheduler.
* Arguments: scheduler - pointer to TaskScheduler structure
* task - the task to add to the scheduler
* Returns: 0 - the task has been added successfully,
* 1 - the task has NOT been added.
*/
int
TaskScheduler_addTask(TaskScheduler* scheduler, Task* task)
{
TaskListNode* tmp;
assert(scheduler != NULL);
if (scheduler == NULL) {
return 1;
}
assert(task != NULL);
if (task == NULL) {
return 1;
}
tmp = TaskListNode_new(task);
assert(tmp != NULL);
if (tmp == NULL) {
return 1;
}
if (scheduler->head) {
TaskListNode_set_next(tmp, scheduler->head);
TaskListNode_set_previous(scheduler->head, tmp);
}
scheduler->head = tmp;
scheduler->numberOfTasks += 1;
if (scheduler->actualTask) {
if (timeval_compare(TaskScheduler_get_actualTimer(scheduler), Task_get_timer(task)) > 0) {
scheduler->actualTask = tmp;
}
}
else {
scheduler->actualTask = tmp;
}
return 0;
}
/*
* Function name: find_tasknode_with_minimal_timer
* Description: Finds the task list node with minimal timer.
* Arguments: scheduler - pointer to TaskScheduler structure
* Returns: The task list node with minimal timer.
*/
static TaskListNode*
find_tasknode_with_minimal_timer(TaskScheduler* scheduler)
{
TaskListNode* iterator;
TaskListNode* candidate;
assert(scheduler != NULL);
if (scheduler == NULL) {
return NULL;
}
if (scheduler->head == NULL) {
return NULL;
}
candidate = scheduler->head;
iterator = scheduler->head;
iterator = iterator->next;
while (iterator) {
if (timeval_compare(Task_get_timer(TaskListNode_get_task(iterator)),
Task_get_timer(TaskListNode_get_task(candidate))) == -1) {
candidate = iterator;
}
iterator = iterator->next;
}
return candidate;
}
/*
* Function name: TaskScheduler_removeTask
* Description: Removes the task from the scheduler.
* Arguments: scheduler - pointer to TaskScheduler structure
* task - the task to remove from the scheduler
* Returns: 0 - the task has been removed successfully,
* 1 - the task has NOT been removed (probably there was no such task in the scheduler).
*/
int
TaskScheduler_removeTask(TaskScheduler* scheduler, Task* task)
{
TaskListNode* iterator;
Task* tmp;
assert(scheduler != NULL);
if (scheduler == NULL) {
return 1;
}
assert(task != NULL);
if (task == NULL) {
return 1;
}
iterator = scheduler->head;
while (iterator) {
tmp = TaskListNode_get_task(iterator);
if (tmp == task) {
if (iterator->previous) {
TaskListNode_set_next(iterator->previous, iterator->next);
}
if (iterator->next) {
TaskListNode_set_previous(iterator->next, iterator->previous);
}
if (iterator == scheduler->head) {
scheduler->head = iterator->next;
}
if (iterator == scheduler->actualTask) {
scheduler->actualTask = find_tasknode_with_minimal_timer(scheduler);
}
scheduler->numberOfTasks -= 1;
TaskListNode_free(&iterator);
return 0;
}
iterator = iterator->next;
}
return 1;
}
/*
* Function name: TaskScheduler_startWatching
* Description: Starts counting the time for the actual timer.
* Arguments: scheduler - pointer to TaskScheduler structure
* Returns: 0 - success,
* 1 - failure.
*/
int
TaskScheduler_startWatching(TaskScheduler* scheduler)
{
struct timeval* tmp;
assert(scheduler != NULL);
if (scheduler == NULL) {
return 1;
}
tmp = TaskScheduler_get_actualTimer(scheduler);
assert(tmp != NULL);
if (tmp == NULL) {
return 1;
}
scheduler->delta = *tmp;
return 0;
}
/*
* Function name: TaskScheduler_stopWatching
* Description: Stops counting the time for the actual timer.
* Updates all the timers and destroys them if needed.
* Arguments: scheduler - pointer to TaskScheduler structure
* Returns: 0 - success,
* 1 - failure.
*/
int
TaskScheduler_stopWatching(TaskScheduler* scheduler)
{
TaskListNode* iterator;
TaskListNode* actualTask;
Task* backup;
struct timeval* tmp;
int result;
assert(scheduler != NULL);
if (scheduler == NULL) {
return 1;
}
tmp = TaskScheduler_get_actualTimer(scheduler);
assert(tmp != NULL);
if (tmp == NULL) {
return 1;
}
if (timeval_subtract(&(scheduler->delta), tmp)) {
return 1;
}
actualTask = scheduler->actualTask;
assert(scheduler->actualTask != NULL);
iterator = scheduler->head;
while (iterator) {
if (iterator != actualTask) {
result = timeval_subtract(Task_get_timer(TaskListNode_get_task(iterator)), &(scheduler->delta));
assert(result == 0);
}
if (timeval_lq_zero(Task_get_timer(TaskListNode_get_task(iterator)))) {
Task_exec(TaskListNode_get_task(iterator));
backup = TaskListNode_get_task(iterator);
iterator = iterator->next;
TaskScheduler_removeTask(scheduler, backup);
continue;
}
iterator = iterator->next;
}
TaskScheduler_update(scheduler);
return 0;
}
/*
* Function name: TaskScheduler_update
* Description: Updates the actual timer.
* Arguments: scheduler - pointer to TaskScheduler structure
* Returns: 0 - success,
* 1 - failure.
*/
int
TaskScheduler_update(TaskScheduler* scheduler)
{
assert(scheduler != NULL);
if (scheduler == NULL) {
return 1;
}
scheduler->actualTask = find_tasknode_with_minimal_timer(scheduler);
return 0;
}
|