]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/fosa_clocks_and_timers.c
Time abstraction added to FOSA to replace timespec_operations
[frescor/fosa.git] / src_marte / 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 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FOSA (Frsh Operating System Abstraction)
32 //
33 // FOSA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FOSA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FOSA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 //fosa_clocks_and_timers.c
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      //
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65
66
67 #include <pthread.h>
68 #include <time.h>
69 #include <stdio.h>
70
71 #include "fosa_time.h"
72 #include "fosa_clocks_and_timers.h"
73
74 static const struct timespec zero_time={0,0};
75
76
77 /*************************
78  * Timing: Clocks
79  *************************/
80
81 /**
82  * fosa_get_time()
83  *
84  * Get the time from a clock
85  *
86  * This function sets the variable pointed to by current_time to the
87  * current value of the clock specified by clockid, which may be the
88  * FOSA_CLOCK_REALTIME constant or a value obtained with
89  * fosa_get_cputime_clock()
90  *
91  * Returns 0 if successful; otherwise it returns an error code:
92  *     EINVAL: the value of clockid is invalid
93  *
94  * Alternatively, in case of error the implementation is allowed to
95  * notify it to the system console and then terminate the FRSH
96  * implementation and dependant applications
97  **/
98 int fosa_clock_get_time(fosa_clock_id_t clockid, fosa_abs_time_t *current_time)
99 {
100     struct timespec current_time_tspec;
101     int ret_value;
102
103     ret_value = clock_gettime(clockid, &current_time_tspec);
104     *current_time = fosa_timespec_to_abs_time(current_time_tspec);
105
106     return ret_value;
107 }
108
109
110 /**
111  * fosa_get_cputime_clock()
112  *
113  * Get the identifier of a cpu-time clock
114  *
115  * This function stores in the variable pointed to by clockid the
116  * identifier of a cpu-time clock for the thread specified by tid.
117  *
118  * Returns 0 if successful; otherwise it returns an error code:
119  *    EINVAL: the value of tid is invalid
120  *
121  * Alternatively, in case of error the implementation is allowed to
122  * notify it to the system console and then terminate the FRSH
123  * implementation and dependant applications
124  **/
125 int fosa_thread_get_cputime_clock
126     (fosa_thread_id_t tid, fosa_clock_id_t *clockid)
127 {
128   return pthread_getcpuclockid(tid,clockid);
129 }
130
131
132 /*************************
133  * Timing: Timers
134  *************************/
135
136 /**
137  * fosa_create_timer()
138  *
139  * Create a one-shot timer
140  *
141  * This function creates a timer based on the clock specified by clock,
142  * and associates to this timer a notification mechanism consisting of
143  * a signal and associated information. Initially, the timer is in the
144  * disarmed state, i.e., not counting time. It can be armed to start
145  * counting time with fosa_timer_arm().
146  *
147  * The function stores the identifier of the newly created timer in the
148  * variable pointed to by timerid.
149  *
150  * When the timer expires, the signal number specified by signal will be
151  * sent together with the information specified by info, to the thread
152  * that armed the timer (@see fosa_timer_arm()).
153  *
154  * In those implementations that do not support queueing a
155  * signal with information to a thread (such as POSIX), the signal may
156  * be sent to any thread that is waiting for this signal via
157  * fosa_signal_wait(). Portability can be ensured by having the receiver
158  * thread be the one who is waiting for the signal.
159  *
160  * Returns 0 if successful; otherwise it returns an error code:
161  *     EINVAL: the value of clockid or signal is invalid
162  *
163  *     EAGAIN: the system lacks enough resources to create the timer
164  *
165  * Alternatively, in case of error the implementation is allowed to
166  * notify it to the system console and then terminate the FRSH
167  * implementation and dependant applications
168  **/
169  int fosa_timer_create
170       (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
171        fosa_timer_id_t *timerid)
172 {
173   struct sigevent evp;
174   evp.sigev_notify=SIGEV_SIGNAL;
175   evp.sigev_signo=signal;
176   evp.sigev_value=* ((union sigval *) &info);
177   // the above casting construct is used to overcome the compiler
178   // restriction that does not allow casts between unions
179   return timer_create(clockid,&evp,timerid);
180 }
181
182 /**
183  * fosa_timer_create_with_receiver()
184  *
185  * Create a one-shot timer with a specific signal receiver thread
186  *
187  * This function creates a timer in the same way as fosa_timer_create,
188  * except that the signal generated when the timer expires is sent to
189  * the thread specified by receiver
190  *
191  * Returns 0 if successful; otherwise it returns an error code:
192  *     FOSA_EINVAL: the value of clockid or signal is invalid
193  *
194  *     FOSA_EAGAIN: the system lacks enough resources to create the timer
195  *
196  * Alternatively, in case of error the implementation is allowed to
197  * notify it to the system console and then terminate the FRSH
198  * implementation and dependant applications
199  **/
200  int fosa_timer_create_with_receiver
201       (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
202        fosa_timer_id_t *timerid, fosa_thread_id_t receiver)
203 {
204   // in POSIX the receiver thread cannot be specified
205   return fosa_timer_create(clockid,signal,info,timerid);
206 }
207
208
209 /**
210  * Delete a timer
211  *
212  * The function deletes the timer specified by timerid, which becomes
213  * unusable. If the timer was armed, it is automatically disarmed before
214  * deletion.
215  *
216  * Returns 0 if successful; otherwise it returns an error code:
217  *     EINVAL: the value of timerid is not valid
218  *
219  * Alternatively, in case of error the implementation is allowed to
220  * notify it to the system console and then terminate the FRSH
221  * implementation and dependant applications
222  **/
223 int fosa_timer_delete(fosa_timer_id_t timerid)
224 {
225   return timer_delete(timerid);
226 }
227
228
229 /**
230  * fosa_rel_timer_arm()
231  *
232  * Arm a timer with a relative time interval
233  *
234  * The timer specified by timer is armed and starts counting time.
235  *
236  * The value pointed to by value is the relative interval that must
237  * elapse for the timer to expire.  Negative values cause the timer to
238  * expire immediately.
239  *
240  * The time is measured with the clock associated with the timer when
241  * it was created. 
242  *
243  * If the timer was already armed, the previous time or interval is discarded
244  * and the timer is rearmed with the new value.
245  *
246  * When the timer expires, it is disarmed.
247  *
248  * Returns 0 if successful; otherwise it returns an error code:
249  *    FOSA_EINVAL: the value of timerid or value is invalid
250  *
251  * Alternatively, in case of error the implementation is allowed to
252  * notify it to the system console and then terminate the FRSH
253  * implementation and dependant applications
254  **/
255 int fosa_rel_timer_arm
256       (fosa_timer_id_t timerid, const fosa_rel_time_t *value)
257 {
258   struct itimerspec timer_value;
259
260   // set the timer to the specified value, one shot only
261   timer_value.it_value= fosa_rel_time_to_timespec(*value);
262   timer_value.it_interval=zero_time;
263
264   // arm the timer
265   return timer_settime(timerid, 0, &timer_value,NULL);
266 }
267
268
269 /**
270  * fosa_abs_timer_arm()
271  *
272  * Arm a timer that will expire in an absolute time instant.
273  *
274  * The timer specified by timer is armed and starts counting time.
275  *
276  * The value pointed to by value is the absolute time at which the
277  * timer will expire. If value specifies a time instant in the past,
278  * the timer expires immediately. 
279  *
280  * The time is measured with the clock associated with the timer when
281  * it was created. 
282  *
283  * If the timer was already armed, the previous time or interval is discarded
284  * and the timer is rearmed with the new value.
285  *
286  * When the timer expires, it is disarmed.
287  *
288  * Returns 0 if successful; otherwise it returns an error code:
289  *    FOSA_EINVAL: the value of timerid or value is invalid
290  *
291  * Alternatively, in case of error the implementation is allowed to
292  * notify it to the system console and then terminate the FRSH
293  * implementation and dependant applications
294  **/
295 int fosa_abs_timer_arm
296       (fosa_timer_id_t timerid, const fosa_abs_time_t *value)
297 {
298   struct itimerspec timer_value;
299
300   // set the timer to the specified value, one shot only
301   timer_value.it_value= fosa_abs_time_to_timespec(*value);
302   timer_value.it_interval=zero_time;
303
304   // arm the timer
305   return timer_settime(timerid, TIMER_ABSTIME,&timer_value,NULL);
306 }
307
308
309
310
311 /**
312  * fosa_timer_get_remaining_time()
313  *
314  * Get the remaining time for timer expiration
315  *
316  * Returns the relative remaining time for timer expiration.  If the
317  * clock is a CPU clock it returns the time as if the thread was
318  * executing constantly.
319  *
320  * If the timer is disarmed it returns 0.
321  *
322  * Returns 0 if successful; otherwise it returns an error code:
323  *    EINVAL: the value of timerid or value is invalid
324  *
325  * Alternatively, in case of error the implementation is allowed to
326  * notify it to the system console and then terminate the FRSH
327  * implementation and dependant applications
328  **/
329 int fosa_timer_get_remaining_time
330         (fosa_timer_id_t timerid, fosa_rel_time_t *remaining_time)
331 {
332     struct itimerspec timer_value;
333     int error_code;
334
335     if (remaining_time == NULL) {
336         return EINVAL;
337     }
338     error_code=timer_gettime(timerid,&timer_value);
339     if (error_code==-1) return errno;
340     
341     *remaining_time = fosa_timespec_to_rel_time(timer_value.it_value);
342
343     return 0;
344 }
345
346 /**
347  * fosa_timer_disarm()
348  *
349  * Disarm a timer
350  *
351  * The timer specified by timer is disarmed, and will not expire unless
352  * it is rearmed. If the timer was already disramed, the function has
353  * no effect.
354  *
355  * If the pointer remaining_time is != NULL, the remaining time before
356  * expiration will be returned in that pointer.  If the timer was
357  * disarmed a 0 value will be set.
358  *
359  * Returns 0 if successful; otherwise it returns an error code:
360  *    EINVAL: the value of timerid or value is invalid
361  *
362  * Alternatively, in case of error the implementation is allowed to
363  * notify it to the system console and then terminate the FRSH
364  * implementation and dependant applications
365  **/
366 int fosa_timer_disarm
367    (fosa_timer_id_t timerid,
368     fosa_rel_time_t *remaining_time)
369 {
370   struct itimerspec timer_value;
371   int error_code;
372
373   if (remaining_time!=NULL) {
374     error_code=timer_gettime(timerid,&timer_value);
375     if (error_code==-1) return errno;
376     *remaining_time = fosa_timespec_to_rel_time(timer_value.it_value);
377   }
378   timer_value.it_value=zero_time;
379   timer_value.it_interval=zero_time;
380   return timer_settime(timerid,0,&timer_value,NULL);
381 }