]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_partikle/fosa_clocks_and_timers.c
New way of handling long jumps
[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 int fosa_timer_create
91     (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
92      fosa_timer_id_t *timerid)
93  {
94         struct sigevent se;
95         struct sigaction act, oact;
96
97         sigaction (signal, NULL, &oact);
98         act = oact;
99         act.sa_flags = SA_SIGINFO;      // accept signal queuing
100         sigaction (signal, &act, NULL);
101         
102         se.sigev_notify = SIGEV_SIGNAL;
103         se.sigev_signo = signal;
104         
105         se.sigev_value.sival_int = info.sival_int;
106         se.sigev_value.sival_ptr = info.sival_ptr;
107
108         return timer_create (clockid, &se, timerid);
109 }
110
111
112 int fosa_timer_create_with_receiver
113     (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
114      fosa_timer_id_t *timerid, fosa_thread_id_t receiver)
115 {
116   return fosa_timer_create (clockid, signal, info, timerid);
117 }
118
119 int fosa_timer_delete(fosa_timer_id_t timerid)
120 {
121         return timer_delete (timerid);
122 }
123
124
125 int fosa_rel_timer_arm (fosa_timer_id_t timerid, const fosa_rel_time_t *value)
126 {
127         struct itimerspec tvalue; 
128         
129         tvalue.it_value = fosa_rel_time_to_timespec(*value);
130         tvalue.it_interval = (struct timespec) {0,0};
131         
132         if (timer_settime (timerid, 0, &tvalue, NULL))
133                 return FOSA_EINVAL;
134         
135         return 0;
136 }
137
138 int fosa_abs_timer_arm (fosa_timer_id_t timerid, const fosa_abs_time_t *value)
139 {
140         struct itimerspec tvalue; 
141         
142         tvalue.it_value = fosa_abs_time_to_timespec(*value);
143         tvalue.it_interval = (struct timespec) {0,0};
144         
145         if (timer_settime (timerid, TIMER_ABSTIME, &tvalue, NULL))
146                 return FOSA_EINVAL;
147         
148         return 0;
149 }
150
151
152 int fosa_timer_get_remaining_time
153                 (fosa_timer_id_t timerid, fosa_rel_time_t *remaining_time)
154 {
155         struct itimerspec value;
156         
157         if (timer_gettime (timerid, &value))
158                 return FOSA_EINVAL;
159         
160         *remaining_time = fosa_timespec_to_rel_time (value.it_value);
161         return 0;
162 }
163
164
165 int fosa_timer_disarm (fosa_timer_id_t timerid, fosa_rel_time_t *remaining_time)
166 {
167         struct itimerspec null_timer, old;
168         
169         // NULL timer -> disarm
170         null_timer = (struct itimerspec) {
171                 .it_value = {0, 0},
172                 .it_interval = {0, 0},
173         };
174         
175         if (timer_settime (timerid, 0, &null_timer, &old))
176                 return FOSA_EINVAL;
177         
178         if (remaining_time)
179                 *remaining_time = fosa_timespec_to_rel_time (old.it_value);
180                 
181         return 0;
182 }
183