-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_sched.c
86 lines (66 loc) · 1.5 KB
/
c_sched.c
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
#include "c_sched.h"
#include "p_engine.h"
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
sched_t * sched_ctor (void)
{
sched_t * self = (sched_t *) malloc(sizeof(struct sched_s));
__GUARD(self, "sched_t obj");
// self->tasks = NULL;
self->align = 0;
self->syncro = time(NULL) + 1;
int i;
for (i = 0; i < SCHED_SIZE; i++)
self->busy[i] = false;
return self;
}
void sched_dtor (sched_t * self)
{
// task_t * ptr;
// task_t * nxt;
__UNGUARD(self, "sched_t obj");
free(self);
}
void sched_update (sched_t * self, engine_t * engine)
{
if (time(NULL) < self->syncro)
return;
self->align = 0;
int throthle = 0;
int i;
for (i = 0; i < SCHED_SIZE; i++)
{
if (self->busy[i] != true)
continue;
task_t * ptr = &self->tasks[i];
ptr->delay -= 1 / engine->delta;
if (ptr->delay <= 0)
{
ptr->funct(self, ptr->ptr, ptr->val);
self->busy[i] = false;
throthle++;
if (throthle == SCHED_MAX_THROTHLE)
return;
}
}
}
void yield (sched_t * self, void (*funct)( sched_t * sched, void * ptr, int val ), void * ptr, int val, float delay )
{
// find any free spot
int i;
for (i = 0; i < SCHED_SIZE; i++)
if (self->busy[i] == false)
break;
if (i >= SCHED_SIZE)
{
printf("[sched] out of space exception\n");
return;
}
task_t * task = &self->tasks[i];
task->delay = delay + self->align++;
task->funct = funct;
task->ptr = ptr;
task->val = val;
self->busy[i] = true; // make it happen
}