]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_aquosa/fosa_clocks_and_timers.c
Bugfixes and restyling.
[frescor/fosa.git] / src_aquosa / fosa_clocks_and_timers.c
index 9c7e2930cda6e9cf384f21b41baad35610b746da..5adb0d6ba9fd8a29f25ed68bb01507e3f7d3e089 100644 (file)
 // FOSA(Frescor Operating System Adaptation layer)
 //================================================
 
+#include "fosa_time.h"
 #include "fosa_clocks_and_timers.h"
 
+static const struct timespec zero_time={0,0};
+
+
 /*************************
  * Timing: Clocks
  *************************/
 
+/**
+ * fosa_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, &current_time_tspec);
+       *current_time = fosa_timespec_to_abs_time(current_time_tspec);
+
+       return ret;
 }
 
+/**
+ * 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) {
                /* 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);
+       }
 }
 
 /*************************
@@ -123,7 +157,6 @@ int fosa_timer_create(fosa_clock_id_t 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);
@@ -138,9 +171,6 @@ int fosa_timer_create(fosa_clock_id_t clockid,
  * 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,
@@ -149,42 +179,83 @@ int fosa_timer_create(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);
 }
 
- /**
 * 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
 **/
+/**
+ * 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)
 {
        return timer_delete(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.
+ * 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.
  *
- * If abstime is false, the value pointed to by value is the relative interval
- * that must elapse for the timer to expire.
+ * 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.
  *
- * In both cases, absolute or relative, the time is measured with the clock
- * associated with the timer when it was created.
+ * 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)
+{
+       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, 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.
@@ -192,23 +263,21 @@ int fosa_timer_delete(fosa_timer_id_t  timerid)
  * 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 struct timespec *value)
+int fosa_abs_timer_arm(fosa_timer_id_t timerid, const fosa_abs_time_t *value)
 {
        struct itimerspec when;
 
-       /* non-periodic one shot timer configuration */
-       when.it_value = *value;
-       when.it_interval.tv_sec = 0;
-       when.it_interval.tv_nsec = 0;
+       /* non-periodic one shot timer */
+       when.it_value = fosa_abs_time_to_timespec(*value);
+       when.it_interval = zero_time;
 
-       return timer_settime(timerid,
-                            (abstime ? TIMER_ABSTIME : 0),
-                            &when,
-                            NULL);
+       return timer_settime(timerid, TIMER_ABSTIME, &when, NULL);
 }
 
 /**
@@ -226,20 +295,18 @@ int fosa_timer_arm (fosa_timer_id_t timerid,
  *    EINVAL: the value of timerid or value is invalid
  **/
 int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
-                                 struct timespec *remaining_time)
+                                 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)
+               return FOSA_EINVAL;
 
-               *remaining_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;
 }
 
 /**
@@ -261,19 +328,20 @@ int fosa_timer_get_remaining_time(fosa_timer_id_t timerid,
 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 = time.it_value;
-       }
+       if (!remaining_time)
+               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);
 }