From 3d57435de47a2a9e8f12dddc7480c66373fb0460 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Wed, 21 Aug 2002 17:45:10 +0000 Subject: 2002-08-21 Joel Sherrill * src/adjtime.c: New file -- adjtime() support required by the Network Time Protocol (NTP) port to RTEMS. * src/Makefile.am: Modified to reflect above. --- cpukit/posix/ChangeLog | 6 +++++ cpukit/posix/src/Makefile.am | 8 +++--- cpukit/posix/src/adjtime.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 cpukit/posix/src/adjtime.c (limited to 'cpukit/posix') diff --git a/cpukit/posix/ChangeLog b/cpukit/posix/ChangeLog index c14111f776..42d5b8816c 100644 --- a/cpukit/posix/ChangeLog +++ b/cpukit/posix/ChangeLog @@ -1,3 +1,9 @@ +2002-08-21 Joel Sherrill + + * src/adjtime.c: New file -- adjtime() support required by the + Network Time Protocol (NTP) port to RTEMS. + * src/Makefile.am: Modified to reflect above. + 2002-08-09 Joel Sherrill * src/cancelrun.c: Remove check for PTHREAD_CANCELED not being defined diff --git a/cpukit/posix/src/Makefile.am b/cpukit/posix/src/Makefile.am index 1b853e510b..ec08068468 100644 --- a/cpukit/posix/src/Makefile.am +++ b/cpukit/posix/src/Makefile.am @@ -67,10 +67,10 @@ SEMAPHORE_C_FILES = semaphore.c semaphorecreatesupp.c semaphoredeletesupp.c \ semdestroy.c semgetvalue.c seminit.c semopen.c sempost.c semtimedwait.c \ semtrywait.c semunlink.c semwait.c -TIME_C_FILES = time.c posixtimespecsubtract.c posixtimespectointerval.c \ - posixintervaltotimespec.c clockgetcpuclockid.c clockgetenableattr.c \ - clockgetres.c clockgettime.c clocksetenableattr.c clocksettime.c \ - nanosleep.c sleep.c +TIME_C_FILES = adjtime.c time.c posixtimespecsubtract.c \ + posixtimespectointerval.c posixintervaltotimespec.c clockgetcpuclockid.c \ + clockgetenableattr.c clockgetres.c clockgettime.c clocksetenableattr.c \ + clocksettime.c nanosleep.c sleep.c # the timer manager needs to be split further but only after its # dependence on the Classic API Timer Manager is removed. diff --git a/cpukit/posix/src/adjtime.c b/cpukit/posix/src/adjtime.c new file mode 100644 index 0000000000..12edb63e91 --- /dev/null +++ b/cpukit/posix/src/adjtime.c @@ -0,0 +1,60 @@ +/* + * adjustime() function - required by NTP + * + * I am unaware of the history behind the definition of this service + * and don't know if its behavior is covered by any standard. --joel + * + * $Id$ + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +#include +#include +#include + +static long __adjustment = 0; + +int adjtime ( struct timeval *delta, struct timeval *olddelta ) +{ + struct timespec ts; + + if ( olddelta ) { + olddelta->tv_sec = __adjustment / TOD_MICROSECONDS_PER_SECOND; + olddelta->tv_usec = __adjustment / TOD_MICROSECONDS_PER_SECOND; + } + + if ( !delta ) + return -1; + + __adjustment = (delta->tv_sec * TOD_MICROSECONDS_PER_SECOND) + delta->tv_usec; + /* too small to account for */ + if ( __adjustment < _TOD_Microseconds_per_tick ) + return 0; + + clock_gettime( CLOCK_REALTIME, &ts ); + + ts.tv_sec += (__adjustment / TOD_MICROSECONDS_PER_SECOND); + ts.tv_nsec += (__adjustment % TOD_MICROSECONDS_PER_SECOND) * + TOD_NANOSECONDS_PER_MICROSECOND; + + /* if adjustment is too much positive */ + while ( ts.tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) { + ts.tv_nsec -= TOD_NANOSECONDS_PER_SECOND; + ts.tv_sec++; + } + + /* if adjustment is too much negative */ + while ( ts.tv_nsec <= (-1 * TOD_NANOSECONDS_PER_SECOND) ) { + ts.tv_nsec += TOD_NANOSECONDS_PER_SECOND; + ts.tv_sec--; + } + + clock_settime( CLOCK_REALTIME, &ts ); + return 0; +} -- cgit v1.2.3