// -----------------------------------------------------------------------
-// Copyright (C) 2006 - 2007 FRESCOR consortium partners:
+// Copyright (C) 2006 - 2009 FRESCOR consortium partners:
//
// Universidad de Cantabria, SPAIN
// University of York, UK
// Scuola Superiore Sant'Anna, ITALY
// Kaiserslautern University, GERMANY
-// Univ. Politecnica Valencia, SPAIN
+// Univ. Politécnica Valencia, SPAIN
// Czech Technical University in Prague, CZECH REPUBLIC
// ENEA SWEDEN
// Thales Communication S.A. FRANCE
// Visual Tools S.A. SPAIN
// Rapita Systems Ltd UK
// Evidence ITALY
-//
+//
// See http://www.frescor.org for a link to partners' websites
//
// FRESCOR project (FP6/2005/IST/5-034026) is funded
// The European Union is not liable of any use that may be
// made of this code.
//
-// This file is part of the FRSH implementation
//
-// FRSH is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2, or (at your option)
-// any later version.
+// based on previous work (FSF) done in the FIRST project
+//
+// Copyright (C) 2005 Mälardalen University, SWEDEN
+// Scuola Superiore S.Anna, ITALY
+// Universidad de Cantabria, SPAIN
+// University of York, UK
+//
+// FSF API web pages: http://marte.unican.es/fsf/docs
+// http://shark.sssup.it/contrib/first/docs/
//
-// FRSH is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// General Public License for more details.
+// This file is part of FOSA (Frsh Operating System Adaption)
//
-// You should have received a copy of the GNU General Public License
-// distributed with FRSH; see file COPYING. If not, write to the
-// Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-// 02111-1307, USA.
+// FOSA is free software; you can redistribute it and/or modify it
+// under terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option) any
+// later version. FOSA is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details. You should have received a
+// copy of the GNU General Public License along with FOSA; see file
+// COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
+// Cambridge, MA 02139, USA.
//
-// As a special exception, if you include this header file into source
-// files to be compiled, this header file does not by itself cause
-// the resulting executable to be covered by the GNU General Public
-// License. This exception does not however invalidate any other
-// reasons why the executable file might be covered by the GNU General
-// Public License.
+// As a special exception, including FOSA header files in a file,
+// instantiating FOSA generics or templates, or linking other files
+// with FOSA objects to produce an executable application, does not
+// by itself cause the resulting executable application to be covered
+// by the GNU General Public License. This exception does not
+// however invalidate any other reasons why the executable file might be
+// covered by the GNU Public License.
// -----------------------------------------------------------------------
//
//==============================================
// FOSA(Frescor Operating System Adaptation layer)
//================================================
-#include <fosa.h>
+#include "fosa_time.h"
+#include "fosa_clocks_and_timers.h"
+
+static const struct timespec zero_time={0,0};
+
/*************************
* Timing: Clocks
*************************/
+/**
+ * fosa_clock_get_time()
+ *
+ * Get the time from a clock
+ *
+ * This function sets the variable pointed to by current_time to the
+ * current value of the clock specified by clockid, which may be the
+ * FOSA_CLOCK_REALTIME constant or a value obtained with
+ * fosa_get_cputime_clock()
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * EINVAL: the value of clockid is invalid
+ **/
int fosa_clock_get_time(fosa_clock_id_t clockid,
- struct timespec *current_time)
+ fosa_abs_time_t *current_time)
{
- return clock_gettime(clockid, current_time);
+ int ret;
+ struct timespec current_time_tspec;
+
+ ret = clock_gettime(clockid, ¤t_time_tspec);
+ if (ret) return errno;
+
+ *current_time = fosa_timespec_to_abs_time(current_time_tspec);
+
+ return 0;
}
-int fosa_thread_get_cputime_clock(frsh_thread_id_t tid,
- fosa_clock_id_t *clockid)
+/**
+ * fosa_get_cputime_clock()
+ *
+ * Get the identifier of a cpu-time clock
+ *
+ * This function stores in the variable pointed to by clockid the
+ * identifier of a cpu-time clock for the thread specified by tid.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * EINVAL: the value of tid is invalid
+ **/
+int fosa_thread_get_cputime_clock(fosa_thread_id_t tid,
+ fosa_clock_id_t *clockid)
{
- if (tid.linux_pid == tid.linux_tid) {
- /* we're in a standard UNIX process */
- *clockid = FOSA_SYSTEM_CLOCK;
-
- return 0;
- } else
- /* we're in a thread */
- return pthread_getcpuclockid(tid.pthread_id, clockid);
+ int ret;
+
+ if (tid.linux_pid == tid.linux_tid) /* standard UNIX process */
+ ret = clock_getcpuclockid(tid.linux_pid, clockid);
+ else /* POSIX thread */
+ ret = pthread_getcpuclockid(tid.pthread_id, clockid);
+
+ return ret ? errno : 0;
}
/*************************
* Timing: Timers
*************************/
+/**
+ * fosa_create_timer()
+ *
+ * Create a one-shot timer
+ *
+ * This function creates a timer based on the clock specified by clock,
+ * and associates to this timer a notification mechanism consisting of
+ * a signal and associated information. Initially, the timer is in the
+ * disarmed state, i.e., not counting time. It can be armed to start
+ * counting time with fosa_timer_arm().
+ *
+ * The function stores the identifier of the newly created timer in the
+ * variable pointed to by timerid.
+ *
+ * When the timer expires, the signal number specified by signal will be
+ * sent together with the information specified by info, to the thread
+ * that armed the timer (@see fosa_timer_arm()).
+ *
+ * Note that, since this is a POSIX implementation, the signal will be sent
+ * to any thread waiting fot it (in a given UNIX process).
+ * No signal-waiting thread or similar strategy is implemented, the specific
+ * thread can be choosed (for example) by means of the signal number.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * EINVAL: the value of clockid or signal is invalid
+ *
+ * EAGAIN: the system lacks enough resources to create the timer
+ **/
int fosa_timer_create(fosa_clock_id_t clockid,
- frsh_signal_t signal,
- frsh_signal_info_t info,
- fosa_timer_id_t *timerid)
+ fosa_signal_t signal,
+ fosa_signal_info_t info,
+ fosa_timer_id_t *timerid)
{
+ int ret;
struct sigevent event;
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_signo = signal;
- event.sigev_value.sival_int = info.value;
+ event.sigev_value = *((union sigval*) &info);
- return timer_create(clockid, &event, timerid);
+ ret = timer_create(clockid, &event, timerid);
+
+ return ret ? errno : 0;
}
-int fosa_timer_delete(fosa_timer_id_t timerid){
- timer_delete(timerid);
- return 0;
+/**
+ * fosa_timer_create_with_receiver()
+ *
+ * Create a one-shot timer with a specific signal receiver thread
+ *
+ * This function creates a timer in the same way as fosa_timer_create,
+ * except that the signal generated when the timer expires is sent to
+ * the thread specified by receiver
+ *
+ * You have to use the "simple" fosa_timer_create.
+ **/
+ int fosa_timer_create_with_receiver(fosa_clock_id_t clockid,
+ fosa_signal_t signal,
+ fosa_signal_info_t info,
+ fosa_timer_id_t *timerid,
+ fosa_thread_id_t receiver)
+{
+ int ret;
+ struct sigevent event;
+
+ event.sigev_notify = SIGEV_SIGNAL;
+ event.sigev_signo = signal;
+ event.sigev_value = *((union sigval*) &info);
+ event.sigev_notify = SIGEV_THREAD_ID | SIGEV_SIGNAL;
+ event._sigev_un._tid = receiver.linux_tid;
+
+ ret = timer_create(clockid, &event, timerid);
+
+ return ret ? errno : 0;
}
-int fosa_timer_arm (fosa_timer_id_t timerid,
- bool abstime,
- const struct timespec *value)
+/**
+ * Delete a timer
+ *
+ * The function deletes the timer specified by timerid, which becomes
+ * unusable. If the timer was armed, it is automatically disarmed before
+ * deletion.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * EINVAL: the value of timerid is not valid
+ **/
+int fosa_timer_delete(fosa_timer_id_t timerid)
{
- int flags;
- struct itimerspec when;
+ int ret;
- when.it_value = *value; /* one shot timer */
- when.it_interval.tv_sec = 0;
- when.it_interval.tv_nsec = 0; /* no periodic behaviour */
- if (abstime)
- flags = TIMER_ABSTIME;
- else
- flags = 0;
+ ret = timer_delete(timerid);
- return timer_settime(timerid, flags, &when, NULL);
+ return ret ? errno : 0;
}
-int fosa_timer_get_remaining_time(fosa_timer_id_t timerid, struct timespec *remaining_time)
+/**
+ * fosa_rel_timer_arm()
+ *
+ * Arm a timer with a relative time interval
+ *
+ * The timer specified by timer is armed and starts counting time.
+ *
+ * The value pointed to by value is the relative interval that must
+ * elapse for the timer to expire. Negative values cause the timer to
+ * expire immediately.
+ *
+ * The time is measured with the clock associated with the timer when
+ * it was created.
+ *
+ * If the timer was already armed, the previous time or interval is discarded
+ * and the timer is rearmed with the new value.
+ *
+ * When the timer expires, it is disarmed.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * FOSA_EINVAL: the value of timerid or value is invalid
+ *
+ * Alternatively, in case of error the implementation is allowed to
+ * notify it to the system console and then terminate the FRSH
+ * implementation and dependant applications
+ **/
+int fosa_rel_timer_arm(fosa_timer_id_t timerid, const fosa_rel_time_t *value)
{
- errno = EINVAL;
- return -1;
+ int ret;
+ struct itimerspec when;
+
+ /* non-periodic one shot timer */
+ when.it_value = fosa_abs_time_to_timespec(*value);
+ when.it_interval = zero_time;
+
+ ret = timer_settime(timerid, 0, &when, NULL);
+
+ return ret ? errno : 0;
}
-int fosa_timer_disarm(fosa_timer_id_t timerid, struct timespec *remaining_time)
+
+/**
+ * fosa_abs_timer_arm()
+ *
+ * Arm a timer that will expire in an absolute time instant.
+ *
+ * The timer specified by timer is armed and starts counting time.
+ *
+ * The value pointed to by value is the absolute time at which the
+ * timer will expire. If value specifies a time instant in the past,
+ * the timer expires immediately.
+ *
+ * The time is measured with the clock associated with the timer when
+ * it was created.
+ *
+ * If the timer was already armed, the previous time or interval is discarded
+ * and the timer is rearmed with the new value.
+ *
+ * When the timer expires, it is disarmed.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * FOSA_EINVAL: the value of timerid or value is invalid
+ *
+ * Alternatively, in case of error the implementation is allowed to
+ * notify it to the system console and then terminate the FRSH
+ * implementation and dependant applications
+ **/
+int fosa_abs_timer_arm(fosa_timer_id_t timerid, const fosa_abs_time_t *value)
{
+ int ret;
struct itimerspec when;
- /* maybe not needed but safer */
- when.it_value.tv_sec = 0;
- when.it_value.tv_nsec = 0;
- when.it_interval = when.it_value;
+ /* non-periodic one shot timer */
+ when.it_value = fosa_abs_time_to_timespec(*value);
+ when.it_interval = zero_time;
+
+ ret = timer_settime(timerid, TIMER_ABSTIME, &when, NULL);
+
+ return ret ? errno : 0;
+}
+
+/**
+ * fosa_timer_get_remaining_time()
+ *
+ * Get the remaining time for timer expiration
+ *
+ * Returns the relative remaining time for timer expiration. If the
+ * clock is a CPU clock it returns the time as if the thread was
+ * executing constantly.
+ *
+ * If the timer is disarmed it returns 0.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * EINVAL: the value of timerid or value is invalid
+ **/
+int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
+ fosa_rel_time_t *remaining_time)
+{
+ int ret;
+ struct itimerspec time;
+
+ if (!remaining_time)
+ return FOSA_EINVAL;
+
+ ret = timer_gettime(timerid, &time);
+ *remaining_time = fosa_timespec_to_rel_time(time.it_value);
+
+ return ret ? errno : 0;
+}
+
+/**
+ * fosa_timer_disarm()
+ *
+ * Disarm a timer
+ *
+ * The timer specified by timer is disarmed, and will not expire unless
+ * it is rearmed. If the timer was already disramed, the function has
+ * no effect.
+ *
+ * If the pointer remaining_time is != NULL, the remaining time before
+ * expiration will be returned in that pointer. If the timer was
+ * disarmed a 0 value will be set.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ * EINVAL: the value of timerid or value is invalid
+ **/
+int fosa_timer_disarm(fosa_timer_id_t timerid,
+ struct timespec *remaining_time)
+{
+ int ret;
+ struct itimerspec time;
+
+ if (!remaining_time)
+ return FOSA_EINVAL;
+
+ ret = timer_gettime(timerid, &time);
+ if (ret) return errno;
+
+ *remaining_time = fosa_timespec_to_rel_time(time.it_value);
+
+ time.it_value = zero_time;
+ time.it_interval = zero_time;
+
+ ret = timer_settime(timerid, 0, &time, NULL);
- return timer_settime(timerid, TIMER_ABSTIME , &when, NULL);
+ return ret ? errno : 0;
}