]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte_linux/fosa_clocks_and_timers.c
Including FOSA for MarTE-OS
[frescor/fosa.git] / src_marte_linux / fosa_clocks_and_timers.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
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 for a link to partners' websites
17 //
18 //           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 //  This file is part of the FRSH implementation
24 //
25 //  FRSH is free software; you can  redistribute it and/or  modify
26 //  it under the terms of  the GNU General Public License as published by
27 //  the Free Software Foundation;  either  version 2, or (at  your option)
28 //  any later version.
29 //
30 //  FRSH  is distributed  in  the hope  that  it  will  be useful,  but
31 //  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
32 //  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
33 //  General Public License for more details.
34 //
35 //  You should have  received a  copy of  the  GNU  General Public License
36 //  distributed  with  FRSH;  see file COPYING.   If not,  write to the
37 //  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
38 //  02111-1307, USA.
39 //
40 // -----------------------------------------------------------------------
41 //fosa_clocks_and_timers.c
42 //==============================================
43 //  ********  ******    ********  **********
44 //  **///// /**    **  **//////  /**     /**
45 //  **      /**    ** /**        /**     /**
46 //  ******* /**    ** /********* /**********
47 //  **////  /**    ** ////////** /**//////**
48 //  **      /**    **        /** /**     /**
49 //  **      /**    **  ********  /**     /**
50 //  //       /******/  ////////   //      // 
51 //
52 // FOSA(Frescor Operating System Adaptation layer)
53 //================================================
54
55
56 #include <fosa_clocks_and_timers.h>
57 #include <pthread.h>
58 #include <time.h>
59 #include <stdio.h>
60 #include <string.h>
61
62 static const struct timespec zero_time={0,0};
63
64
65 /*************************
66  * Timing: Clocks
67  *************************/
68
69 /**
70  * fosa_get_time()
71  *
72  * Get the time from a clock
73  *
74  * This function sets the variable pointed to by current_time to the 
75  * current value of the clock specified by clockid, which may be the 
76  * FOSA_CLOCK_REALTIME constant or a value obtained with 
77  * fosa_get_cputime_clock()
78  *
79  * Returns 0 if successful; otherwise it returns an error code:
80  *     EINVAL: the value of clockid is invalid
81  *
82  * Alternatively, in case of error the implementation is allowed to
83  * notify it to the system console and then terminate the FRSH
84  * implementation and dependant applications
85  **/
86 int fosa_clock_get_time(fosa_clock_id_t clockid, struct timespec *current_time)
87 {
88   return clock_gettime(clockid,current_time);
89 }
90
91
92 /**
93  * fosa_get_cputime_clock()
94  *
95  * Get the identifier of a cpu-time clock
96  *
97  * This function stores in the variable pointed to by clockid the 
98  * identifier of a cpu-time clock for the thread specified by tid.
99  *
100  * Returns 0 if successful; otherwise it returns an error code:
101  *    EINVAL: the value of tid is invalid
102  * 
103  * Alternatively, in case of error the implementation is allowed to
104  * notify it to the system console and then terminate the FRSH
105  * implementation and dependant applications
106  **/
107 int fosa_thread_get_cputime_clock
108     (frsh_thread_id_t tid, fosa_clock_id_t *clockid)
109 {
110   return pthread_getcpuclockid(tid,clockid);
111 }
112
113
114 /*************************
115  * Timing: Timers
116  *************************/
117
118 /**
119  * fosa_create_timer()
120  *
121  * Create a one-shot timer
122  *
123  * This function creates a timer based on the clock specified by clock,
124  * and associates to this timer a notification mechanism consisting of
125  * a signal and associated information. Initially, the timer is in the
126  * disarmed state, i.e., not counting time. It can be armed to start
127  * counting time with fosa_timer_arm().
128  *
129  * The function stores the identifier of the newly created timer in the 
130  * variable pointed to by timerid.
131  *
132  * When the timer expires, the signal number specified by signal will be
133  * sent together with the information specified by info, to the thread
134  * that armed the timer (@see fosa_timer_arm()). 
135  *
136  * In those implementations that do not support queueing a
137  * signal with information to a thread (such as POSIX), the signal may
138  * be sent to any thread that is waiting for this signal via
139  * fosa_signal_wait(). Portability can be ensured by having the receiver
140  * thread be the one who is waiting for the signal. 
141  * 
142  * Returns 0 if successful; otherwise it returns an error code:
143  *     EINVAL: the value of clockid or signal is invalid
144  * 
145  *     EAGAIN: the system lacks enough resources to create the timer
146  *
147  * Alternatively, in case of error the implementation is allowed to
148  * notify it to the system console and then terminate the FRSH
149  * implementation and dependant applications
150  **/
151  int fosa_timer_create
152       (fosa_clock_id_t clockid, frsh_signal_t signal, frsh_signal_info_t info,
153        fosa_timer_id_t *timerid)
154 {
155   struct sigevent evp;
156   evp.sigev_notify=SIGEV_SIGNAL;
157   evp.sigev_signo=signal;
158   evp.sigev_value=*( (union sigval *) &info);
159   return timer_create(clockid,&evp,timerid);
160 }
161
162 /**
163  * Delete a timer
164  * 
165  * The function deletes the timer specified by timerid, which becomes 
166  * unusable. If the timer was armed, it is automatically disarmed before
167  * deletion.
168  * 
169  * Returns 0 if successful; otherwise it returns an error code:
170  *     EINVAL: the value of timerid is not valid
171  *
172  * Alternatively, in case of error the implementation is allowed to
173  * notify it to the system console and then terminate the FRSH
174  * implementation and dependant applications
175  **/
176 int fosa_timer_delete(fosa_timer_id_t timerid)
177 {
178   return timer_delete(timerid);
179 }
180
181 /**
182  * fosa_timer_arm()
183  *
184  * Arm a timer
185  *
186  * The timer specified by timer is armed and starts counting time.
187  *
188  * If abstime is true, the value pointed to by value is the absolute
189  * time at which the timer will expire. If value specifies a time instant 
190  * in the past, the timer expires immediately.
191  *
192  * If abstime is false, the value pointed to by value is the relative interval
193  * that must elapse for the timer to expire. 
194  *
195  * In both cases, absolute or relative, the time is measured with the clock 
196  * associated with the timer when it was created.
197  *
198  * If the timer was already armed, the previous time or interval is discarded
199  * and the timer is rearmed with the new value.
200  *
201  * When the timer expires, it is disarmed.
202  *
203  * Returns 0 if successful; otherwise it returns an error code:
204  *    EINVAL: the value of timerid or value is invalid
205  *
206  * Alternatively, in case of error the implementation is allowed to
207  * notify it to the system console and then terminate the FRSH
208  * implementation and dependant applications
209  **/
210 int fosa_timer_arm
211       (fosa_timer_id_t timerid, bool abstime,
212        const struct timespec *value)
213 {
214   int timer_abstime=0;
215   struct itimerspec timer_value;  
216
217   // set the abstime flag if necessary
218   if (abstime) {
219     timer_abstime=TIMER_ABSTIME;
220   }
221
222   // set the timer to the specified value, one shot only
223   timer_value.it_value=*value;
224   timer_value.it_interval=zero_time;
225
226   // arm the timer
227   return timer_settime(timerid,timer_abstime,&timer_value,NULL);
228 }
229
230 /**
231  * fosa_timer_disarm()
232  *
233  * Disarm a timer
234  * 
235  * The timer specified by timer is disarmed, and will not expire unless 
236  * it is rearmed. If the timer was already disramed, the function has 
237  * no effect.
238  *
239  * Returns 0 if successful; otherwise it returns an error code:
240  *    EINVAL: the value of timerid or value is invalid
241  *
242  * Alternatively, in case of error the implementation is allowed to
243  * notify it to the system console and then terminate the FRSH
244  * implementation and dependant applications
245  **/
246 int fosa_timer_disarm(fosa_timer_id_t timerid, struct timespec *remaining_time)
247 {
248   struct itimerspec timer_value;
249   struct itimerspec it_remaining_time;
250   int error = 0;
251
252   memset(&it_remaining_time, 0, sizeof(it_remaining_time) );
253
254   timer_value.it_value=zero_time;
255   timer_value.it_interval=zero_time;
256
257   if (remaining_time == NULL)
258   {
259       return timer_settime(timerid,0,&timer_value,NULL);
260   }
261
262   /* We need to return the remaining time */
263   error = timer_settime(timerid, 0, &timer_value, &it_remaining_time);
264
265   if (error != 0)
266   {
267       return error;
268   }
269
270   *remaining_time = it_remaining_time.it_value;
271
272   return 0;
273
274 }