adjtime.c: Use timestamp math and simplify

This commit is contained in:
Joel Sherrill
2014-07-18 19:46:59 -05:00
parent 8abbbdde38
commit 3b6352d2e6

View File

@@ -1,12 +1,12 @@
/** /**
* @file * @file
* *
* @brief Correct the Time to Synchronize the System Clock * @brief Adjust the Time to Synchronize the System Clock
* @ingroup POSIXAPI * @ingroup POSIXAPI
*/ */
/* /*
* COPYRIGHT (c) 1989-2007. * COPYRIGHT (c) 1989-2014.
* On-Line Applications Research Corporation (OAR). * On-Line Applications Research Corporation (OAR).
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
@@ -18,6 +18,7 @@
#include "config.h" #include "config.h"
#endif #endif
#define _BSD_SOURCE
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <errno.h> #include <errno.h>
@@ -42,8 +43,9 @@ int adjtime(
struct timeval *olddelta struct timeval *olddelta
) )
{ {
struct timespec ts; Timestamp_Control delta_as_timestamp;
long adjustment; Timestamp_Control tod_as_timestamp;
Timestamp_Control *tod_as_timestamp_ptr;
/* /*
* Simple validations * Simple validations
@@ -51,21 +53,22 @@ int adjtime(
if ( !delta ) if ( !delta )
rtems_set_errno_and_return_minus_one( EINVAL ); rtems_set_errno_and_return_minus_one( EINVAL );
if ( delta->tv_usec >= TOD_MICROSECONDS_PER_SECOND ) /*
rtems_set_errno_and_return_minus_one( EINVAL ); * Currently, RTEMS does the adjustment in one movement.
* Given interest, requirements, and sponsorship, a future
* enhancement would be to adjust the time in smaller increments
* at each clock tick. Until then, there is no outstanding
* adjustment.
*/
if ( olddelta ) { if ( olddelta ) {
olddelta->tv_sec = 0; olddelta->tv_sec = 0;
olddelta->tv_usec = 0; olddelta->tv_usec = 0;
} }
/* convert delta to microseconds */ /*
adjustment = (delta->tv_sec * TOD_MICROSECONDS_PER_SECOND); * convert delta timeval to internal timestamp
adjustment += delta->tv_usec; */
_Timestamp_Set( &delta_as_timestamp, delta->tv_sec, delta->tv_usec * 1000 );
/* too small to account for */
if ( adjustment < rtems_configuration_get_microseconds_per_tick() )
return 0;
/* /*
* This prevents context switches while we are adjusting the TOD * This prevents context switches while we are adjusting the TOD
@@ -73,30 +76,15 @@ int adjtime(
_Thread_Disable_dispatch(); _Thread_Disable_dispatch();
_TOD_Get( &ts ); tod_as_timestamp_ptr =
_TOD_Get_with_nanoseconds( &tod_as_timestamp, &_TOD.now );
ts.tv_sec += delta->tv_sec;
ts.tv_nsec += delta->tv_usec * TOD_NANOSECONDS_PER_MICROSECOND;
/* if adjustment is too much positive */ _Timestamp_Add_to( tod_as_timestamp_ptr, &delta_as_timestamp );
while ( ts.tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) {
ts.tv_nsec -= TOD_NANOSECONDS_PER_SECOND;
ts.tv_sec++;
}
/* if adjustment is too much negative */ _TOD_Set_with_timestamp( tod_as_timestamp_ptr );
while ( ts.tv_nsec <= (-1 * TOD_NANOSECONDS_PER_SECOND) ) {
ts.tv_nsec += TOD_NANOSECONDS_PER_SECOND;
ts.tv_sec--;
}
_TOD_Set( &ts );
_Thread_Enable_dispatch(); _Thread_Enable_dispatch();
/* set the user's output */
if ( olddelta )
*olddelta = *delta;
return 0; return 0;
} }