]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_ose/fosa_clocks_and_timers.c
Makefile: Add missing header file
[frescor/fosa.git] / src_ose / fosa_clocks_and_timers.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //  All rights reserved.
24 //
25 //  Redistribution and use in source and binary forms, with or 
26 //  without modification, are permitted provided that the 
27 //  following conditions are met:
28 //
29 //    * Redistributions of source code must retain the above 
30 //      copyright notice, this list of conditions and the 
31 //      following disclaimer.
32 //    * Redistributions in binary form must reproduce the above 
33 //      copyright notice, this list of conditions and the 
34 //      following disclaimer in the documentation and/or other 
35 //      materials provided with the distribution.
36 //    * Neither the name of FRESCOR nor the names of its 
37 //      contributors may be used to endorse or promote products 
38 //      derived from this software without specific prior 
39 //      written permission.
40 //
41 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
42 //  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
43 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
44 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
45 //  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
46 //  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
47 //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
48 //  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
49 //  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
50 //  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
51 //  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
52 //  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
53 //  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
54 //  POSSIBILITY OF SUCH DAMAGE.
55 // -----------------------------------------------------------------------
56 //fosa_clocks_and_timers.c
57 //==============================================
58 //  ********  ******    ********  **********
59 //  **///// /**    **  **//////  /**     /**
60 //  **      /**    ** /**        /**     /**
61 //  ******* /**    ** /********* /**********
62 //  **////  /**    ** ////////** /**//////**
63 //  **      /**    **        /** /**     /**
64 //  **      /**    **  ********  /**     /**
65 //  //       /******/  ////////   //      // 
66 //
67 // FOSA(Frescor Operating System Adaptation layer)
68 //================================================
69
70 #include "fosa.h"
71 #include "fosa_ose_implementation_specific.h"
72 #include "malloc.h"
73
74 //Debug purpose only.......................
75 #include "stdio.h"
76
77 /**
78  * @defgroup clocksandtimers Clocks and Timers
79  * @ingroup fosa
80  *
81  * This module defines the types and functions to abstract clocks and
82  * timers for the FRSH implementation.
83  *
84  * @{
85  **/
86
87
88
89 /*************************
90  * Timing: Clocks
91  *************************/
92
93 /**
94  * fosa_get_time()
95  *
96  * Get the time from a clock
97  *
98  * This function sets the variable pointed to by current_time to the 
99  * current value of the clock specified by clockid, which may be the 
100  * FOSA_CLOCK_REALTIME constant or a value obtained with 
101  * fosa_get_cputime_clock()
102  *
103  * Returns 0 if successful; otherwise it returns an error code:
104  *     EINVAL: the value of clockid is invalid
105  *
106  * Alternatively, in case of error the implementation is allowed to
107  * notify it to the system console and then terminate the FRSH
108  * implementation and dependant applications
109  **/
110 int fosa_clock_get_time(fosa_clock_id_t clockid, 
111                         struct timespec *current_time) 
112 {
113     if (clockid == FOSA_CLOCK_REALTIME) {
114         OSTICK ticks;
115         OSTICK micros;
116         ticks = get_systime(&micros);
117         //If micros == NULL this are not provided.
118         fosa_ose_systime_to_timespec(ticks, micros, current_time);
119     } else if (fosa_ose_is_process(clockid)) {
120         // Opimization, save the last called pid and a pointer to it's 
121         // execution time. Make if statement to check for it.
122         
123         //The clock are an execution time measurement.
124         //extern struct* global_time_ptr;
125         
126         // Find the pid in the global linked list.
127         
128         // Save the executiontime in current_time->tv_nsec and 
129         // current_time->tv_sec.
130         // fosa_ose_systime_to_timespec(systick, micro, current_time);
131     } else {
132         //No valid clock_id.
133         return EINVAL;
134     }
135     
136     return 0;
137 }
138
139 /**
140  * fosa_get_cputime_clock()
141  *
142  * Get the identifier of a cpu-time clock
143  *
144  * This function stores in the variable pointed to by clockid the 
145  * identifier of a cpu-time clock for the thread specified by tid.
146  *
147  * Returns 0 if successful; otherwise it returns an error code:
148  *    EINVAL: the value of tid is invalid
149  * 
150  * Alternatively, in case of error the implementation is allowed to
151  * notify it to the system console and then terminate the FRSH
152  * implementation and dependant applications
153  **/
154 int fosa_thread_get_cputime_clock(frsh_thread_id_t tid, fosa_clock_id_t *clockid)
155 {
156     //Might need to change. Possibility to have many clocks? Probably not
157     //One clock but many timers. .........................................
158     
159     if (!fosa_ose_is_process(tid)) return EINVAL;
160     *clockid = (fosa_clock_id_t) tid;
161     return 0;
162 }
163
164
165 /*************************
166  * Timing: Timers
167  *************************/
168
169 /**
170  * fosa_create_timer()
171  *
172  * Create a one-shot timer
173  *
174  * This function creates a timer based on the clock specified by clock,
175  * and associates to this timer a notification mechanism consisting of
176  * a signal and associated information. Initially, the timer is in the
177  * disarmed state, i.e., not counting time. It can be armed to start
178  * counting time with fosa_timer_arm().
179  *
180  * The function stores the identifier of the newly created timer in the 
181  * variable pointed to by timerid.
182  *
183  * When the timer expires, the signal number specified by signal will be
184  * sent together with the information specified by info, to the thread
185  * that armed the timer (@see fosa_timer_arm()). 
186  *
187  * In those implementations that do not support queueing a
188  * signal with information to a thread (such as POSIX), the signal may
189  * be sent to any thread that is waiting for this signal via
190  * fosa_signal_wait(). Portability can be ensured by having the receiver
191  * thread be the one who is waiting for the signal. 
192  * 
193  * Returns 0 if successful; otherwise it returns an error code:
194  *     EINVAL: the value of clockid or signal is invalid
195  * 
196  *     EAGAIN: the system lacks enough resources to create the timer
197  *
198  * Alternatively, in case of error the implementation is allowed to
199  * notify it to the system console and then terminate the FRSH
200  * implementation and dependant applications
201  **/
202  int fosa_timer_create
203       (fosa_clock_id_t clockid, frsh_signal_t signal, 
204        frsh_signal_info_t info, fosa_timer_id_t *timerid)
205 {
206     *timerid = (fosa_timer_id_t)malloc(sizeof(fosa_ose_timer_id_t));  
207     fosa_timer_id_t timer_id = *timerid; //To clearify only.
208     
209     timer_id->sig_no     = signal;  
210     timer_id->info       = info;
211     timer_id->clock_id   = clockid;
212     timer_id->ose_tmoref = -1;
213     
214     return 0;
215 }
216
217 /**
218  * Delete a timer
219  * 
220  * The function deletes the timer specified by timerid, which becomes 
221  * unusable. If the timer was armed, it is automatically disarmed before
222  * deletion.
223  * 
224  * Returns 0 if successful; otherwise it returns an error code:
225  *     EINVAL: the value of timerid is not valid
226  *
227  * Alternatively, in case of error the implementation is allowed to
228  * notify it to the system console and then terminate the FRSH
229  * implementation and dependant applications
230  **/
231 int fosa_timer_delete(fosa_timer_id_t timerid)
232 {
233     if (timerid->clock_id == FOSA_CLOCK_REALTIME) {
234         cancel_tmo(&(timerid->ose_tmoref));
235     } else { 
236         //Maybe do something with some global data accessible in the 
237         //swap-handlers...................................................     
238     }
239     timerid->ose_tmoref = -1;
240     free(timerid);
241     //Errors taken care of by OSE.
242     return 0;
243 }
244
245 /**
246  * fosa_timer_arm()
247  *
248  * Arm a timer
249  *
250  * The timer specified by timer is armed and starts counting time.
251  *
252  * If abstime is true, the value pointed to by value is the absolute
253  * time at which the timer will expire. If value specifies a time instant 
254  * in the past, the timer expires immediately.
255  *
256  * If abstime is false, the value pointed to by value is the relative interval
257  * that must elapse for the timer to expire. 
258  *
259  * In both cases, absolute or relative, the time is measured with the clock 
260  * associated with the timer when it was created.
261  *
262  * If the timer was already armed, the previous time or interval is discarded
263  * and the timer is rearmed with the new value.
264  *
265  * When the timer expires, it is disarmed.
266  *
267  * Returns 0 if successful; otherwise it returns an error code:
268  *    EINVAL: the value of timerid or value is invalid
269  *
270  * Alternatively, in case of error the implementation is allowed to
271  * notify it to the system console and then terminate the FRSH
272  * implementation and dependant applications
273  **/
274 int fosa_timer_arm
275       (fosa_timer_id_t timerid, bool abstime,
276        const struct timespec *value)
277 {
278     /*printf("Timerid sig_no %d, info %d, clock_id %d, ose_tmoref %d\n",
279         timerid->sig_no, timerid->info.value, timerid->clock_id,
280         timerid->ose_tmoref);
281     printf("Lika? %d == %d\n",timerid->clock_id, FOSA_CLOCK_REALTIME);*/
282     
283     OSTIME timeout_ms = value->tv_nsec / 1000000 + 
284                         value->tv_sec  * 1000; 
285             
286     //if (timerid->ose_tmoref != -1) {
287     //    restart_tmo(&(timerid->ose_tmoref), timeout_ms);
288     //} 
289     //else 
290     if (timerid->clock_id == FOSA_CLOCK_REALTIME) {
291         if (abstime) {
292             //Absolute time
293         } else {
294             fosa_ose_signal_t *ose_sig;
295             ose_sig = (fosa_ose_signal_t *)
296                       alloc(sizeof(fosa_ose_signal_t), timerid->sig_no);
297             ose_sig->info   = timerid->info;
298             ose_sig->sig_no = timerid->sig_no;
299             
300             timerid->ose_tmoref = request_tmo_sig(timeout_ms, 
301                                             (union SIGNAL **) &ose_sig);
302         }
303     } else {
304         if (abstime) {
305             return EINVAL;
306             /*
307              * author erth
308              *
309              * A absolute timer for a clock that measures the time a 
310              * process has used the CPU does not make sense. Therefore
311              * error is given.
312              */
313         } else {
314             //En absolut timer mäter tiden jämfört med systemtiden. Medens
315             //en realtiv gör det i antal sekunder och nanosekunder från 
316             //och med nu.
317             
318             //Do something, preapare a signal?................................            
319         }
320     }
321     return 0;
322 }
323 /*
324  * author erth
325  *
326  * Ange i den globala variabel som swap-handlern använder vilken 
327  * timeouten är. Då startar swap-handlern att räkna tid? Om abstime är 
328  * falsk.
329  */
330
331 /**
332  * fosa_timer_disarm()
333  *
334  * Disarm a timer
335  * 
336  * The timer specified by timer is disarmed, and will not expire unless 
337  * it is rearmed. If the timer was already disramed, the function has 
338  * no effect.
339  *
340  * Returns 0 if successful; otherwise it returns an error code:
341  *    EINVAL: the value of timerid or value is invalid
342  *
343  * Alternatively, in case of error the implementation is allowed to
344  * notify it to the system console and then terminate the FRSH
345  * implementation and dependant applications
346  **/
347 int fosa_timer_disarm(fosa_timer_id_t timerid)
348 {
349     if (timerid->clock_id == FOSA_CLOCK_REALTIME) {
350         //OSTIME time = 500;
351         //restart_tmo(&(timerid->ose_tmoref),time);
352         //Hur kolla om signal är given?...................................
353         cancel_tmo(&(timerid->ose_tmoref));
354     } else { 
355         //Maybe do something with some global data accessible in the 
356         //swap-handlers...................................................     
357     }
358     timerid->ose_tmoref = -1;
359     return 0;
360 }
361 /*
362  * author erth
363  *
364  * Ta bort timeouten i den globala variabel som swap-handlern använder. 
365  * Då slutar swap-handlern att räkna tid? 
366  */
367  
368 /*@}*/
369
370