]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_aquosa/fosa_clocks_and_timers.c
Keep the FOSA implementation for the AQuoSA platform in touch
[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_clocks_and_timers.h"
62
63 /*************************
64  * Timing: Clocks
65  *************************/
66
67 int fosa_clock_get_time(fosa_clock_id_t clockid,
68                         struct timespec *current_time)
69 {
70         return clock_gettime(clockid, current_time);
71 }
72
73 int fosa_thread_get_cputime_clock(fosa_thread_id_t tid,
74                                   fosa_clock_id_t *clockid)
75 {
76         if (tid.linux_pid == tid.linux_tid) {
77                 /* standard UNIX process */
78                 *clockid = FOSA_CLOCK_REALTIME;
79                 return 0;
80         } else
81                 /* POSIX thread */
82                 return pthread_getcpuclockid(tid.pthread_id, clockid);
83 }
84
85 /*************************
86  * Timing: Timers
87  *************************/
88
89 /**
90  * fosa_create_timer()
91  *
92  * Create a one-shot timer
93  *
94  * This function creates a timer based on the clock specified by clock,
95  * and associates to this timer a notification mechanism consisting of
96  * a signal and associated information. Initially, the timer is in the
97  * disarmed state, i.e., not counting time. It can be armed to start
98  * counting time with fosa_timer_arm().
99  *
100  * The function stores the identifier of the newly created timer in the
101  * variable pointed to by timerid.
102  *
103  * When the timer expires, the signal number specified by signal will be
104  * sent together with the information specified by info, to the thread
105  * that armed the timer (@see fosa_timer_arm()).
106  *
107  * Note that, since this is a POSIX implementation, the signal will be sent
108  * to any thread waiting fot it (in a given UNIX process).
109  * No signal-waiting thread or similar strategy is implemented, the specific
110  * thread can be choosed (for example) by means of the signal number.
111  *
112  * Returns 0 if successful; otherwise it returns an error code:
113  *     EINVAL: the value of clockid or signal is invalid
114  *
115  *     EAGAIN: the system lacks enough resources to create the timer
116  **/
117 int fosa_timer_create(fosa_clock_id_t clockid,
118                       fosa_signal_t signal,
119                       fosa_signal_info_t info,
120                       fosa_timer_id_t *timerid)
121 {
122         struct sigevent event;
123
124         event.sigev_notify = SIGEV_SIGNAL;
125         event.sigev_signo = signal;
126         //event.sigev_value.sival_int = info.sival_int;
127         event.sigev_value = *((union sigval*) &info);
128
129         return timer_create(clockid, &event, timerid);
130 }
131
132 /**
133  * fosa_timer_create_with_receiver()
134  *
135  * Create a one-shot timer with a specific signal receiver thread
136  *
137  * This function creates a timer in the same way as fosa_timer_create,
138  * except that the signal generated when the timer expires is sent to
139  * the thread specified by receiver
140  * 
141  * In this implementation, since in POSIX we can not specify a receiver,
142  * always returns ENOSYS.
143  * 
144  * You have to use the "simple" fosa_timer_create. 
145  **/
146  int fosa_timer_create_with_receiver(fosa_clock_id_t clockid,
147                                      fosa_signal_t signal,
148                                      fosa_signal_info_t info,
149                                      fosa_timer_id_t *timerid,
150                                      fosa_thread_id_t receiver)
151 {
152         /* in POSIX the receiver thread cannot be specified!! */
153         //return fosa_timer_create(clockid, signal, info, timerid);
154         return ENOSYS;
155 }
156
157  /**
158   * Delete a timer
159   *
160   * The function deletes the timer specified by timerid, which becomes
161   * unusable. If the timer was armed, it is automatically disarmed before
162   * deletion.
163   *
164   * Returns 0 if successful; otherwise it returns an error code:
165   *     EINVAL: the value of timerid is not valid
166   **/
167 int fosa_timer_delete(fosa_timer_id_t  timerid)
168 {
169         return timer_delete(timerid);
170 }
171
172 /**
173  * fosa_timer_arm()
174  *
175  * Arm a timer
176  *
177  * The timer specified by timer is armed and starts counting time.
178  *
179  * If abstime is true, the value pointed to by value is the absolute
180  * time at which the timer will expire. If value specifies a time instant
181  * in the past, the timer expires immediately.
182  *
183  * If abstime is false, the value pointed to by value is the relative interval
184  * that must elapse for the timer to expire.
185  *
186  * In both cases, absolute or relative, the time is measured with the clock
187  * associated with the timer when it was created.
188  *
189  * If the timer was already armed, the previous time or interval is discarded
190  * and the timer is rearmed with the new value.
191  *
192  * When the timer expires, it is disarmed.
193  *
194  * Returns 0 if successful; otherwise it returns an error code:
195  *    EINVAL: the value of timerid or value is invalid
196  **/
197 int fosa_timer_arm (fosa_timer_id_t timerid,
198                     bool abstime,
199                     const struct timespec *value)
200 {
201         struct itimerspec when;
202
203         /* non-periodic one shot timer configuration */
204         when.it_value = *value;
205         when.it_interval.tv_sec = 0;
206         when.it_interval.tv_nsec = 0;
207
208         return timer_settime(timerid,
209                              (abstime ? TIMER_ABSTIME : 0),
210                              &when,
211                              NULL);
212 }
213
214 /**
215  * fosa_timer_get_remaining_time()
216  *
217  * Get the remaining time for timer expiration
218  *
219  * Returns the relative remaining time for timer expiration.  If the
220  * clock is a CPU clock it returns the time as if the thread was
221  * executing constantly.
222  *
223  * If the timer is disarmed it returns 0.
224  *
225  * Returns 0 if successful; otherwise it returns an error code:
226  *    EINVAL: the value of timerid or value is invalid
227  **/
228 int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
229                                   struct timespec *remaining_time)
230 {
231         int error;
232         struct itimerspec time;
233
234         if (remaining_time != NULL) {
235                 if ((error = timer_gettime(timerid, &time)) == -1)
236                         return error;
237
238                 *remaining_time = time.it_value;
239         } else
240                 return EINVAL;
241
242         return 0;
243 }
244
245 /**
246  * fosa_timer_disarm()
247  *
248  * Disarm a timer
249  *
250  * The timer specified by timer is disarmed, and will not expire unless
251  * it is rearmed. If the timer was already disramed, the function has
252  * no effect.
253  *
254  * If the pointer remaining_time is != NULL, the remaining time before
255  * expiration will be returned in that pointer.  If the timer was
256  * disarmed a 0 value will be set.
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_disarm(fosa_timer_id_t timerid,
262                       struct timespec *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 = time.it_value;
272         }
273
274         time.it_value.tv_sec = 0;
275         time.it_value.tv_nsec = 0;
276
277         return timer_settime(timerid, 0, &time, NULL);
278 }
279