#include "fosa_time.h"
#include "fosa_clocks_and_timers.h"
+static const struct timespec zero_time={0,0};
+
+
/*************************
* Timing: Clocks
*************************/
int fosa_clock_get_time(fosa_clock_id_t clockid,
fosa_abs_time_t *current_time)
{
- int error;
+ int ret;
struct timespec current_time_tspec;
- if ((error = clock_gettime(clockid, ¤t_time_tspec)) != 0)
- return error;
-
+ ret = clock_gettime(clockid, ¤t_time_tspec);
*current_time = fosa_timespec_to_abs_time(current_time_tspec);
- return 0;
+ return ret;
}
/**
{
if (tid.linux_pid == tid.linux_tid) {
/* standard UNIX process */
- *clockid = FOSA_CLOCK_REALTIME;
- return 0;
- } else
+ return clock_getcpuclockid(tid.linux_pid, clockid);
+ } else {
/* POSIX thread */
return pthread_getcpuclockid(tid.pthread_id, clockid);
+ }
}
/*************************
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_signo = signal;
- //event.sigev_value.sival_int = info.sival_int;
event.sigev_value = *((union sigval*) &info);
return timer_create(clockid, &event, timerid);
* except that the signal generated when the timer expires is sent to
* the thread specified by receiver
*
- * In this implementation, since in POSIX we can not specify a receiver,
- * always returns ENOSYS.
- *
* You have to use the "simple" fosa_timer_create.
**/
int fosa_timer_create_with_receiver(fosa_clock_id_t clockid,
fosa_timer_id_t *timerid,
fosa_thread_id_t receiver)
{
- /* in POSIX the receiver thread cannot be specified!! */
- //return fosa_timer_create(clockid, signal, info, timerid);
- return ENOSYS;
+ 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;
+
+ return timer_create(clockid, &event, timerid);
}
/**
}
/**
- * fosa_timer_arm()
+ * fosa_rel_timer_arm()
*
- * Arm a timer
+ * Arm a timer with a relative time interval
*
* The timer specified by timer is armed and starts counting time.
*
- * If abstime is true, 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.
- *
- * If abstime is false, the value pointed to by value is the relative interval
- * that must elapse for the timer to expire.
+ * 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.
*
- * In both cases, absolute or relative, the time is measured with the clock
- * associated with the timer when it was created.
+ * 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:
- * EINVAL: the value of timerid or value is invalid
+ * 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_timer_arm (fosa_timer_id_t timerid,
- bool abstime,
- const fosa_abs_time_t *value)
+int fosa_rel_timer_arm (fosa_timer_id_t timerid, const fosa_rel_time_t *value)
{
struct itimerspec when;
- /* non-periodic one shot timer configuration */
+ /* non-periodic one shot timer */
when.it_value = fosa_abs_time_to_timespec(*value);
- when.it_interval.tv_sec = 0;
- when.it_interval.tv_nsec = 0;
+ when.it_interval = zero_time;
- return timer_settime(timerid,
- (abstime ? TIMER_ABSTIME : 0),
- &when,
- NULL);
+ return timer_settime(timerid, 0, &when, NULL);
+}
+
+
+/**
+ * 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)
+{
+ struct itimerspec when;
+
+ /* non-periodic one shot timer */
+ when.it_value= fosa_abs_time_to_timespec(*value);
+ when.it_interval=zero_time;
+
+ return timer_settime(timerid, TIMER_ABSTIME, &when, NULL);
}
/**
int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
fosa_rel_time_t *remaining_time)
{
- int error;
+ int ret;
struct itimerspec time;
- if (remaining_time != NULL) {
- if ((error = timer_gettime(timerid, &time)) == -1)
- return error;
+ if (remaining_time == NULL)
+ return FOSA_EINVAL;
- *remaining_time = fosa_timespec_to_rel_time(time.it_value);
- } else
- return EINVAL;
+ ret = timer_gettime(timerid, &time);
+ *remaining_time = fosa_timespec_to_rel_time(time.it_value);
- return 0;
+ return ret;
}
/**
int fosa_timer_disarm(fosa_timer_id_t timerid,
struct timespec *remaining_time)
{
- int error;
+ int ret;
struct itimerspec time;
- if (remaining_time != NULL) {
- if ((error = timer_gettime(timerid, &time)) == -1)
- return error;
-
- *remaining_time = fosa_timespec_to_rel_time(time.it_value);
- }
+ if (remaining_time == NULL)
+ return FOSA_EINVAL;
- time.it_value.tv_sec = 0;
- time.it_value.tv_nsec = 0;
+ ret = timer_gettime(timerid, &time);
+ *remaining_time = fosa_timespec_to_rel_time(time.it_value);
+ if (ret < 0)
+ return ret;
+ time.it_value = zero_time;
+ time.it_interval = zero_time;
+
return timer_settime(timerid, 0, &time, NULL);
}