Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1624 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: Giorgio Buttazzo <giorgio@sssup.it> |
||
5 | * |
||
6 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
7 | * |
||
8 | * http://www.sssup.it |
||
9 | * http://retis.sssup.it |
||
10 | * http://shark.sssup.it |
||
11 | */ |
||
12 | |||
13 | #include "chimera.h" |
||
14 | |||
15 | struct action_event *first_action_event = NULL; |
||
16 | struct action_event *action_event_list; |
||
17 | int total_events = 0; |
||
18 | |||
19 | int get_free_slot(); |
||
20 | |||
21 | int init_action_event(int number_of_events) { |
||
22 | |||
23 | action_event_list = malloc(number_of_events * sizeof(struct action_event)); |
||
24 | memset(action_event_list,0,number_of_events * sizeof(struct action_event)); |
||
25 | total_events = number_of_events; |
||
26 | |||
27 | if (action_event_list != NULL) return 0; |
||
28 | |||
29 | total_events = 0; |
||
30 | return -1; |
||
31 | |||
32 | } |
||
33 | |||
34 | int insert_action_event(struct action_event *e) { |
||
35 | |||
36 | struct action_event *t = first_action_event, *k = NULL; |
||
37 | int free; |
||
38 | |||
39 | SYS_FLAGS f; |
||
40 | |||
41 | f = kern_fsave(); |
||
42 | |||
43 | free = get_free_slot(); |
||
44 | |||
45 | if (free != -1) { |
||
46 | |||
47 | memcpy(&(action_event_list[free]),e,sizeof(struct action_event)); |
||
48 | e = &(action_event_list[free]); |
||
49 | |||
50 | } else { |
||
51 | |||
52 | return -1; |
||
53 | |||
54 | } |
||
55 | |||
56 | e->status = EVT_STATUS_WAIT; |
||
57 | |||
58 | if (!t) { |
||
59 | |||
60 | first_action_event = e; |
||
61 | e->next = NULL; |
||
62 | kern_frestore(f); |
||
63 | return 0; |
||
64 | |||
65 | } |
||
66 | |||
67 | while(t) { |
||
68 | if (TIMESPEC_A_LT_B(&e->time,&t->time)) |
||
69 | break; |
||
70 | k = t; |
||
71 | t = t->next; |
||
72 | } |
||
73 | |||
74 | t = k->next; |
||
75 | k->next = e; |
||
76 | e->next = t; |
||
77 | |||
78 | t = first_action_event; |
||
79 | |||
80 | kern_frestore(f); |
||
81 | |||
82 | return free; |
||
83 | |||
84 | } |
||
85 | |||
86 | int delete_action_event(int event) { |
||
87 | |||
88 | struct action_event *t = first_action_event; |
||
89 | struct action_event *e = &(action_event_list[event]); |
||
90 | |||
91 | SYS_FLAGS f; |
||
92 | |||
93 | if ((!t || !e) && (e->status != EVT_STATUS_FREE)) return -1; |
||
94 | |||
95 | f = kern_fsave(); |
||
96 | |||
97 | if (t == e) { |
||
98 | |||
99 | first_action_event = t->next; |
||
100 | e->status = EVT_STATUS_FREE; |
||
101 | kern_frestore(f); |
||
102 | return 0; |
||
103 | |||
104 | } |
||
105 | |||
106 | while(t) { |
||
107 | if (t->next == e) |
||
108 | break; |
||
109 | t = t->next; |
||
110 | } |
||
111 | |||
112 | if (t) { |
||
113 | t->next = e->next; |
||
114 | e->status = EVT_STATUS_FREE; |
||
115 | kern_frestore(f); |
||
116 | return 0; |
||
117 | } |
||
118 | |||
119 | kern_frestore(f); |
||
120 | |||
121 | return -1; |
||
122 | |||
123 | } |
||
124 | |||
125 | int get_free_slot() { |
||
126 | |||
127 | int k = 0; |
||
128 | |||
129 | while (k < total_events) { |
||
130 | |||
131 | if (action_event_list[k].status == EVT_STATUS_FREE || |
||
132 | action_event_list[k].status == EVT_STATUS_DONE) |
||
133 | return k; |
||
134 | k++; |
||
135 | |||
136 | } |
||
137 | |||
138 | return -1; |
||
139 | |||
140 | } |
||
141 | |||
142 | struct action_event * get_first_old_event(struct timespec *time) { |
||
143 | |||
144 | struct action_event *t = first_action_event; |
||
145 | SYS_FLAGS f; |
||
146 | |||
147 | if (!t) return NULL; |
||
148 | |||
149 | f = kern_fsave(); |
||
150 | |||
151 | if (TIMESPEC_A_GT_B(time,&(t->time))) { |
||
152 | first_action_event = t->next; |
||
153 | t->status = EVT_STATUS_EXEC; |
||
154 | kern_frestore(f); |
||
155 | return t; |
||
156 | } |
||
157 | |||
158 | kern_frestore(f); |
||
159 | return NULL; |
||
160 | |||
161 | } |