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