Statistics
| Branch: | Tag: | Revision:

sssimulator / EventScheduler / event_scheduler.c @ af271552

History | View | Annotate | Download (2.83 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 0;
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)
80
{
81
        if(es && e && interval >= 0)
82
        {
83
                e->time = es->time + interval;
84
                return ord_array_insert(es->queue, (void *) e);
85
        }
86
        return -1;
87
}
88

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

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

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

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

    
123
        return res;
124
}
125

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

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