]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_aquosa/fosa_clocks_and_timers.c
Fixed a typo in fosa_clock_get_time() function name.
[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 static const struct timespec zero_time={0,0};
65
66
67 /*************************
68  * Timing: Clocks
69  *************************/
70
71 /**
72  * fosa_clock_get_time()
73  *
74  * Get the time from a clock
75  *
76  * This function sets the variable pointed to by current_time to the
77  * current value of the clock specified by clockid, which may be the
78  * FOSA_CLOCK_REALTIME constant or a value obtained with
79  * fosa_get_cputime_clock()
80  *
81  * Returns 0 if successful; otherwise it returns an error code:
82  *     EINVAL: the value of clockid is invalid
83  **/
84 int fosa_clock_get_time(fosa_clock_id_t clockid,
85                         fosa_abs_time_t *current_time)
86 {
87         int ret;
88         struct timespec current_time_tspec;
89
90         ret = clock_gettime(clockid, &current_time_tspec);
91         if (ret) return errno;
92
93         *current_time = fosa_timespec_to_abs_time(current_time_tspec);
94
95         return 0;
96 }
97
98 /**
99  * fosa_get_cputime_clock()
100  *
101  * Get the identifier of a cpu-time clock
102  *
103  * This function stores in the variable pointed to by clockid the
104  * identifier of a cpu-time clock for the thread specified by tid.
105  *
106  * Returns 0 if successful; otherwise it returns an error code:
107  *    EINVAL: the value of tid is invalid
108  **/
109 int fosa_thread_get_cputime_clock(fosa_thread_id_t tid,
110                                   fosa_clock_id_t *clockid)
111 {
112         int ret;
113
114         if (tid.linux_pid == tid.linux_tid) /* standard UNIX process */
115                 ret = clock_getcpuclockid(tid.linux_pid, clockid);
116         else /* POSIX thread */
117                 ret = pthread_getcpuclockid(tid.pthread_id, clockid);
118
119         return ret ? errno : 0;
120 }
121
122 /*************************
123  * Timing: Timers
124  *************************/
125
126 /**
127  * fosa_create_timer()
128  *
129  * Create a one-shot timer
130  *
131  * This function creates a timer based on the clock specified by clock,
132  * and associates to this timer a notification mechanism consisting of
133  * a signal and associated information. Initially, the timer is in the
134  * disarmed state, i.e., not counting time. It can be armed to start
135  * counting time with fosa_timer_arm().
136  *
137  * The function stores the identifier of the newly created timer in the
138  * variable pointed to by timerid.
139  *
140  * When the timer expires, the signal number specified by signal will be
141  * sent together with the information specified by info, to the thread
142  * that armed the timer (@see fosa_timer_arm()).
143  *
144  * Note that, since this is a POSIX implementation, the signal will be sent
145  * to any thread waiting fot it (in a given UNIX process).
146  * No signal-waiting thread or similar strategy is implemented, the specific
147  * thread can be choosed (for example) by means of the signal number.
148  *
149  * Returns 0 if successful; otherwise it returns an error code:
150  *     EINVAL: the value of clockid or signal is invalid
151  *
152  *     EAGAIN: the system lacks enough resources to create the timer
153  **/
154 int fosa_timer_create(fosa_clock_id_t clockid,
155                       fosa_signal_t signal,
156                       fosa_signal_info_t info,
157                       fosa_timer_id_t *timerid)
158 {
159         int ret;
160         struct sigevent event;
161
162         event.sigev_notify = SIGEV_SIGNAL;
163         event.sigev_signo = signal;
164         event.sigev_value = *((union sigval*) &info);
165
166         ret = timer_create(clockid, &event, timerid);
167
168         return ret ? errno : 0;
169 }
170
171 /**
172  * fosa_timer_create_with_receiver()
173  *
174  * Create a one-shot timer with a specific signal receiver thread
175  *
176  * This function creates a timer in the same way as fosa_timer_create,
177  * except that the signal generated when the timer expires is sent to
178  * the thread specified by receiver
179  * 
180  * You have to use the "simple" fosa_timer_create. 
181  **/
182  int fosa_timer_create_with_receiver(fosa_clock_id_t clockid,
183                                      fosa_signal_t signal,
184                                      fosa_signal_info_t info,
185                                      fosa_timer_id_t *timerid,
186                                      fosa_thread_id_t receiver)
187 {
188         int ret;
189         struct sigevent event;
190
191         event.sigev_notify = SIGEV_SIGNAL;
192         event.sigev_signo = signal;
193         event.sigev_value = *((union sigval*) &info);
194         event.sigev_notify = SIGEV_THREAD_ID | SIGEV_SIGNAL;
195         event._sigev_un._tid = receiver.linux_tid;
196
197         ret = timer_create(clockid, &event, timerid);
198
199         return ret ? errno : 0;
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 int fosa_timer_delete(fosa_timer_id_t timerid)
213 {
214         int ret;
215
216         ret = timer_delete(timerid);
217
218         return ret ? errno : 0;
219 }
220
221 /**
222  * fosa_rel_timer_arm()
223  *
224  * Arm a timer with a relative time interval
225  *
226  * The timer specified by timer is armed and starts counting time.
227  *
228  * The value pointed to by value is the relative interval that must
229  * elapse for the timer to expire.  Negative values cause the timer to
230  * expire immediately.
231  *
232  * The time is measured with the clock associated with the timer when
233  * it was created. 
234  *
235  * If the timer was already armed, the previous time or interval is discarded
236  * and the timer is rearmed with the new value.
237  *
238  * When the timer expires, it is disarmed.
239  *
240  * Returns 0 if successful; otherwise it returns an error code:
241  *    FOSA_EINVAL: the value of timerid or value is invalid
242  *
243  * Alternatively, in case of error the implementation is allowed to
244  * notify it to the system console and then terminate the FRSH
245  * implementation and dependant applications
246  **/
247 int fosa_rel_timer_arm(fosa_timer_id_t timerid, const fosa_rel_time_t *value)
248 {
249         int ret;
250         struct itimerspec when;
251
252         /* non-periodic one shot timer */
253         when.it_value = fosa_abs_time_to_timespec(*value);
254         when.it_interval = zero_time;
255
256         ret = timer_settime(timerid, 0, &when, NULL);
257
258         return ret ? errno : 0;
259 }
260
261
262 /**
263  * fosa_abs_timer_arm()
264  *
265  * Arm a timer that will expire in an absolute time instant.
266  *
267  * The timer specified by timer is armed and starts counting time.
268  *
269  * The value pointed to by value is the absolute time at which the
270  * timer will expire. If value specifies a time instant in the past,
271  * the timer expires immediately. 
272  *
273  * The time is measured with the clock associated with the timer when
274  * it was created. 
275  *
276  * If the timer was already armed, the previous time or interval is discarded
277  * and the timer is rearmed with the new value.
278  *
279  * When the timer expires, it is disarmed.
280  *
281  * Returns 0 if successful; otherwise it returns an error code:
282  *    FOSA_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_abs_timer_arm(fosa_timer_id_t timerid, const fosa_abs_time_t *value)
289 {
290         int ret;
291         struct itimerspec when;
292
293         /* non-periodic one shot timer */
294         when.it_value = fosa_abs_time_to_timespec(*value);
295         when.it_interval = zero_time;
296
297         ret = timer_settime(timerid, TIMER_ABSTIME, &when, NULL);
298
299         return ret ? errno : 0;
300 }
301
302 /**
303  * fosa_timer_get_remaining_time()
304  *
305  * Get the remaining time for timer expiration
306  *
307  * Returns the relative remaining time for timer expiration.  If the
308  * clock is a CPU clock it returns the time as if the thread was
309  * executing constantly.
310  *
311  * If the timer is disarmed it returns 0.
312  *
313  * Returns 0 if successful; otherwise it returns an error code:
314  *    EINVAL: the value of timerid or value is invalid
315  **/
316 int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
317                                   fosa_rel_time_t *remaining_time)
318 {
319         int ret;
320         struct itimerspec time;
321
322         if (!remaining_time)
323                 return FOSA_EINVAL;
324
325         ret = timer_gettime(timerid, &time);
326         *remaining_time = fosa_timespec_to_rel_time(time.it_value);
327
328         return ret ? errno : 0;
329 }
330
331 /**
332  * fosa_timer_disarm()
333  *
334  * Disarm a timer
335  *
336  * The timer specified by timer is disarmed, and will not expire unless
337  * it is rearmed. If the timer was already disramed, the function has
338  * no effect.
339  *
340  * If the pointer remaining_time is != NULL, the remaining time before
341  * expiration will be returned in that pointer.  If the timer was
342  * disarmed a 0 value will be set.
343  *
344  * Returns 0 if successful; otherwise it returns an error code:
345  *    EINVAL: the value of timerid or value is invalid
346  **/
347 int fosa_timer_disarm(fosa_timer_id_t timerid,
348                       struct timespec *remaining_time)
349 {
350         int ret;
351         struct itimerspec time;
352
353         if (!remaining_time)
354                 return FOSA_EINVAL;
355
356         ret = timer_gettime(timerid, &time);
357         if (ret) return errno;
358
359         *remaining_time = fosa_timespec_to_rel_time(time.it_value);
360
361         time.it_value = zero_time;
362         time.it_interval = zero_time;
363
364         ret = timer_settime(timerid, 0, &time, NULL);
365
366         return ret ? errno : 0;
367 }
368