]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - frsh_api/frsh_synchobj.c
Fixed "allocation" of dummy VRESes
[frescor/frsh.git] / frsh_api / frsh_synchobj.c
1 /**
2  * @file   frsh_synchobj.c
3  * @author Dario Faggioli <faggioli@gandalf.sssup.it>
4  *
5  * @brief  FRSH core synchronization objects related functions
6  *         not implamented in managers.
7  *
8  *
9  */
10 #include <fres_vres.h>
11 #include <fra_generic.h>
12 #include <fres_synchobj.h>
13
14 #include <frsh_core.h>
15
16 int frsh_synchobj_create(frsh_synchobj_handle_t *synch_handle)
17 {
18         if (!synch_handle)
19                 return FRSH_ERR_BAD_ARGUMENT;
20
21         *synch_handle = fres_synchobj_new();
22         if (!(*synch_handle)) goto err;
23
24         return 0;
25 err:
26         return errno;
27 }
28
29 int frsh_synchobj_destroy(const frsh_synchobj_handle_t synch_handle)
30 {
31         return fres_synchobj_destroy(synch_handle);
32 }
33
34 static void
35 __frsh_synchobj_check_wcet_and_deadline(frsh_thread_id_t thread,
36                                         fres_thread_vres_t *th_vres,
37                                         bool *was_deadline_missed,
38                                         bool *was_budget_overrun)
39 {
40         fosa_clock_id_t th_clockid;
41         fosa_abs_time_t curr_time, curr_exec_time;
42         frsh_vres_id_t vres;
43         fres_block_basic *b;
44         fres_block_timing_reqs *t;
45
46         if (!was_deadline_missed && !was_budget_overrun)
47                 return;
48
49         *was_deadline_missed = false;
50         *was_budget_overrun = false;
51
52         fosa_thread_get_cputime_clock(thread, &th_clockid);
53         fosa_clock_get_time(FOSA_CLOCK_REALTIME, &curr_time);
54         fosa_clock_get_time(th_clockid, &curr_exec_time);
55
56         curr_time = fosa_abs_time_decr(curr_time, th_vres->job_start_time);
57         curr_exec_time = fosa_abs_time_decr(curr_exec_time,
58                                             th_vres->job_cpu_time);
59
60         vres = th_vres->vres;
61         b = fres_contract_get_basic(vres->perceived);
62         t = fres_contract_get_timing_reqs(vres->perceived);
63
64         if (was_deadline_missed &&
65             fosa_abs_time_smaller_or_equal(t->deadline, curr_time))
66                 *was_deadline_missed = true;
67
68         if (was_budget_overrun &&
69             fosa_abs_time_smaller_or_equal(b->budget, curr_exec_time))
70                 *was_budget_overrun = false;
71 }
72
73 static void
74 __frsh_synchobj_set_wcet_and_deadline(frsh_thread_id_t thread,
75                                       fres_thread_vres_t *th_vres)
76 {
77         fosa_clock_id_t th_clockid;
78
79         fosa_thread_get_cputime_clock(thread, &th_clockid);
80         fosa_clock_get_time(FOSA_CLOCK_REALTIME,
81                                   &th_vres->job_start_time);
82         fosa_clock_get_time(th_clockid, &th_vres->job_cpu_time);
83
84 }
85
86 int frsh_synchobj_wait
87   (const frsh_synchobj_handle_t synch_handle,
88    frsh_rel_time_t *next_budget,
89    frsh_rel_time_t *next_period,
90    bool *was_deadline_missed,
91    bool *was_budget_overrun)
92 {
93         frsh_thread_id_t thread = fosa_thread_self();
94         fres_thread_vres_t *th_vres;
95         int ret = 0;
96
97         th_vres = fra_get_thread_vres(&thread, FRSH_RT_PROCESSOR);
98         if (!th_vres) goto out;
99
100         __frsh_synchobj_check_wcet_and_deadline(thread,
101                                                 th_vres,
102                                                 was_deadline_missed,
103                                                 was_budget_overrun);
104
105         ret = fres_synchobj_wait_with_timeout(synch_handle, NULL);
106         if (ret) goto out;
107
108         __frsh_synchobj_set_wcet_and_deadline(thread, th_vres);
109         frsh_vres_get_budget_and_period(th_vres->vres,
110                                         next_budget,
111                                         next_period);
112
113 out:
114         return ret;
115 }
116
117 int frsh_synchobj_wait_with_timeout
118   (const frsh_synchobj_handle_t synch_handle,
119    const frsh_abs_time_t *abs_timeout,
120    bool *timed_out,
121    frsh_rel_time_t *next_budget,
122    frsh_rel_time_t *next_period,
123    bool *was_deadline_missed,
124    bool *was_budget_overrun)
125 {
126         frsh_thread_id_t thread = fosa_thread_self();
127         fres_thread_vres_t *th_vres;
128         int ret = 0;
129
130         th_vres = fra_get_thread_vres(&thread, FRSH_RT_PROCESSOR);
131         if (!th_vres) goto out;
132
133         __frsh_synchobj_check_wcet_and_deadline(thread,
134                                                 th_vres,
135                                                 was_deadline_missed,
136                                                 was_budget_overrun);
137
138         ret = fres_synchobj_wait_with_timeout(synch_handle, abs_timeout);
139         if (ret == ETIMEDOUT) *timed_out = true;
140         else if (ret != 0) goto out;
141
142         __frsh_synchobj_set_wcet_and_deadline(thread, th_vres);
143         frsh_vres_get_budget_and_period(th_vres->vres,
144                                         next_budget,
145                                         next_period);
146
147 out:
148         return ret;
149 }
150
151 int frsh_synchobj_signal(const frsh_synchobj_handle_t synch_handle)
152 {
153         return fres_synchobj_signal(synch_handle);
154 }
155
156 int frsh_timed_wait
157   (const frsh_abs_time_t *abs_time,
158    frsh_rel_time_t *next_budget,
159    frsh_rel_time_t *next_period,
160    bool *was_deadline_missed,
161    bool *was_budget_overrun)
162 {
163         frsh_thread_id_t thread = fosa_thread_self();
164         fres_thread_vres_t *th_vres;
165         int ret = 0;
166
167         th_vres = fra_get_thread_vres(&thread, FRSH_RT_PROCESSOR);
168         if (!th_vres) goto out;
169
170         __frsh_synchobj_check_wcet_and_deadline(thread,
171                                                 th_vres,
172                                                 was_deadline_missed,
173                                                 was_budget_overrun);
174
175         ret = clock_nanosleep(FOSA_CLOCK_REALTIME,
176                               TIMER_ABSTIME,
177                               abs_time, NULL);
178
179         __frsh_synchobj_set_wcet_and_deadline(thread, th_vres);
180         frsh_vres_get_budget_and_period(th_vres->vres,
181                                         next_budget,
182                                         next_period);
183
184 out:
185         return ret;
186 }
187
188 int frsh_vresperiod_wait
189   (unsigned long period_num,
190    frsh_rel_time_t       *next_budget,
191    frsh_rel_time_t       *next_period,
192    bool                  *was_deadline_missed,
193    bool                  *was_budget_overran)
194 {
195         return FRSH_ERR_NOT_IMPLEMENTED;
196 }
197
198 int frsh_get_period_number(const frsh_vres_id_t vres, long *period_num)
199
200 {
201         return FRSH_ERR_NOT_IMPLEMENTED;
202 }
203