Subversion Repositories shark

Rev

Rev 1359 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1333 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;
1395 giacomo 16
struct action_event *action_event_list;
17
int total_events = 0;
1333 giacomo 18
 
1395 giacomo 19
int get_free_slot();
20
 
21
int init_action_event(int number_of_events) {
22
 
23
  action_event_list = calloc(number_of_events * sizeof(struct action_event),0);
24
  total_events = number_of_events;
25
 
26
  if (action_event_list != NULL) return 0;
27
 
28
  total_events = 0;
29
  return -1;
30
 
31
}
32
 
1333 giacomo 33
int insert_action_event(struct action_event *e) {
34
 
35
  struct action_event *t = first_action_event, *k = NULL;
1395 giacomo 36
  int free;
1333 giacomo 37
 
38
  SYS_FLAGS f;
39
 
40
  f = kern_fsave();
41
 
1395 giacomo 42
  free = get_free_slot();
43
 
44
  if (free != -1) {
45
 
46
        memcpy(&(action_event_list[free]),e,sizeof(struct action_event));
47
        e = &(action_event_list[free]);
48
 
49
  } else {
50
 
51
        return -1;
52
 
53
  }
54
 
1334 giacomo 55
  e->status = EVT_STATUS_WAIT;
56
 
1333 giacomo 57
  if (!t) {
58
 
59
        first_action_event = e;
60
        e->next = NULL;
61
        kern_frestore(f);
62
        return 0;
63
 
64
  }
65
 
66
  while(t) {
67
        if (TIMESPEC_A_LT_B(&e->time,&t->time))
68
                break;
69
        k = t;
70
        t = t->next;
71
  }
72
 
73
  t = k->next;
74
  k->next = e;
75
  e->next = t;
76
 
77
  t = first_action_event;
78
 
79
  kern_frestore(f);
80
 
1395 giacomo 81
  return free;
1333 giacomo 82
 
83
}
84
 
1395 giacomo 85
int delete_action_event(int event) {
1333 giacomo 86
 
87
  struct action_event *t = first_action_event;
1395 giacomo 88
  struct action_event *e = &(action_event_list[event]);
1333 giacomo 89
 
90
  SYS_FLAGS f;
91
 
1395 giacomo 92
  if ((!t || !e) && (e->status != EVT_STATUS_FREE)) return -1;
1333 giacomo 93
 
94
  f = kern_fsave();
95
 
96
  if (t == e) {
97
 
98
        first_action_event = t->next;
1395 giacomo 99
        e->status = EVT_STATUS_FREE;
1333 giacomo 100
        kern_frestore(f);
101
        return 0;
102
 
103
  }
104
 
105
  while(t) {
106
        if (t->next == e)
107
                break;
108
        t = t->next;
109
  }
110
 
111
  if (t) {
112
        t->next = e->next;
1395 giacomo 113
        e->status = EVT_STATUS_FREE;
1333 giacomo 114
        kern_frestore(f);
115
        return 0;
116
  }        
117
 
118
  kern_frestore(f);
119
 
120
  return -1;
121
 
122
}
123
 
1395 giacomo 124
int get_free_slot() {
125
 
126
  int k = 0;
127
 
128
  while (k < total_events) {
129
 
130
        if (action_event_list[k].status == EVT_STATUS_FREE ||
131
                action_event_list[k].status == EVT_STATUS_DONE)
132
                return k;
133
        k++;
134
 
135
  }
136
 
137
  return -1;
138
 
139
}
140
 
1333 giacomo 141
struct action_event * get_first_old_event(struct timespec *time) {
142
 
143
  struct action_event *t = first_action_event;
144
  SYS_FLAGS f;
145
 
146
  if (!t) return NULL;
147
 
148
  f = kern_fsave();
149
 
150
  if (TIMESPEC_A_GT_B(time,&(t->time))) {
151
    first_action_event = t->next;
1334 giacomo 152
    t->status = EVT_STATUS_EXEC;
1333 giacomo 153
    kern_frestore(f);
154
    return t;
155
  }
156
 
157
  kern_frestore(f);
158
  return NULL;
159
 
160
}