]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/fosa_clocks_and_timers.c
updates of michael and test_fosa_ads which does not work yet
[frescor/fosa.git] / src_marte / 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 // -----------------------------------------------------------------------
41 //fosa_clocks_and_timers.c
42 //==============================================
43 //  ********  ******    ********  **********
44 //  **///// /**    **  **//////  /**     /**
45 //  **      /**    ** /**        /**     /**
46 //  ******* /**    ** /********* /**********
47 //  **////  /**    ** ////////** /**//////**
48 //  **      /**    **        /** /**     /**
49 //  **      /**    **  ********  /**     /**
50 //  //       /******/  ////////   //      // 
51 //
52 // FOSA(Frescor Operating System Adaptation layer)
53 //================================================
54
55
56 #include "fosa_clocks_and_timers.h"
57 #include <pthread.h>
58 #include <time.h>
59 #include <stdio.h>
60
61 static const struct timespec zero_time={0,0};
62
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  * Alternatively, in case of error the implementation is allowed to
82  * notify it to the system console and then terminate the FRSH
83  * implementation and dependant applications
84  **/
85 int fosa_clock_get_time(fosa_clock_id_t clockid, struct timespec *current_time)
86 {
87   return clock_gettime(clockid,current_time);
88 }
89
90
91 /**
92  * fosa_get_cputime_clock()
93  *
94  * Get the identifier of a cpu-time clock
95  *
96  * This function stores in the variable pointed to by clockid the 
97  * identifier of a cpu-time clock for the thread specified by tid.
98  *
99  * Returns 0 if successful; otherwise it returns an error code:
100  *    EINVAL: the value of tid is invalid
101  * 
102  * Alternatively, in case of error the implementation is allowed to
103  * notify it to the system console and then terminate the FRSH
104  * implementation and dependant applications
105  **/
106 int fosa_thread_get_cputime_clock
107     (frsh_thread_id_t tid, fosa_clock_id_t *clockid)
108 {
109   return pthread_getcpuclockid(tid,clockid);
110 }
111
112
113 /*************************
114  * Timing: Timers
115  *************************/
116
117 /**
118  * fosa_create_timer()
119  *
120  * Create a one-shot timer
121  *
122  * This function creates a timer based on the clock specified by clock,
123  * and associates to this timer a notification mechanism consisting of
124  * a signal and associated information. Initially, the timer is in the
125  * disarmed state, i.e., not counting time. It can be armed to start
126  * counting time with fosa_timer_arm().
127  *
128  * The function stores the identifier of the newly created timer in the 
129  * variable pointed to by timerid.
130  *
131  * When the timer expires, the signal number specified by signal will be
132  * sent together with the information specified by info, to the thread
133  * that armed the timer (@see fosa_timer_arm()). 
134  *
135  * In those implementations that do not support queueing a
136  * signal with information to a thread (such as POSIX), the signal may
137  * be sent to any thread that is waiting for this signal via
138  * fosa_signal_wait(). Portability can be ensured by having the receiver
139  * thread be the one who is waiting for the signal. 
140  * 
141  * Returns 0 if successful; otherwise it returns an error code:
142  *     EINVAL: the value of clockid or signal is invalid
143  * 
144  *     EAGAIN: the system lacks enough resources to create the timer
145  *
146  * Alternatively, in case of error the implementation is allowed to
147  * notify it to the system console and then terminate the FRSH
148  * implementation and dependant applications
149  **/
150  int fosa_timer_create
151       (fosa_clock_id_t clockid, frsh_signal_t signal, frsh_signal_info_t info,
152        fosa_timer_id_t *timerid)
153 {
154   struct sigevent evp;
155   evp.sigev_notify=SIGEV_SIGNAL;
156   evp.sigev_signo=signal;
157   evp.sigev_value=* ((union sigval *) &info);
158   // the above casting construct is used to overcome the compiler
159   // restriction that does not allow casts between unions
160   return timer_create(clockid,&evp,timerid);
161 }
162
163 /**
164  * Delete a timer
165  * 
166  * The function deletes the timer specified by timerid, which becomes 
167  * unusable. If the timer was armed, it is automatically disarmed before
168  * deletion.
169  * 
170  * Returns 0 if successful; otherwise it returns an error code:
171  *     EINVAL: the value of timerid is not valid
172  *
173  * Alternatively, in case of error the implementation is allowed to
174  * notify it to the system console and then terminate the FRSH
175  * implementation and dependant applications
176  **/
177 int fosa_timer_delete(fosa_timer_id_t timerid)
178 {
179   return timer_delete(timerid);
180 }
181
182 /**
183  * fosa_timer_arm()
184  *
185  * Arm a timer
186  *
187  * The timer specified by timer is armed and starts counting time.
188  *
189  * If abstime is true, the value pointed to by value is the absolute
190  * time at which the timer will expire. If value specifies a time instant 
191  * in the past, the timer expires immediately.
192  *
193  * If abstime is false, the value pointed to by value is the relative interval
194  * that must elapse for the timer to expire. 
195  *
196  * In both cases, absolute or relative, the time is measured with the clock 
197  * associated with the timer when it was created.
198  *
199  * If the timer was already armed, the previous time or interval is discarded
200  * and the timer is rearmed with the new value.
201  *
202  * When the timer expires, it is disarmed.
203  *
204  * Returns 0 if successful; otherwise it returns an error code:
205  *    EINVAL: the value of timerid or value is invalid
206  *
207  * Alternatively, in case of error the implementation is allowed to
208  * notify it to the system console and then terminate the FRSH
209  * implementation and dependant applications
210  **/
211 int fosa_timer_arm
212       (fosa_timer_id_t timerid, bool abstime,
213        const struct timespec *value)
214 {
215   int timer_abstime=0;
216   struct itimerspec timer_value;  
217
218   // set the abstime flag if necessary
219   if (abstime) {
220     timer_abstime=TIMER_ABSTIME;
221   }
222
223   // set the timer to the specified value, one shot only
224   timer_value.it_value=*value;
225   timer_value.it_interval=zero_time;
226
227   // arm the timer
228   return timer_settime(timerid,timer_abstime,&timer_value,NULL);
229 }
230
231 /**
232  * fosa_timer_disarm()
233  *
234  * Disarm a timer
235  * 
236  * The timer specified by timer is disarmed, and will not expire unless 
237  * it is rearmed. If the timer was already disramed, the function has 
238  * no effect.
239  *
240  * If the pointer remaining_time is != NULL, the remaining time before
241  * expiration will be returned in that pointer.  If the timer was
242  * disarmed a 0 value will be set.
243  *
244  * Returns 0 if successful; otherwise it returns an error code:
245  *    EINVAL: the value of timerid or value is invalid
246  *
247  * Alternatively, in case of error the implementation is allowed to
248  * notify it to the system console and then terminate the FRSH
249  * implementation and dependant applications
250  **/
251 int fosa_timer_disarm
252    (fosa_timer_id_t timerid, 
253     struct timespec *remaining_time)
254 {
255   struct itimerspec timer_value;  
256   int error_code;
257
258   if (remaining_time!=NULL) {
259     error_code=timer_gettime(timerid,&timer_value);
260     if (error_code==-1) return errno;
261     *remaining_time=timer_value.it_value;
262   }
263   timer_value.it_value=zero_time;
264   timer_value.it_interval=zero_time;
265   return timer_settime(timerid,0,&timer_value,NULL);
266 }