]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_partikle/fosa_clocks_and_timers.c
dc536e4ab37e16763d343015e77f06512ba55aad
[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
62 /*************************
63  * Timing: Clocks
64  *************************/
65 int fosa_clock_get_time(fosa_clock_id_t clockid, struct timespec *current_time)
66 {
67         if (clock_gettime (clockid, current_time))
68                 return FOSA_EINVAL;
69         
70         return 0;
71 }
72
73
74 int fosa_thread_get_cputime_clock(fosa_thread_id_t tid, fosa_clock_id_t *clockid)
75 {
76   return pthread_getcpuclockid (tid, clockid);
77 }
78
79
80
81
82 /*************************
83  * Timing: Timers
84  *************************/
85 // #define FRSH_DEADLINE_NEWJOB_TIMEOUT   0x00050000
86 void void_handler (int signo) {};
87 int fosa_timer_create
88     (fosa_clock_id_t clockid, fosa_signal_t signal, fosa_signal_info_t info,
89      fosa_timer_id_t *timerid)
90  {
91         struct sigevent se;
92         struct sigaction act;
93
94         // Assumption: the signal should accept queued values
95         act.sa_handler = void_handler;
96         sigfillset (&act.sa_mask);      
97         act.sa_flags = SA_SIGINFO;      // accept signal queuing
98         sigaction (signal, &act, NULL);
99         
100         se.sigev_notify = SIGEV_SIGNAL;
101         se.sigev_signo = signal;
102         
103         se.sigev_value.sival_int = info.sival_int;
104         se.sigev_value.sival_ptr = info.sival_ptr;
105
106         return timer_create (clockid, &se, timerid);
107 /*
108         if ((info.sival_int & 0xFFFF0000) == FRSH_DEADLINE_NEWJOB_TIMEOUT)
109                 printf ("\n\ndeadline %d timer created: info=0x%x, signal=%d ()\n\n", *timerid, info.sival_int, signal);
110 */
111 }
112
113
114 int fosa_timer_delete(fosa_timer_id_t timerid)
115 {
116         return timer_delete (timerid);
117 }
118
119
120 int fosa_timer_arm
121                 (fosa_timer_id_t timerid, bool abstime,
122                  const struct timespec *value)
123 {
124         struct timespec now;
125         
126         clock_gettime (CLOCK_REALTIME, &now);
127         
128 //      printf ("(0x%x): Arm timer (%d). abstime=%d, value={%ld,%ld}, now={%ld, %ld}\n",\
129 //              pthread_self (), timerid, abstime, value -> tv_sec, value -> tv_nsec, now.tv_sec, now.tv_nsec);
130
131         struct itimerspec tvalue =
132         {
133                 .it_value = (struct timespec) *value,
134                 .it_interval = {0,0},
135         };
136         
137         if (timer_settime (timerid, abstime, &tvalue, NULL))
138                 return EINVAL;
139         
140         return 0;
141 }
142
143
144 int fosa_timer_get_remaining_time
145                 (fosa_timer_id_t timerid, struct timespec *remaining_time)
146 {
147         struct itimerspec value;
148         
149         if (timer_gettime (timerid, &value))
150                 return EINVAL;
151         
152         *remaining_time = value.it_value;
153         return 0;
154 }
155
156
157 int fosa_timer_disarm(fosa_timer_id_t timerid, struct timespec
158                 *remaining_time)
159 {
160         struct itimerspec null_timer, old;
161         
162         // NULL timer -> disarm
163         null_timer = (struct itimerspec) {
164                 .it_value = {0, 0},
165                 .it_interval = 0,
166         };
167         
168         if (timer_settime (timerid, 0, &null_timer, &old))
169                 return EINVAL;
170         
171         if (remaining_time)
172                 *remaining_time = old.it_value; 
173                 
174         return 0;
175 }
176