]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_partikle/fosa_clocks_and_timers.c
a5e051c31962ea96232c28fc125f5066f1bd11ea
[frescor/fosa.git] / src_partikle / 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 // FOSA(Frescor Operating System Adaptation layer)
58 //================================================
59
60 #include <fosa_clocks_and_timers.h>
61 #include <fosa_time.h>
62 #include <stdlib.h>
63
64 /*************************
65  * Timing: Clocks
66  *************************/
67 int fosa_clock_get_time(fosa_clock_id_t clockid, fosa_abs_time_t *current_time)
68 {       
69         int err;
70         struct timespec now;
71         
72         err = clock_gettime (clockid, &now);
73         *current_time = fosa_timespec_to_abs_time(now);
74         
75         return 0;
76 }
77
78
79 int fosa_thread_get_cputime_clock(fosa_thread_id_t tid, fosa_clock_id_t *clockid)
80 {
81   return pthread_getcpuclockid (tid, clockid);
82 }
83
84
85
86
87 /*************************
88  * Timing: Timers
89  *************************/
90 void void_handler (int signo) {};
91 int fosa_timer_create
92     (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
93      fosa_timer_id_t *timerid)
94  {
95         struct sigevent se;
96         struct sigaction act;
97
98         // Assumption: the signal should accept queued values
99         act.sa_handler = void_handler;
100         sigfillset (&act.sa_mask);      
101         act.sa_flags = SA_SIGINFO;      // accept signal queuing
102         sigaction (signal, &act, NULL);
103         
104         se.sigev_notify = SIGEV_SIGNAL;
105         se.sigev_signo = signal;
106         
107         se.sigev_value.sival_int = info.sival_int;
108         se.sigev_value.sival_ptr = info.sival_ptr;
109
110         return timer_create (clockid, &se, timerid);
111 }
112
113
114 int fosa_timer_create_with_receiver
115     (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
116      fosa_timer_id_t *timerid, fosa_thread_id_t receiver)
117 {
118   return fosa_timer_create (clockid, signal, info, timerid);
119 }
120
121 int fosa_timer_delete(fosa_timer_id_t timerid)
122 {
123         return timer_delete (timerid);
124 }
125
126
127 int fosa_rel_timer_arm (fosa_timer_id_t timerid, const fosa_rel_time_t *value)
128 {
129         struct itimerspec tvalue; 
130         
131         tvalue.it_value = fosa_rel_time_to_timespec(*value);
132         tvalue.it_interval = (struct timespec) {0,0};
133         
134         if (timer_settime (timerid, 0, &tvalue, NULL))
135                 return FOSA_EINVAL;
136         
137         return 0;
138 }
139
140 int fosa_abs_timer_arm (fosa_timer_id_t timerid, const fosa_abs_time_t *value)
141 {
142         struct itimerspec tvalue; 
143         
144         tvalue.it_value = fosa_abs_time_to_timespec(*value);
145         tvalue.it_interval = (struct timespec) {0,0};
146         
147         if (timer_settime (timerid, TIMER_ABSTIME, &tvalue, NULL))
148                 return FOSA_EINVAL;
149         
150         return 0;
151 }
152
153
154 int fosa_timer_get_remaining_time
155                 (fosa_timer_id_t timerid, fosa_rel_time_t *remaining_time)
156 {
157         struct itimerspec value;
158         
159         if (timer_gettime (timerid, &value))
160                 return FOSA_EINVAL;
161         
162         *remaining_time = fosa_timespec_to_rel_time (value.it_value);
163         return 0;
164 }
165
166
167 int fosa_timer_disarm (fosa_timer_id_t timerid, fosa_rel_time_t *remaining_time)
168 {
169         struct itimerspec null_timer, old;
170         
171         // NULL timer -> disarm
172         null_timer = (struct itimerspec) {
173                 .it_value = {0, 0},
174                 .it_interval = {0, 0},
175         };
176         
177         if (timer_settime (timerid, 0, &null_timer, &old))
178                 return FOSA_EINVAL;
179         
180         if (remaining_time)
181                 *remaining_time = fosa_timespec_to_rel_time (old.it_value);
182                 
183         return 0;
184 }
185