Subversion Repositories shark

Rev

Rev 224 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
221 giacomo 1
//=====================================================================
2
//       FFFFFFIII   RRRRR      SSTTTTTTT
3
//      FF         IIR   RR    SS
4
//     FF           IR        SS
5
//    FFFFFF         RRRR    SSSSST      
6
//   FF       FI       RRR  SS
7
//  FF         II     RRR  SS
8
// FF           IIIIIR    RS 
9
//       
10
// Basic FSF(FIRST Scheduling Framework) contract management
11
// S.Ha.R.K. Implementation
12
//=====================================================================
13
 
14
#include "fsf_contract.h"
15
 
16
#include "cbsstar.h"
17
#include "posixstar.h"
18
#include "edfstar.h"
19
 
20
#include <stdlib.h>
21
 
22
//#define FSF_DEBUG
23
 
24
extern int cbsstar_level;
25
 
26
int
27
fsf_create_synchobject(fsf_synch_object_handle_t *synch_handle)
28
{
29
 
30
  if (!synch_handle) return FSF_ERR_INVALID_SYNCH_OBJECT_HANDLE;
31
 
32
  iq_init(&synch_handle->threads, NULL, 0);  
33
  synch_handle->events = 0;
34
 
35
  return 0;
36
 
37
}
38
 
39
int
40
fsf_signal_synchobject(fsf_synch_object_handle_t *synch_handle)
41
{
42
 
43
  PID p;
44
 
45
  if (!synch_handle) return FSF_ERR_INVALID_SYNCH_OBJECT_HANDLE;
46
 
47
  if ((p = iq_getfirst(&synch_handle->threads)) != NIL)
48
    task_activate(p);
49
  else
50
    synch_handle->events++;
51
 
52
  return 0;
53
 
54
}
55
 
56
int
57
fsf_destroy_synchobject(fsf_synch_object_handle_t *synch_handle)
58
{
59
 
60
  if (!synch_handle) return FSF_ERR_INVALID_SYNCH_OBJECT_HANDLE;
61
 
62
  while (iq_getfirst(&synch_handle->threads) != NIL);  
63
  synch_handle->events = 0;
64
 
65
  return 0;
66
 
67
}
68
 
69
int fsf_schedule_next_timed_job
70
  (const struct timespec *at_absolute_time,
71
   struct timespec       *next_budget,
72
   struct timespec       *next_period,
73
   bool                  *was_deadline_missed,
74
   bool                  *was_budget_overran)
75
{
76
 
77
  TIME T,Q;
78
  int budget, local_scheduler_level, scheduler_id;
79
 
80
  local_scheduler_level = CBSSTAR_get_local_scheduler_level_from_pid(cbsstar_level, exec_shadow);
81
  scheduler_id = CBSSTAR_get_local_scheduler_id_from_pid(cbsstar_level, exec_shadow);
82
 
83
  switch (scheduler_id) {
84
    case FSF_SCHEDULER_POSIX:
85
      budget = POSIXSTAR_getbudget(local_scheduler_level, exec_shadow);
86
      break;
87
    case FSF_SCHEDULER_EDF:
88
      budget = EDFSTAR_getbudget(local_scheduler_level, exec_shadow);
89
      break;
90
    case FSF_SCHEDULER_RM:
91
    default:
92
      budget = -1;
93
      break;
94
  }
95
 
96
  if (budget == -1) return FSF_ERR_INVALID_SERVER;
97
 
98
  if (next_budget != NULL && next_period != NULL) {
99
 
100
    CBSSTAR_getbudgetinfo(cbsstar_level, &Q, &T, budget);
101
 
102
    #ifdef FSF_DEBUG
103
      kern_printf("(budget %d Q=%d T=%d)",budget,(int)Q,(int)T);
104
    #endif
105
 
106
    next_budget->tv_sec = Q / 1000000;
107
    next_budget->tv_nsec = (Q % 1000000) * 1000;
108
    next_period->tv_sec = T / 1000000;
109
    next_period->tv_nsec = (T % 1000000) * 1000;
110
 
111
  }
112
 
113
  if (was_deadline_missed != NULL) *was_deadline_missed = false;
114
  if (was_budget_overran != NULL)
115
    *was_budget_overran = (bool)CBSSTAR_was_budget_overran(cbsstar_level,budget);
116
 
117
  if (at_absolute_time != NULL)
118
    kern_event_post(at_absolute_time, (void (*)(void *))&task_activate, (void *)(exec_shadow));
119
 
120
  #ifdef FSF_DEBUG
121
    if (at_absolute_time != NULL)
122
      kern_printf("(Next act s%d:us%d)",(int)at_absolute_time->tv_sec,(int)at_absolute_time->tv_nsec/1000);
123
    else
124
      kern_printf("(End Cycle %d)",exec_shadow);
125
  #endif
126
 
127
  task_endcycle();
128
 
129
  return 0;
130
 
131
}
132
 
133
int
134
fsf_schedule_next_event_triggered_job
135
  (fsf_synch_object_handle_t *synch_handle,
136
   struct timespec           *next_budget,
137
   struct timespec           *next_period,
138
   bool                      *was_deadline_missed,
139
   bool                      *was_budget_overran)
140
{
141
 
142
  TIME T,Q;
143
  int budget, local_scheduler_level, scheduler_id;
144
 
145
  local_scheduler_level = CBSSTAR_get_local_scheduler_level_from_pid(cbsstar_level, exec_shadow);
146
  scheduler_id = CBSSTAR_get_local_scheduler_id_from_pid(cbsstar_level, exec_shadow);
147
 
148
  switch (scheduler_id) {
149
    case FSF_SCHEDULER_POSIX:
150
      budget = POSIXSTAR_getbudget(local_scheduler_level, exec_shadow);
151
      break;
152
    case FSF_SCHEDULER_EDF:
153
      budget = EDFSTAR_getbudget(local_scheduler_level, exec_shadow);
154
      break;
155
    case FSF_SCHEDULER_RM:
156
    default:
157
      budget = -1;
158
      break;
159
  }
160
 
161
  if (budget == -1) return FSF_ERR_INVALID_SERVER;
162
 
163
  if (next_budget != NULL && next_period != NULL) {
164
 
165
    CBSSTAR_getbudgetinfo(cbsstar_level, &Q, &T, budget);
166
 
167
    #ifdef FSF_DEBUG
168
      kern_printf("(budget %d Q=%d T=%d)",budget,(int)Q,(int)T);
169
    #endif
170
 
171
    next_budget->tv_sec = Q / 1000000;
172
    next_budget->tv_nsec = (Q % 1000000) * 1000;
173
    next_period->tv_sec = T / 1000000;
174
    next_period->tv_nsec = (T % 1000000) * 1000;
175
 
176
  }
177
 
178
  if (was_deadline_missed != NULL) *was_deadline_missed = false;
179
  if (was_budget_overran != NULL)
180
    *was_budget_overran = (bool)CBSSTAR_was_budget_overran(cbsstar_level,budget);
181
 
182
  if (synch_handle->events > 0) {
183
    task_activate(exec_shadow);
184
    synch_handle->events--;
185
  } else
186
    iq_insertlast(exec_shadow,&synch_handle->threads);
187
 
188
  #ifdef FSF_DEBUG
189
    kern_printf("(Synch_Handle Events %d)",synch_handle->events);
190
  #endif
191
 
192
  task_endcycle();
193
 
194
  return 0;
195
 
196
}