// 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
// **//// /** ** ////////** /**//////**
// ** /** ** /** /** /**
// ** /** ** ******** /** /**
-// // /******/ //////// // //
+// // /******/ //////// // //
//
// FOSA(Frescor Operating System Adaptation layer)
//================================================
*
* 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:
*
* 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
* 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
/**
* 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
*
* 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
const struct timespec *value)
{
int timer_abstime=0;
- struct itimerspec timer_value;
+ struct itimerspec timer_value;
// set the abstime flag if necessary
if (abstime) {
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
* 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) {
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;
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);