]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/fosa_clocks_and_timers.c
Adding FOSA_CPP_BEING|END_DECLS to allow interfacing with C++
[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_clocks_and_timers.h"
72
73 static const struct timespec zero_time={0,0};
74
75
76 /*************************
77  * Timing: Clocks
78  *************************/
79
80 /**
81  * fosa_get_time()
82  *
83  * Get the time from a clock
84  *
85  * This function sets the variable pointed to by current_time to the
86  * current value of the clock specified by clockid, which may be the
87  * FOSA_CLOCK_REALTIME constant or a value obtained with
88  * fosa_get_cputime_clock()
89  *
90  * Returns 0 if successful; otherwise it returns an error code:
91  *     EINVAL: the value of clockid is invalid
92  *
93  * Alternatively, in case of error the implementation is allowed to
94  * notify it to the system console and then terminate the FRSH
95  * implementation and dependant applications
96  **/
97 int fosa_clock_get_time(fosa_clock_id_t clockid, struct timespec *current_time)
98 {
99   return clock_gettime(clockid,current_time);
100 }
101
102
103 /**
104  * fosa_get_cputime_clock()
105  *
106  * Get the identifier of a cpu-time clock
107  *
108  * This function stores in the variable pointed to by clockid the
109  * identifier of a cpu-time clock for the thread specified by tid.
110  *
111  * Returns 0 if successful; otherwise it returns an error code:
112  *    EINVAL: the value of tid is invalid
113  *
114  * Alternatively, in case of error the implementation is allowed to
115  * notify it to the system console and then terminate the FRSH
116  * implementation and dependant applications
117  **/
118 int fosa_thread_get_cputime_clock
119     (fosa_thread_id_t tid, fosa_clock_id_t *clockid)
120 {
121   return pthread_getcpuclockid(tid,clockid);
122 }
123
124
125 /*************************
126  * Timing: Timers
127  *************************/
128
129 /**
130  * fosa_create_timer()
131  *
132  * Create a one-shot timer
133  *
134  * This function creates a timer based on the clock specified by clock,
135  * and associates to this timer a notification mechanism consisting of
136  * a signal and associated information. Initially, the timer is in the
137  * disarmed state, i.e., not counting time. It can be armed to start
138  * counting time with fosa_timer_arm().
139  *
140  * The function stores the identifier of the newly created timer in the
141  * variable pointed to by timerid.
142  *
143  * When the timer expires, the signal number specified by signal will be
144  * sent together with the information specified by info, to the thread
145  * that armed the timer (@see fosa_timer_arm()).
146  *
147  * In those implementations that do not support queueing a
148  * signal with information to a thread (such as POSIX), the signal may
149  * be sent to any thread that is waiting for this signal via
150  * fosa_signal_wait(). Portability can be ensured by having the receiver
151  * thread be the one who is waiting for the signal.
152  *
153  * Returns 0 if successful; otherwise it returns an error code:
154  *     EINVAL: the value of clockid or signal is invalid
155  *
156  *     EAGAIN: the system lacks enough resources to create the timer
157  *
158  * Alternatively, in case of error the implementation is allowed to
159  * notify it to the system console and then terminate the FRSH
160  * implementation and dependant applications
161  **/
162  int fosa_timer_create
163       (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
164        fosa_timer_id_t *timerid)
165 {
166   struct sigevent evp;
167   evp.sigev_notify=SIGEV_SIGNAL;
168   evp.sigev_signo=signal;
169   evp.sigev_value=* ((union sigval *) &info);
170   // the above casting construct is used to overcome the compiler
171   // restriction that does not allow casts between unions
172   return timer_create(clockid,&evp,timerid);
173 }
174
175 /**
176  * fosa_timer_create_with_receiver()
177  *
178  * Create a one-shot timer with a specific signal receiver thread
179  *
180  * This function creates a timer in the same way as fosa_timer_create,
181  * except that the signal generated when the timer expires is sent to
182  * the thread specified by receiver
183  *
184  * Returns 0 if successful; otherwise it returns an error code:
185  *     FOSA_EINVAL: the value of clockid or signal is invalid
186  *
187  *     FOSA_EAGAIN: the system lacks enough resources to create the timer
188  *
189  * Alternatively, in case of error the implementation is allowed to
190  * notify it to the system console and then terminate the FRSH
191  * implementation and dependant applications
192  **/
193  int fosa_timer_create_with_receiver
194       (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
195        fosa_timer_id_t *timerid, fosa_thread_id_t receiver)
196 {
197   // in POSIX the receiver thread cannot be specified
198   return fosa_timer_create(clockid,signal,info,timerid);
199 }
200
201
202 /**
203  * Delete a timer
204  *
205  * The function deletes the timer specified by timerid, which becomes
206  * unusable. If the timer was armed, it is automatically disarmed before
207  * deletion.
208  *
209  * Returns 0 if successful; otherwise it returns an error code:
210  *     EINVAL: the value of timerid is not valid
211  *
212  * Alternatively, in case of error the implementation is allowed to
213  * notify it to the system console and then terminate the FRSH
214  * implementation and dependant applications
215  **/
216 int fosa_timer_delete(fosa_timer_id_t timerid)
217 {
218   return timer_delete(timerid);
219 }
220
221 /**
222  * fosa_timer_arm()
223  *
224  * Arm a timer
225  *
226  * The timer specified by timer is armed and starts counting time.
227  *
228  * If abstime is true, the value pointed to by value is the absolute
229  * time at which the timer will expire. If value specifies a time instant
230  * in the past, the timer expires immediately.
231  *
232  * If abstime is false, the value pointed to by value is the relative interval
233  * that must elapse for the timer to expire.
234  *
235  * In both cases, absolute or relative, the time is measured with the clock
236  * associated with the timer when it was created.
237  *
238  * If the timer was already armed, the previous time or interval is discarded
239  * and the timer is rearmed with the new value.
240  *
241  * When the timer expires, it is disarmed.
242  *
243  * Returns 0 if successful; otherwise it returns an error code:
244  *    EINVAL: the value of timerid or value is invalid
245  *
246  * Alternatively, in case of error the implementation is allowed to
247  * notify it to the system console and then terminate the FRSH
248  * implementation and dependant applications
249  **/
250 int fosa_timer_arm
251       (fosa_timer_id_t timerid, bool abstime,
252        const struct timespec *value)
253 {
254   int timer_abstime=0;
255   struct itimerspec timer_value;
256
257   // set the abstime flag if necessary
258   if (abstime) {
259     timer_abstime=TIMER_ABSTIME;
260   }
261
262   // set the timer to the specified value, one shot only
263   timer_value.it_value=*value;
264   timer_value.it_interval=zero_time;
265
266   // arm the timer
267   return timer_settime(timerid,timer_abstime,&timer_value,NULL);
268 }
269
270 /**
271  * fosa_timer_get_remaining_time()
272  *
273  * Get the remaining time for timer expiration
274  *
275  * Returns the relative remaining time for timer expiration.  If the
276  * clock is a CPU clock it returns the time as if the thread was
277  * executing constantly.
278  *
279  * If the timer is disarmed it returns 0.
280  *
281  * Returns 0 if successful; otherwise it returns an error code:
282  *    EINVAL: the value of timerid or value is invalid
283  *
284  * Alternatively, in case of error the implementation is allowed to
285  * notify it to the system console and then terminate the FRSH
286  * implementation and dependant applications
287  **/
288 int fosa_timer_get_remaining_time
289         (fosa_timer_id_t timerid, struct timespec *remaining_time)
290 {
291     struct itimerspec timer_value;
292     int error_code;
293
294     if (remaining_time!=NULL) {
295         error_code=timer_gettime(timerid,&timer_value);
296         if (error_code==-1) return errno;
297         *remaining_time=timer_value.it_value;
298     } else {
299         return EINVAL;
300     }
301     return 0;
302 }
303
304 /**
305  * fosa_timer_disarm()
306  *
307  * Disarm a timer
308  *
309  * The timer specified by timer is disarmed, and will not expire unless
310  * it is rearmed. If the timer was already disramed, the function has
311  * no effect.
312  *
313  * If the pointer remaining_time is != NULL, the remaining time before
314  * expiration will be returned in that pointer.  If the timer was
315  * disarmed a 0 value will be set.
316  *
317  * Returns 0 if successful; otherwise it returns an error code:
318  *    EINVAL: the value of timerid or value is invalid
319  *
320  * Alternatively, in case of error the implementation is allowed to
321  * notify it to the system console and then terminate the FRSH
322  * implementation and dependant applications
323  **/
324 int fosa_timer_disarm
325    (fosa_timer_id_t timerid,
326     struct timespec *remaining_time)
327 {
328   struct itimerspec timer_value;
329   int error_code;
330
331   if (remaining_time!=NULL) {
332     error_code=timer_gettime(timerid,&timer_value);
333     if (error_code==-1) return errno;
334     *remaining_time=timer_value.it_value;
335   }
336   timer_value.it_value=zero_time;
337   timer_value.it_interval=zero_time;
338   return timer_settime(timerid,0,&timer_value,NULL);
339 }