]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_aquosa/fosa_clocks_and_timers.c
move all the time related issues to FOSA absolute and relative data types.
[frescor/fosa.git] / src_aquosa / 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 //  As a special exception, if you include this header file into source
41 //  files to be compiled, this header file does not by itself cause
42 //  the resulting executable to be covered by the GNU General Public
43 //  License.  This exception does not however invalidate any other
44 //  reasons why the executable file might be covered by the GNU General
45 //  Public License.
46 // -----------------------------------------------------------------------
47 //
48 //==============================================
49 //  ********  ******    ********  **********
50 //  **///// /**    **  **//////  /**     /**
51 //  **      /**    ** /**        /**     /**
52 //  ******* /**    ** /********* /**********
53 //  **////  /**    ** ////////** /**//////**
54 //  **      /**    **        /** /**     /**
55 //  **      /**    **  ********  /**     /**
56 //  //       /******/  ////////   //      // 
57 //
58 // FOSA(Frescor Operating System Adaptation layer)
59 //================================================
60
61 #include "fosa_time.h"
62 #include "fosa_clocks_and_timers.h"
63
64 /*************************
65  * Timing: Clocks
66  *************************/
67
68 /**
69  * fosa_get_time()
70  *
71  * Get the time from a clock
72  *
73  * This function sets the variable pointed to by current_time to the
74  * current value of the clock specified by clockid, which may be the
75  * FOSA_CLOCK_REALTIME constant or a value obtained with
76  * fosa_get_cputime_clock()
77  *
78  * Returns 0 if successful; otherwise it returns an error code:
79  *     EINVAL: the value of clockid is invalid
80  **/
81 int fosa_clock_get_time(fosa_clock_id_t clockid,
82                         fosa_abs_time_t *current_time)
83 {
84         int error;
85         struct timespec current_time_tspec;
86
87         if ((error = clock_gettime(clockid, &current_time_tspec)) != 0)
88                 return error;
89
90         *current_time = fosa_timespec_to_abs_time(current_time_tspec);
91
92         return 0;
93 }
94
95 /**
96  * fosa_get_cputime_clock()
97  *
98  * Get the identifier of a cpu-time clock
99  *
100  * This function stores in the variable pointed to by clockid the
101  * identifier of a cpu-time clock for the thread specified by tid.
102  *
103  * Returns 0 if successful; otherwise it returns an error code:
104  *    EINVAL: the value of tid is invalid
105  **/
106 int fosa_thread_get_cputime_clock(fosa_thread_id_t tid,
107                                   fosa_clock_id_t *clockid)
108 {
109         if (tid.linux_pid == tid.linux_tid) {
110                 /* standard UNIX process */
111                 *clockid = FOSA_CLOCK_REALTIME;
112                 return 0;
113         } else
114                 /* POSIX thread */
115                 return pthread_getcpuclockid(tid.pthread_id, clockid);
116 }
117
118 /*************************
119  * Timing: Timers
120  *************************/
121
122 /**
123  * fosa_create_timer()
124  *
125  * Create a one-shot timer
126  *
127  * This function creates a timer based on the clock specified by clock,
128  * and associates to this timer a notification mechanism consisting of
129  * a signal and associated information. Initially, the timer is in the
130  * disarmed state, i.e., not counting time. It can be armed to start
131  * counting time with fosa_timer_arm().
132  *
133  * The function stores the identifier of the newly created timer in the
134  * variable pointed to by timerid.
135  *
136  * When the timer expires, the signal number specified by signal will be
137  * sent together with the information specified by info, to the thread
138  * that armed the timer (@see fosa_timer_arm()).
139  *
140  * Note that, since this is a POSIX implementation, the signal will be sent
141  * to any thread waiting fot it (in a given UNIX process).
142  * No signal-waiting thread or similar strategy is implemented, the specific
143  * thread can be choosed (for example) by means of the signal number.
144  *
145  * Returns 0 if successful; otherwise it returns an error code:
146  *     EINVAL: the value of clockid or signal is invalid
147  *
148  *     EAGAIN: the system lacks enough resources to create the timer
149  **/
150 int fosa_timer_create(fosa_clock_id_t clockid,
151                       fosa_signal_t signal,
152                       fosa_signal_info_t info,
153                       fosa_timer_id_t *timerid)
154 {
155         struct sigevent event;
156
157         event.sigev_notify = SIGEV_SIGNAL;
158         event.sigev_signo = signal;
159         //event.sigev_value.sival_int = info.sival_int;
160         event.sigev_value = *((union sigval*) &info);
161
162         return timer_create(clockid, &event, timerid);
163 }
164
165 /**
166  * fosa_timer_create_with_receiver()
167  *
168  * Create a one-shot timer with a specific signal receiver thread
169  *
170  * This function creates a timer in the same way as fosa_timer_create,
171  * except that the signal generated when the timer expires is sent to
172  * the thread specified by receiver
173  * 
174  * In this implementation, since in POSIX we can not specify a receiver,
175  * always returns ENOSYS.
176  * 
177  * You have to use the "simple" fosa_timer_create. 
178  **/
179  int fosa_timer_create_with_receiver(fosa_clock_id_t clockid,
180                                      fosa_signal_t signal,
181                                      fosa_signal_info_t info,
182                                      fosa_timer_id_t *timerid,
183                                      fosa_thread_id_t receiver)
184 {
185         /* in POSIX the receiver thread cannot be specified!! */
186         //return fosa_timer_create(clockid, signal, info, timerid);
187         return ENOSYS;
188 }
189
190 /**
191  * Delete a timer
192  *
193  * The function deletes the timer specified by timerid, which becomes
194  * unusable. If the timer was armed, it is automatically disarmed before
195  * deletion.
196  *
197  * Returns 0 if successful; otherwise it returns an error code:
198  *     EINVAL: the value of timerid is not valid
199  **/
200 int fosa_timer_delete(fosa_timer_id_t  timerid)
201 {
202         return timer_delete(timerid);
203 }
204
205 /**
206  * fosa_timer_arm()
207  *
208  * Arm a timer
209  *
210  * The timer specified by timer is armed and starts counting time.
211  *
212  * If abstime is true, the value pointed to by value is the absolute
213  * time at which the timer will expire. If value specifies a time instant
214  * in the past, the timer expires immediately.
215  *
216  * If abstime is false, the value pointed to by value is the relative interval
217  * that must elapse for the timer to expire.
218  *
219  * In both cases, absolute or relative, the time is measured with the clock
220  * associated with the timer when it was created.
221  *
222  * If the timer was already armed, the previous time or interval is discarded
223  * and the timer is rearmed with the new value.
224  *
225  * When the timer expires, it is disarmed.
226  *
227  * Returns 0 if successful; otherwise it returns an error code:
228  *    EINVAL: the value of timerid or value is invalid
229  **/
230 int fosa_timer_arm (fosa_timer_id_t timerid,
231                     bool abstime,
232                     const fosa_abs_time_t *value)
233 {
234         struct itimerspec when;
235
236         /* non-periodic one shot timer configuration */
237         when.it_value = fosa_abs_time_to_timespec(*value);
238         when.it_interval.tv_sec = 0;
239         when.it_interval.tv_nsec = 0;
240
241         return timer_settime(timerid,
242                              (abstime ? TIMER_ABSTIME : 0),
243                              &when,
244                              NULL);
245 }
246
247 /**
248  * fosa_timer_get_remaining_time()
249  *
250  * Get the remaining time for timer expiration
251  *
252  * Returns the relative remaining time for timer expiration.  If the
253  * clock is a CPU clock it returns the time as if the thread was
254  * executing constantly.
255  *
256  * If the timer is disarmed it returns 0.
257  *
258  * Returns 0 if successful; otherwise it returns an error code:
259  *    EINVAL: the value of timerid or value is invalid
260  **/
261 int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
262                                   fosa_rel_time_t *remaining_time)
263 {
264         int error;
265         struct itimerspec time;
266
267         if (remaining_time != NULL) {
268                 if ((error = timer_gettime(timerid, &time)) == -1)
269                         return error;
270
271                 *remaining_time = fosa_timespec_to_rel_time(time.it_value);
272         } else
273                 return EINVAL;
274
275         return 0;
276 }
277
278 /**
279  * fosa_timer_disarm()
280  *
281  * Disarm a timer
282  *
283  * The timer specified by timer is disarmed, and will not expire unless
284  * it is rearmed. If the timer was already disramed, the function has
285  * no effect.
286  *
287  * If the pointer remaining_time is != NULL, the remaining time before
288  * expiration will be returned in that pointer.  If the timer was
289  * disarmed a 0 value will be set.
290  *
291  * Returns 0 if successful; otherwise it returns an error code:
292  *    EINVAL: the value of timerid or value is invalid
293  **/
294 int fosa_timer_disarm(fosa_timer_id_t timerid,
295                       struct timespec *remaining_time)
296 {
297         int error;
298         struct itimerspec time;
299
300         if (remaining_time != NULL) {
301                 if ((error = timer_gettime(timerid, &time)) == -1)
302                         return error;
303
304                 *remaining_time = fosa_timespec_to_rel_time(time.it_value);
305         }
306
307         time.it_value.tv_sec = 0;
308         time.it_value.tv_nsec = 0;
309
310         return timer_settime(timerid, 0, &time, NULL);
311 }
312