1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 by the FRESCOR consortium:
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
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org
18 // The 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
23 // All rights reserved.
25 // Redistribution and use in source and binary forms, with or
26 // without modification, are permitted provided that the
27 // following conditions are met:
29 // * Redistributions of source code must retain the above
30 // copyright notice, this list of conditions and the
31 // following disclaimer.
32 // * Redistributions in binary form must reproduce the above
33 // copyright notice, this list of conditions and the
34 // following disclaimer in the documentation and/or other
35 // materials provided with the distribution.
36 // * Neither the name of FRESCOR nor the names of its
37 // contributors may be used to endorse or promote products
38 // derived from this software without specific prior
39 // written permission.
41 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
42 // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
43 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
44 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
46 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
47 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
50 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 // POSSIBILITY OF SUCH DAMAGE.
55 // -----------------------------------------------------------------------
56 //fosa_clocks_and_timers.c
57 //==============================================
58 // ******** ****** ******** **********
59 // **///// /** ** **////// /** /**
60 // ** /** ** /** /** /**
61 // ******* /** ** /********* /**********
62 // **//// /** ** ////////** /**//////**
63 // ** /** ** /** /** /**
64 // ** /** ** ******** /** /**
65 // // /******/ //////// // //
67 // FOSA(Frescor Operating System Adaptation layer)
68 //================================================
71 #include "fosa_ose_implementation_specific.h"
74 //Debug purpose only.......................
78 * @defgroup clocksandtimers Clocks and Timers
81 * This module defines the types and functions to abstract clocks and
82 * timers for the FRSH implementation.
89 /*************************
91 *************************/
96 * Get the time from a clock
98 * This function sets the variable pointed to by current_time to the
99 * current value of the clock specified by clockid, which may be the
100 * FOSA_CLOCK_REALTIME constant or a value obtained with
101 * fosa_get_cputime_clock()
103 * Returns 0 if successful; otherwise it returns an error code:
104 * EINVAL: the value of clockid is invalid
106 * Alternatively, in case of error the implementation is allowed to
107 * notify it to the system console and then terminate the FRSH
108 * implementation and dependant applications
110 int fosa_clock_get_time(fosa_clock_id_t clockid,
111 struct timespec *current_time)
113 if (clockid == FOSA_CLOCK_REALTIME) {
116 ticks = get_systime(µs);
117 //If micros == NULL this are not provided.
118 fosa_ose_systime_to_timespec(ticks, micros, current_time);
119 } else if (fosa_ose_is_process(clockid)) {
120 // Opimization, save the last called pid and a pointer to it's
121 // execution time. Make if statement to check for it.
123 //The clock are an execution time measurement.
124 //extern struct* global_time_ptr;
126 // Find the pid in the global linked list.
128 // Save the executiontime in current_time->tv_nsec and
129 // current_time->tv_sec.
130 // fosa_ose_systime_to_timespec(systick, micro, current_time);
140 * fosa_get_cputime_clock()
142 * Get the identifier of a cpu-time clock
144 * This function stores in the variable pointed to by clockid the
145 * identifier of a cpu-time clock for the thread specified by tid.
147 * Returns 0 if successful; otherwise it returns an error code:
148 * EINVAL: the value of tid is invalid
150 * Alternatively, in case of error the implementation is allowed to
151 * notify it to the system console and then terminate the FRSH
152 * implementation and dependant applications
154 int fosa_thread_get_cputime_clock(frsh_thread_id_t tid, fosa_clock_id_t *clockid)
156 //Might need to change. Possibility to have many clocks? Probably not
157 //One clock but many timers. .........................................
159 if (!fosa_ose_is_process(tid)) return EINVAL;
160 *clockid = (fosa_clock_id_t) tid;
165 /*************************
167 *************************/
170 * fosa_create_timer()
172 * Create a one-shot timer
174 * This function creates a timer based on the clock specified by clock,
175 * and associates to this timer a notification mechanism consisting of
176 * a signal and associated information. Initially, the timer is in the
177 * disarmed state, i.e., not counting time. It can be armed to start
178 * counting time with fosa_timer_arm().
180 * The function stores the identifier of the newly created timer in the
181 * variable pointed to by timerid.
183 * When the timer expires, the signal number specified by signal will be
184 * sent together with the information specified by info, to the thread
185 * that armed the timer (@see fosa_timer_arm()).
187 * In those implementations that do not support queueing a
188 * signal with information to a thread (such as POSIX), the signal may
189 * be sent to any thread that is waiting for this signal via
190 * fosa_signal_wait(). Portability can be ensured by having the receiver
191 * thread be the one who is waiting for the signal.
193 * Returns 0 if successful; otherwise it returns an error code:
194 * EINVAL: the value of clockid or signal is invalid
196 * EAGAIN: the system lacks enough resources to create the timer
198 * Alternatively, in case of error the implementation is allowed to
199 * notify it to the system console and then terminate the FRSH
200 * implementation and dependant applications
202 int fosa_timer_create
203 (fosa_clock_id_t clockid, frsh_signal_t signal,
204 frsh_signal_info_t info, fosa_timer_id_t *timerid)
206 *timerid = (fosa_timer_id_t)malloc(sizeof(fosa_ose_timer_id_t));
207 fosa_timer_id_t timer_id = *timerid; //To clearify only.
209 timer_id->sig_no = signal;
210 timer_id->info = info;
211 timer_id->clock_id = clockid;
212 timer_id->ose_tmoref = -1;
220 * The function deletes the timer specified by timerid, which becomes
221 * unusable. If the timer was armed, it is automatically disarmed before
224 * Returns 0 if successful; otherwise it returns an error code:
225 * EINVAL: the value of timerid is not valid
227 * Alternatively, in case of error the implementation is allowed to
228 * notify it to the system console and then terminate the FRSH
229 * implementation and dependant applications
231 int fosa_timer_delete(fosa_timer_id_t timerid)
233 if (timerid->clock_id == FOSA_CLOCK_REALTIME) {
234 cancel_tmo(&(timerid->ose_tmoref));
236 //Maybe do something with some global data accessible in the
237 //swap-handlers...................................................
239 timerid->ose_tmoref = -1;
241 //Errors taken care of by OSE.
250 * The timer specified by timer is armed and starts counting time.
252 * If abstime is true, the value pointed to by value is the absolute
253 * time at which the timer will expire. If value specifies a time instant
254 * in the past, the timer expires immediately.
256 * If abstime is false, the value pointed to by value is the relative interval
257 * that must elapse for the timer to expire.
259 * In both cases, absolute or relative, the time is measured with the clock
260 * associated with the timer when it was created.
262 * If the timer was already armed, the previous time or interval is discarded
263 * and the timer is rearmed with the new value.
265 * When the timer expires, it is disarmed.
267 * Returns 0 if successful; otherwise it returns an error code:
268 * EINVAL: the value of timerid or value is invalid
270 * Alternatively, in case of error the implementation is allowed to
271 * notify it to the system console and then terminate the FRSH
272 * implementation and dependant applications
275 (fosa_timer_id_t timerid, bool abstime,
276 const struct timespec *value)
278 /*printf("Timerid sig_no %d, info %d, clock_id %d, ose_tmoref %d\n",
279 timerid->sig_no, timerid->info.value, timerid->clock_id,
280 timerid->ose_tmoref);
281 printf("Lika? %d == %d\n",timerid->clock_id, FOSA_CLOCK_REALTIME);*/
283 OSTIME timeout_ms = value->tv_nsec / 1000000 +
284 value->tv_sec * 1000;
286 //if (timerid->ose_tmoref != -1) {
287 // restart_tmo(&(timerid->ose_tmoref), timeout_ms);
290 if (timerid->clock_id == FOSA_CLOCK_REALTIME) {
294 fosa_ose_signal_t *ose_sig;
295 ose_sig = (fosa_ose_signal_t *)
296 alloc(sizeof(fosa_ose_signal_t), timerid->sig_no);
297 ose_sig->info = timerid->info;
298 ose_sig->sig_no = timerid->sig_no;
300 timerid->ose_tmoref = request_tmo_sig(timeout_ms,
301 (union SIGNAL **) &ose_sig);
309 * A absolute timer for a clock that measures the time a
310 * process has used the CPU does not make sense. Therefore
314 //En absolut timer mäter tiden jämfört med systemtiden. Medens
315 //en realtiv gör det i antal sekunder och nanosekunder från
318 //Do something, preapare a signal?................................
326 * Ange i den globala variabel som swap-handlern använder vilken
327 * timeouten är. Då startar swap-handlern att räkna tid? Om abstime är
332 * fosa_timer_disarm()
336 * The timer specified by timer is disarmed, and will not expire unless
337 * it is rearmed. If the timer was already disramed, the function has
340 * Returns 0 if successful; otherwise it returns an error code:
341 * EINVAL: the value of timerid or value is invalid
343 * Alternatively, in case of error the implementation is allowed to
344 * notify it to the system console and then terminate the FRSH
345 * implementation and dependant applications
347 int fosa_timer_disarm(fosa_timer_id_t timerid)
349 if (timerid->clock_id == FOSA_CLOCK_REALTIME) {
351 //restart_tmo(&(timerid->ose_tmoref),time);
352 //Hur kolla om signal är given?...................................
353 cancel_tmo(&(timerid->ose_tmoref));
355 //Maybe do something with some global data accessible in the
356 //swap-handlers...................................................
358 timerid->ose_tmoref = -1;
364 * Ta bort timeouten i den globala variabel som swap-handlern använder.
365 * Då slutar swap-handlern att räkna tid?