Statistics
| Branch: | Tag: | Revision:

sssimulator / EventScheduler / event_scheduler.c @ f1158e5c

History | View | Annotate | Download (2.9 KB)

1
/*
2
 * this is sssim: the simple & stupid simulator
3
 *
4
 *  copyright (c) 2015 luca baldesi
5
 *
6
 *  this is free software; see gpl-3.0.txt
7
 */
8

    
9
#include "event_scheduler.h"
10
#include "action_event_struct.h"
11

    
12
#define DEFAULT_EVENT_SCHEDULER_SIZE 100
13

    
14
struct event_scheduler {
15
        struct ord_array * queue;
16
        double time;
17
};
18

    
19
int cmp_event(const void * e1, const void * e2)
20
{
21
        struct action_event *ae1, *ae2;
22

    
23
        ae1 = (struct action_event *) e1;
24
        ae2 = (struct action_event *) e2;
25

    
26
        if((ae1->time) == (ae2->time))
27
                return ae2->priority - ae1->priority;
28
        return ((ae1->time) > (ae2->time)) ? 1 : -1;
29
}
30

    
31
int event_scheduler_init(struct event_scheduler * es, const uint32_t size)
32
{
33
        int res = 0;
34

    
35
        es->time = 0;
36
        es->queue = size ? ord_array_new(size, cmp_event) : ord_array_new(DEFAULT_EVENT_SCHEDULER_SIZE, cmp_event);
37
        if (es->queue == NULL)
38
                res = -1;
39

    
40
        return res;
41
}
42

    
43
struct event_scheduler * event_scheduler_new(const uint32_t size)
44
{
45
        struct event_scheduler * es = NULL;
46

    
47
        es = malloc(sizeof(struct event_scheduler));
48
        if(event_scheduler_init(es, size) != 0)
49
        {
50
                event_scheduler_destroy(&es);
51
                es = NULL;
52
        }
53

    
54
        return es;
55
}
56

    
57
void event_scheduler_destroy(struct event_scheduler **es)
58
{
59
//        struct action_event ** ae_ptr;
60

    
61
        if (es && (*es))
62
        {
63
                if((*es)->queue)
64
                {
65
//                        The responsibility of destroying event objects is up to the caller
66
//                        while(ord_array_length((*es)->queue))
67
//                        {
68
//                                ae_ptr = ((struct action_event **)  ord_array_pop((*es)->queue));
69
//                                if (*ae_ptr)
70
//                                        action_event_destroy(ae_ptr);
71
//                        }
72
                        ord_array_destroy(&((*es)->queue));
73
                }
74
                free(*es);
75
                *es = NULL;
76
        }
77
}
78

    
79
int event_scheduler_insert(struct event_scheduler *es, struct action_event * e, double interval, uint8_t priority)
80
{
81
        if(es && e && interval >= 0)
82
        {
83
                e->time = es->time + interval;
84
                e->priority = priority;
85
                return ord_array_insert(es->queue, (void *) e);
86
        }
87
        return -1;
88
}
89

    
90
struct action_event * event_scheduler_pop(struct event_scheduler *es)
91
{
92
        struct action_event * ev = NULL;
93

    
94
        if(es)
95
        {
96
                ev = ((struct action_event *)  ord_array_pop(es->queue));
97
                if(ev)
98
                {
99
                        if((ev->time) >= es->time)
100
                                es->time = (ev->time);
101
                        else
102
                                fprintf(stderr,"[ERROR] time goes backward!!\n");
103
                }
104
        }
105
        return ev;
106
}
107

    
108
// this function is deprecated since the responsibility of destroying 
109
// event objects is up to the caller.
110
// Hence, it is not more part of the public interface but it is left
111
// here for historical reason. (I don't want to admit it is useless)
112
int event_scheduler_trigger_next(struct event_scheduler *es)
113
{
114
        struct action_event * ae_ptr;
115
        int res = -1;
116

    
117
        ae_ptr = ((struct action_event *)  ord_array_pop(es->queue));
118
        if(ae_ptr)
119
        {
120
                res = action_event_trigger(ae_ptr);
121
                action_event_destroy(&ae_ptr);
122
        }
123

    
124
        return res;
125
}
126

    
127
double event_scheduler_elapsed_time(const struct event_scheduler *es)
128
{
129
        if(es)
130
                return es->time;
131
        return -1;
132
}
133

    
134
uint32_t event_scheduler_queue_length(struct event_scheduler *es)
135
{
136
        if(es)
137
                return ord_array_length(es->queue);
138
        return 0;
139
}
140