]> rtime.felk.cvut.cz Git - frescor/fosa.git/commitdiff
added get_remaining_time
authorsangorrin <sangorrin@35b4ef3e-fd22-0410-ab77-dab3279adceb>
Thu, 7 Jun 2007 10:54:56 +0000 (10:54 +0000)
committersangorrin <sangorrin@35b4ef3e-fd22-0410-ab77-dab3279adceb>
Thu, 7 Jun 2007 10:54:56 +0000 (10:54 +0000)
git-svn-id: http://www.frescor.org/private/svn/frescor/fosa/trunk@440 35b4ef3e-fd22-0410-ab77-dab3279adceb

src_marte/fosa_clocks_and_timers.c
src_marte/test_fosa.c

index 9f13e1df6280365b3d7b4212e8690e0c6a10db8f..39befe1757e63b151592bf3d5bf39c0f5c264219 100644 (file)
@@ -12,7 +12,7 @@
 //    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
@@ -47,7 +47,7 @@
 //  **////  /**    ** ////////** /**//////**
 //  **      /**    **        /** /**     /**
 //  **      /**    **  ********  /**     /**
-//  //       /******/  ////////   //      // 
+//  //       /******/  ////////   //      //
 //
 // FOSA(Frescor Operating System Adaptation layer)
 //================================================
@@ -70,9 +70,9 @@ static const struct timespec zero_time={0,0};
  *
  * 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 
+ * 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:
@@ -93,12 +93,12 @@ int fosa_clock_get_time(fosa_clock_id_t clockid, struct timespec *current_time)
  *
  * Get the identifier of a cpu-time clock
  *
- * This function stores in the variable pointed to by clockid the 
+ * 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
- * 
+ *
  * 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
@@ -125,22 +125,22 @@ int fosa_thread_get_cputime_clock
  * 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 
+ * 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()). 
+ * that armed the timer (@see fosa_timer_arm()).
  *
  * In those implementations that do not support queueing a
  * signal with information to a thread (such as POSIX), the signal may
  * be sent to any thread that is waiting for this signal via
  * fosa_signal_wait(). Portability can be ensured by having the receiver
- * thread be the one who is waiting for the signal. 
- * 
+ * thread be the one who is waiting for the signal.
+ *
  * 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
  *
  * Alternatively, in case of error the implementation is allowed to
@@ -162,11 +162,11 @@ int fosa_thread_get_cputime_clock
 
 /**
  * Delete a timer
- * 
- * The function deletes the timer specified by timerid, which becomes 
+ *
+ * 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
  *
@@ -187,13 +187,13 @@ int fosa_timer_delete(fosa_timer_id_t timerid)
  * 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 
+ * 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. 
+ * that must elapse for the timer to expire.
  *
- * In both cases, absolute or relative, the time is measured with the clock 
+ * In both cases, absolute or relative, 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
@@ -213,7 +213,7 @@ int fosa_timer_arm
        const struct timespec *value)
 {
   int timer_abstime=0;
-  struct itimerspec timer_value;  
+  struct itimerspec timer_value;
 
   // set the abstime flag if necessary
   if (abstime) {
@@ -228,13 +228,47 @@ int fosa_timer_arm
   return timer_settime(timerid,timer_abstime,&timer_value,NULL);
 }
 
+/**
+ * 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
+ *
+ * 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_get_remaining_time
+        (fosa_timer_id_t timerid, struct timespec *remaining_time)
+{
+    struct itimerspec timer_value;
+    int error_code;
+
+    if (remaining_time!=NULL) {
+        error_code=timer_gettime(timerid,&timer_value);
+        if (error_code==-1) return errno;
+        *remaining_time=timer_value.it_value;
+    } else {
+        return EINVAL;
+    }
+    return 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 
+ *
+ * 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
@@ -249,10 +283,10 @@ int fosa_timer_arm
  * implementation and dependant applications
  **/
 int fosa_timer_disarm
-   (fosa_timer_id_t timerid, 
+   (fosa_timer_id_t timerid,
     struct timespec *remaining_time)
 {
-  struct itimerspec timer_value;  
+  struct itimerspec timer_value;
   int error_code;
 
   if (remaining_time!=NULL) {
index 9b9052ecf681f2c1f9950aa13fe569bc9b38e696..c8c06e2106e6056726b9b0548134a616c586c79e 100644 (file)
@@ -128,7 +128,7 @@ int main () {
 
   frsh_signal_info_t timer_info;
   fosa_timer_id_t timerid;
-  struct timespec timerval;
+  struct timespec timerval, remaining_time;
 
   timer_info.sival_int=88;
   timerval.tv_sec=1;
@@ -156,18 +156,29 @@ int main () {
   printf("current time sec=%d nsec=%d\n",
         current_time.tv_sec, current_time.tv_nsec);
 
-  timerval.tv_sec=4;
+  timerval.tv_sec=6;
   timerval.tv_nsec=0;
   err=fosa_timer_arm(timerid,false,&timerval);
-  printf("timer armed for 4 secs, err=%d\n",err);
+  printf("timer armed for 6 secs, err=%d\n",err);
 
-  timerval.tv_sec=2;
-  printf("sleeping 2 seconds\n");
+  timerval.tv_sec=1;
+  printf("sleeping 1 second\n");
   nanosleep(&timerval, NULL);
 
-  err=fosa_timer_disarm(timerid,&timerval);
+  err=fosa_timer_get_remaining_time(timerid, &remaining_time);
+  printf("timer remaining time sec=%d nsec=%d, err=%d\n",
+         remaining_time.tv_sec, remaining_time.tv_nsec, err);
+
+  printf("sleeping 1 second\n");
+  nanosleep(&timerval, NULL);
+
+  err=fosa_timer_disarm(timerid,&remaining_time);
   printf("timer disarmed, remaining time sec=%d nsec=%d, err=%d\n",
-         timerval.tv_sec, timerval.tv_nsec, err);
+         remaining_time.tv_sec, remaining_time.tv_nsec, err);
+
+  err=fosa_timer_get_remaining_time(timerid, &remaining_time);
+  printf("timer remaining time after disarm (0?) sec=%d nsec=%d, err=%d\n",
+         remaining_time.tv_sec, remaining_time.tv_nsec, err);
 
   fosa_timer_delete(timerid);