[complement-svn] SF.net SVN: complement: [1346] trunk/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2006-10-24 06:41:46
|
Revision: 1346 http://svn.sourceforge.net/complement/?rev=1346&view=rev Author: complement Date: 2006-10-23 23:41:35 -0700 (Mon, 23 Oct 2006) Log Message: ----------- move timespec functions to time.cc; move sleep-like functions from Thread to xmt namespace in time.cc Modified Paths: -------------- trunk/explore/include/mt/time.h trunk/explore/include/mt/xmt.h trunk/explore/lib/mt/ChangeLog trunk/explore/lib/mt/time.cc trunk/explore/lib/mt/xmt.cc Modified: trunk/explore/include/mt/time.h =================================================================== --- trunk/explore/include/mt/time.h 2006-10-24 06:40:06 UTC (rev 1345) +++ trunk/explore/include/mt/time.h 2006-10-24 06:41:35 UTC (rev 1346) @@ -1,40 +1,83 @@ -// -*- C++ -*- Time-stamp: <02/09/25 11:37:39 ptr> +// -*- C++ -*- Time-stamp: <06/10/24 09:28:28 ptr> /* - * - * Copyright (c) 2002 + * Copyright (c) 2002, 2006 * Petr Ovtchenkov * - * Licensed under the Academic Free License Version 1.0 + * Licensed under the Academic Free License version 3.0 * - * This material is provided "as is", with absolutely no warranty expressed - * or implied. Any use is at your own risk. - * - * Permission to use, copy, modify, distribute and sell this software - * and its documentation for any purpose is hereby granted without fee, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. */ -#ifndef __time_h -#define __time_h +#ifndef __mt_time_h +#define __mt_time_h -#ifdef __unix -# ifdef __HP_aCC -#pragma VERSIONID "@(#)$Id$" -# else -#ident "@(#)$Id$" -# endif -#endif - #ifndef __config_feature_h #include <config/feature.h> #endif +#ifdef WIN32 +# include <windows.h> +# include <memory> +# include <ctime> +# include <limits> +# pragma warning( disable : 4290) +#endif // WIN32 + #include <string> -#include <time.h> +#include <ctime> +#ifdef N_PLAT_NLM +# include <sys/time.h> // timespec, timespec_t +#endif + +#ifdef _WIN32 +extern "C" { + +typedef struct timespec { /* definition per POSIX.4 */ + time_t tv_sec; /* seconds */ + long tv_nsec; /* and nanoseconds */ +} timespec_t; + +} // extern "C" +#endif // _WIN32 + +#if defined(_WIN32) || defined(N_PLAT_NLM) +extern "C" { + +typedef struct timespec timestruc_t; /* definition per SVr4 */ + +} // extern "C" +#endif + std::string calendar_time( time_t t ); -#endif // __time_h +timespec operator +( const timespec& a, const timespec& b ); +timespec operator -( const timespec& a, const timespec& b ); +timespec operator /( const timespec& a, unsigned b ); +timespec operator /( const timespec& a, unsigned long b ); + +// timespec& operator =( timespec& a, const timespec& b ); +timespec& operator +=( timespec& a, const timespec& b ); +timespec& operator -=( timespec& a, const timespec& b ); +timespec& operator /=( timespec& a, unsigned b ); +timespec& operator /=( timespec& a, unsigned long b ); + +bool operator >( const timespec& a, const timespec& b ); +bool operator >=( const timespec& a, const timespec& b ); +bool operator <( const timespec& a, const timespec& b ); +bool operator <=( const timespec& a, const timespec& b ); +bool operator ==( const timespec& a, const timespec& b ); +bool operator !=( const timespec& a, const timespec& b ); + +namespace xmt { + +// delay execution at least on time interval t +__FIT_DECLSPEC void delay( timespec *interval, timespec *remain ); +// sleep at least up to time t +__FIT_DECLSPEC void sleep( timespec *abstime, timespec *real_time ); +// get precise time +__FIT_DECLSPEC void gettime( timespec *t ); + +} // namespace xmt + +#endif // __mt_time_h Modified: trunk/explore/include/mt/xmt.h =================================================================== --- trunk/explore/include/mt/xmt.h 2006-10-24 06:40:06 UTC (rev 1345) +++ trunk/explore/include/mt/xmt.h 2006-10-24 06:41:35 UTC (rev 1346) @@ -1,23 +1,14 @@ -// -*- C++ -*- Time-stamp: <06/09/18 13:32:44 ptr> +// -*- C++ -*- Time-stamp: <06/10/24 09:24:01 ptr> /* - * - * Copyright (c) 1997-1999, 2002-2005 + * Copyright (c) 1997-1999, 2002-2006 * Petr Ovtchenkov * * Portion Copyright (c) 1999-2001 * Parallel Graphics Ltd. * - * Licensed under the Academic Free License Version 2.1 + * Licensed under the Academic Free License version 3.0 * - * This material is provided "as is", with absolutely no warranty expressed - * or implied. Any use is at your own risk. - * - * Permission to use, copy, modify, distribute and sell this software - * and its documentation for any purpose is hereby granted without fee, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. */ #ifndef __XMT_H @@ -38,7 +29,6 @@ #ifdef WIN32 # include <windows.h> # include <memory> -# include <ctime> # include <limits> # define ETIME 62 /* timer expired */ # pragma warning( disable : 4290) @@ -46,9 +36,6 @@ #ifdef __unix # if defined( _REENTRANT ) && !defined(_NOTHREADS) -# if defined( __STL_USE_NEW_STYLE_HEADERS ) && defined( __SUNPRO_CC ) -# include <ctime> -# endif # ifdef _PTHREADS # include <pthread.h> # include <semaphore.h> @@ -67,37 +54,15 @@ # include <nwthread.h> # include <nwsemaph.h> # include <nwproc.h> -# include <ctime> # elif !defined(_NOTHREADS) // !_REENTRANT # define _NOTHREADS # endif #endif -#ifdef N_PLAT_NLM -# include <sys/time.h> // timespec, timespec_t -#endif - #include <cerrno> -#ifdef _WIN32 -extern "C" { +#include <mt/time.h> -typedef struct timespec { /* definition per POSIX.4 */ - time_t tv_sec; /* seconds */ - long tv_nsec; /* and nanoseconds */ -} timespec_t; - -} // extern "C" -#endif // _WIN32 - -#if defined(_WIN32) || defined(N_PLAT_NLM) -extern "C" { - -typedef struct timespec timestruc_t; /* definition per SVr4 */ - -} // extern "C" -#endif - #ifdef _REENTRANT # define MT_REENTRANT(point,nm) xmt::Locker nm(point) @@ -1512,11 +1477,15 @@ static __FIT_DECLSPEC void signal_exit( int sig ); // signal handler // sleep at least up to time t - static __FIT_DECLSPEC void sleep( timespec *t, timespec *e = 0 ); + static void sleep( timespec *t, timespec *e = 0 ) + { xmt::sleep( t, e ); } // delay execution at least on time interval t - static __FIT_DECLSPEC void delay( timespec *t, timespec *e = 0 ); + static void delay( timespec *t, timespec *e = 0 ) + { xmt::delay( t, e ); } // get precise time - static __FIT_DECLSPEC void gettime( timespec *t ); + static void gettime( timespec *t ) + { xmt::gettime( t ); } + #ifndef _WIN32 static __FIT_DECLSPEC void fork() throw( fork_in_parent, std::runtime_error ); static __FIT_DECLSPEC void become_daemon() throw( fork_in_parent, std::runtime_error ); @@ -1703,7 +1672,7 @@ #endif #if defined(__FIT_UITHREADS) || defined(_PTHREADS) timespec ct; - Thread::gettime( &ct ); + xmt::gettime( &ct ); ct += *interval; int ret = 0; @@ -1824,7 +1793,7 @@ #endif #if defined(__FIT_UITHREADS) || defined(_PTHREADS) timespec ct; - Thread::gettime( &ct ); + xmt::gettime( &ct ); ct += *interval; return this->wait_time( &ct ); @@ -1845,21 +1814,4 @@ namespace __impl = xmt; // compatibility -timespec operator +( const timespec& a, const timespec& b ); -timespec operator -( const timespec& a, const timespec& b ); -timespec operator /( const timespec& a, unsigned b ); -timespec operator /( const timespec& a, unsigned long b ); - -// timespec& operator =( timespec& a, const timespec& b ); -timespec& operator +=( timespec& a, const timespec& b ); -timespec& operator -=( timespec& a, const timespec& b ); -timespec& operator /=( timespec& a, unsigned b ); -timespec& operator /=( timespec& a, unsigned long b ); - -bool operator >( const timespec& a, const timespec& b ); -bool operator >=( const timespec& a, const timespec& b ); -bool operator <( const timespec& a, const timespec& b ); -bool operator <=( const timespec& a, const timespec& b ); -bool operator ==( const timespec& a, const timespec& b ); - #endif // __XMT_H Modified: trunk/explore/lib/mt/ChangeLog =================================================================== --- trunk/explore/lib/mt/ChangeLog 2006-10-24 06:40:06 UTC (rev 1345) +++ trunk/explore/lib/mt/ChangeLog 2006-10-24 06:41:35 UTC (rev 1346) @@ -1,3 +1,9 @@ +2006-10-24 Petr Ovtchenkov <pt...@is...> + + * xmt.h, xmt.cc, time.h, time.cc: move timespec functions to + time.cc; move sleep-like functions from Thread to xmt namespace + in time.cc. + 2006-10-10 Petr Ovtchenkov <pt...@is...> * thr_mgr.cc: don't kill not finished threads in ThreadMgr's Modified: trunk/explore/lib/mt/time.cc =================================================================== --- trunk/explore/lib/mt/time.cc 2006-10-24 06:40:06 UTC (rev 1345) +++ trunk/explore/lib/mt/time.cc 2006-10-24 06:41:35 UTC (rev 1346) @@ -1,36 +1,18 @@ -// -*- C++ -*- Time-stamp: <03/09/24 21:04:53 ptr> +// -*- C++ -*- Time-stamp: <06/10/24 09:26:53 ptr> /* - * - * Copyright (c) 2002, 2003 + * Copyright (c) 2002, 2003, 2006 * Petr Ovtchenkov * - * Licensed under the Academic Free License Version 1.2 + * Licensed under the Academic Free License version 3.0 * - * This material is provided "as is", with absolutely no warranty expressed - * or implied. Any use is at your own risk. - * - * Permission to use, copy, modify, distribute and sell this software - * and its documentation for any purpose is hereby granted without fee, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. */ -#ifdef __unix -# ifdef __HP_aCC -#pragma VERSIONID "@(#)$Id$" -# else -#ident "@(#)$Id$" -# endif -#endif - #include <mt/time.h> +#include <mt/xmt.h> #ifdef _WIN32 -#include <mt/xmt.h> - -__impl::Mutex _l; +xmt::Mutex _l; #endif std::string calendar_time( time_t t ) @@ -63,3 +45,260 @@ return std::string( ctime( &t ) ); #endif } + +timespec operator +( const timespec& a, const timespec& b ) +{ + timespec c; + + c.tv_sec = a.tv_sec; + c.tv_nsec = a.tv_nsec; + c.tv_sec += b.tv_sec; + c.tv_nsec += b.tv_nsec; + c.tv_sec += c.tv_nsec / 1000000000; + c.tv_nsec %= 1000000000; + + return c; +} + +timespec operator -( const timespec& a, const timespec& b ) +{ + timespec c; + + c.tv_sec = a.tv_sec > b.tv_sec ? a.tv_sec - b.tv_sec : 0; // out_of_range? + if ( a.tv_nsec >= b.tv_nsec ) { + c.tv_nsec = a.tv_nsec - b.tv_nsec; + } else if ( c.tv_sec > 0 ) { + --c.tv_sec; + c.tv_nsec = 1000000000 - b.tv_nsec + a.tv_nsec; + } else { + c.tv_nsec = 0; // out_of_range? + } + + return c; +} + +timespec operator /( const timespec& a, unsigned b ) +{ + timespec c; + double d = a.tv_sec + 1.0e-9 * a.tv_nsec; + d /= b; + + c.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); + c.tv_sec = int(d); + + return c; +} + +timespec operator /( const timespec& a, unsigned long b ) +{ + timespec c; + double d = a.tv_sec + 1.0e-9 * a.tv_nsec; + d /= b; + + c.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); + c.tv_sec = int(d); + + return c; +} + +timespec& operator +=( timespec& a, const timespec& b ) +{ + a.tv_sec += b.tv_sec; + a.tv_nsec += b.tv_nsec; + a.tv_sec += a.tv_nsec / 1000000000; + a.tv_nsec %= 1000000000; + + return a; +} + +timespec& operator -=( timespec& a, const timespec& b ) +{ + a.tv_sec = a.tv_sec > b.tv_sec ? a.tv_sec - b.tv_sec : 0; // out_of_range? + if ( a.tv_nsec >= b.tv_nsec ) { + a.tv_nsec -= b.tv_nsec; + } else if ( a.tv_sec > 0 ) { + --a.tv_sec; + a.tv_nsec += 1000000000 - b.tv_nsec; + } else { + a.tv_nsec = 0; // out_of_range? + } + + return a; +} + +timespec& operator /=( timespec& a, unsigned b ) +{ + double d = a.tv_sec + 1.0e-9 * a.tv_nsec; + d /= b; + + a.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); + a.tv_sec = int(d); + + return a; +} + +timespec& operator /=( timespec& a, unsigned long b ) +{ + double d = a.tv_sec + 1.0e-9 * a.tv_nsec; + d /= b; + + a.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); + a.tv_sec = int(d); + + return a; +} + +bool operator ==( const timespec& a, const timespec& b ) +{ + return (a.tv_sec == b.tv_sec) && (a.tv_nsec == b.tv_nsec); +} + +bool operator !=( const timespec& a, const timespec& b ) +{ + return (a.tv_sec != b.tv_sec) || (a.tv_nsec != b.tv_nsec); +} + +bool operator >( const timespec& a, const timespec& b ) +{ + if ( a.tv_sec > b.tv_sec ) { + return true; + } else if ( b.tv_sec > a.tv_sec ) { + return false; + } + + return a.tv_nsec > b.tv_nsec; +} + +bool operator >=( const timespec& a, const timespec& b ) +{ + if ( a.tv_sec > b.tv_sec ) { + return true; + } else if ( b.tv_sec > a.tv_sec ) { + return false; + } + + return a.tv_nsec >= b.tv_nsec; +} + +bool operator <( const timespec& a, const timespec& b ) +{ + if ( a.tv_sec < b.tv_sec ) { + return true; + } else if ( b.tv_sec < a.tv_sec ) { + return false; + } + + return a.tv_nsec < b.tv_nsec; +} + +bool operator <=( const timespec& a, const timespec& b ) +{ + if ( a.tv_sec < b.tv_sec ) { + return true; + } else if ( b.tv_sec < a.tv_sec ) { + return false; + } + + return a.tv_nsec <= b.tv_nsec; +} + +namespace xmt { + +__FIT_DECLSPEC +void delay( timespec *interval, timespec *remain ) +{ +#ifdef __unix + nanosleep( interval, remain ); +#endif +#ifdef WIN32 + unsigned ms = interval->tv_sec * 1000 + interval->tv_nsec / 1000000; + Sleep( ms ); + if ( remain != 0 ) { // M$ not return remain time interval + remain->tv_sec = 0; + remain->tv_nsec = 0; + } +#endif +#ifdef __FIT_NETWARE + unsigned ms = interval->tv_sec * 1000 + interval->tv_nsec / 1000000; + ::delay( ms ); + if ( remain != 0 ) { // Novell not return remain time interval + remain->tv_sec = 0; + remain->tv_nsec = 0; + } +#endif +} + +__FIT_DECLSPEC +void sleep( timespec *abstime, timespec *real_time ) +{ +#ifdef __unix + timespec ct; + gettime( &ct ); + timespec st = *abstime; + + if ( st > ct ) { + st -= ct; + nanosleep( &st, real_time ); + if ( real_time != 0 ) { + *real_time += ct; + } + } else if ( real_time != 0 ) { + *real_time = ct; + } +#endif +#ifdef WIN32 + time_t ct = time( 0 ); + time_t _conv = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000; + + unsigned ms = _conv >= ct ? _conv - ct : 1; + Sleep( ms ); + if ( real_time != 0 ) { // M$ not return elapsed time interval + real_time->tv_sec = abstime->tv_sec; + real_time->tv_nsec = abstime->tv_nsec; + } +#endif +#ifdef __FIT_NETWARE + time_t ct = time( 0 ); + time_t _conv = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000; + + unsigned ms = _conv >= ct ? _conv - ct : 1; + ::delay( ms ); + if ( real_time != 0 ) { // Novell not return elapsed time interval + real_time->tv_sec = abstime->tv_sec; + real_time->tv_nsec = abstime->tv_nsec; + } +#endif +} + +__FIT_DECLSPEC +void gettime( timespec *t ) +{ +#if defined(__linux) || defined(__FreeBSD__) || defined(__OpenBSD__) + timeval tv; + gettimeofday( &tv, 0 ); + TIMEVAL_TO_TIMESPEC( &tv, t ); +#elif defined( WIN32 ) + union { + FILETIME ft; // 100 ns intervals since Jan 1 1601 (UTC) + __int64 t; + } ft; + GetSystemTimeAsFileTime( &ft.ft ); + t->tv_sec = int(ft.t / (__int64)10000000 - (__int64)(11644473600)); // 60 * 60 * 24 * 134774, 1970 - 1601 + t->tv_nsec = int(ft.t % (__int64)(10000000)) * 100; + + //time_t ct = time( 0 ); + //t->tv_sec = ct; // ct / 1000; + //t->tv_nsec = 0; // (ct % 1000) * 1000000; +#elif defined(__sun) || defined(__hpux) + clock_gettime( CLOCK_REALTIME, t ); +#elif defined(__FIT_NETWARE) + time_t ct = time(0); // GetHighResolutionTimer (ret current time in 100 microsec increments) + // GetSuperHighResolutionTimer() (ret current time in 838 nanosec increments) + t->tv_sec = ct; + t->tv_nsec = 0; +#else +#error "You should implement OS-dependent precise clock" +#endif +} + +} // namespace xmt Modified: trunk/explore/lib/mt/xmt.cc =================================================================== --- trunk/explore/lib/mt/xmt.cc 2006-10-24 06:40:06 UTC (rev 1345) +++ trunk/explore/lib/mt/xmt.cc 2006-10-24 06:41:35 UTC (rev 1346) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <06/09/22 22:19:40 ptr> +// -*- C++ -*- Time-stamp: <06/10/24 09:32:17 ptr> /* * Copyright (c) 1997-1999, 2002-2006 @@ -711,103 +711,6 @@ Thread::_exit( 0 ); } -__FIT_DECLSPEC -void Thread::delay( timespec *interval, timespec *remain ) -{ -#ifdef __unix - nanosleep( interval, remain ); -#endif -#ifdef WIN32 - unsigned ms = interval->tv_sec * 1000 + interval->tv_nsec / 1000000; - Sleep( ms ); - if ( remain != 0 ) { // M$ not return remain time interval - remain->tv_sec = 0; - remain->tv_nsec = 0; - } -#endif -#ifdef __FIT_NETWARE - unsigned ms = interval->tv_sec * 1000 + interval->tv_nsec / 1000000; - ::delay( ms ); - if ( remain != 0 ) { // Novell not return remain time interval - remain->tv_sec = 0; - remain->tv_nsec = 0; - } -#endif -} - -__FIT_DECLSPEC -void Thread::sleep( timespec *abstime, timespec *real_time ) -{ -#ifdef __unix - timespec ct; - gettime( &ct ); - timespec st = *abstime; - - if ( st > ct ) { - st -= ct; - nanosleep( &st, real_time ); - if ( real_time != 0 ) { - *real_time += ct; - } - } else if ( real_time != 0 ) { - *real_time = ct; - } -#endif -#ifdef WIN32 - time_t ct = time( 0 ); - time_t _conv = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000; - - unsigned ms = _conv >= ct ? _conv - ct : 1; - Sleep( ms ); - if ( real_time != 0 ) { // M$ not return elapsed time interval - real_time->tv_sec = abstime->tv_sec; - real_time->tv_nsec = abstime->tv_nsec; - } -#endif -#ifdef __FIT_NETWARE - time_t ct = time( 0 ); - time_t _conv = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000; - - unsigned ms = _conv >= ct ? _conv - ct : 1; - ::delay( ms ); - if ( real_time != 0 ) { // Novell not return elapsed time interval - real_time->tv_sec = abstime->tv_sec; - real_time->tv_nsec = abstime->tv_nsec; - } -#endif -} - -__FIT_DECLSPEC -void Thread::gettime( timespec *t ) -{ -#if defined(__linux) || defined(__FreeBSD__) || defined(__OpenBSD__) - timeval tv; - gettimeofday( &tv, 0 ); - TIMEVAL_TO_TIMESPEC( &tv, t ); -#elif defined( WIN32 ) - union { - FILETIME ft; // 100 ns intervals since Jan 1 1601 (UTC) - __int64 t; - } ft; - GetSystemTimeAsFileTime( &ft.ft ); - t->tv_sec = int(ft.t / (__int64)10000000 - (__int64)(11644473600)); // 60 * 60 * 24 * 134774, 1970 - 1601 - t->tv_nsec = int(ft.t % (__int64)(10000000)) * 100; - - //time_t ct = time( 0 ); - //t->tv_sec = ct; // ct / 1000; - //t->tv_nsec = 0; // (ct % 1000) * 1000000; -#elif defined(__sun) || defined(__hpux) - clock_gettime( CLOCK_REALTIME, t ); -#elif defined(__FIT_NETWARE) - time_t ct = time(0); // GetHighResolutionTimer (ret current time in 100 microsec increments) - // GetSuperHighResolutionTimer() (ret current time in 838 nanosec increments) - t->tv_sec = ct; - t->tv_nsec = 0; -#else -#error "You should implement OS-dependent precise clock" -#endif -} - #ifndef _WIN32 __FIT_DECLSPEC void Thread::fork() throw( fork_in_parent, std::runtime_error ) @@ -1102,155 +1005,3 @@ } } // namespace xmt - - -timespec operator +( const timespec& a, const timespec& b ) -{ - timespec c; - - c.tv_sec = a.tv_sec; - c.tv_nsec = a.tv_nsec; - c.tv_sec += b.tv_sec; - c.tv_nsec += b.tv_nsec; - c.tv_sec += c.tv_nsec / 1000000000; - c.tv_nsec %= 1000000000; - - return c; -} - -timespec operator -( const timespec& a, const timespec& b ) -{ - timespec c; - - c.tv_sec = a.tv_sec > b.tv_sec ? a.tv_sec - b.tv_sec : 0; // out_of_range? - if ( a.tv_nsec >= b.tv_nsec ) { - c.tv_nsec = a.tv_nsec - b.tv_nsec; - } else if ( c.tv_sec > 0 ) { - --c.tv_sec; - c.tv_nsec = 1000000000 - b.tv_nsec + a.tv_nsec; - } else { - c.tv_nsec = 0; // out_of_range? - } - - return c; -} - -timespec operator /( const timespec& a, unsigned b ) -{ - timespec c; - double d = a.tv_sec + 1.0e-9 * a.tv_nsec; - d /= b; - - c.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); - c.tv_sec = int(d); - - return c; -} - -timespec operator /( const timespec& a, unsigned long b ) -{ - timespec c; - double d = a.tv_sec + 1.0e-9 * a.tv_nsec; - d /= b; - - c.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); - c.tv_sec = int(d); - - return c; -} - -timespec& operator +=( timespec& a, const timespec& b ) -{ - a.tv_sec += b.tv_sec; - a.tv_nsec += b.tv_nsec; - a.tv_sec += a.tv_nsec / 1000000000; - a.tv_nsec %= 1000000000; - - return a; -} - -timespec& operator -=( timespec& a, const timespec& b ) -{ - a.tv_sec = a.tv_sec > b.tv_sec ? a.tv_sec - b.tv_sec : 0; // out_of_range? - if ( a.tv_nsec >= b.tv_nsec ) { - a.tv_nsec -= b.tv_nsec; - } else if ( a.tv_sec > 0 ) { - --a.tv_sec; - a.tv_nsec += 1000000000 - b.tv_nsec; - } else { - a.tv_nsec = 0; // out_of_range? - } - - return a; -} - -timespec& operator /=( timespec& a, unsigned b ) -{ - double d = a.tv_sec + 1.0e-9 * a.tv_nsec; - d /= b; - - a.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); - a.tv_sec = int(d); - - return a; -} - -timespec& operator /=( timespec& a, unsigned long b ) -{ - double d = a.tv_sec + 1.0e-9 * a.tv_nsec; - d /= b; - - a.tv_nsec = int(1.0e9 * modf( d, &d ) + 0.5); - a.tv_sec = int(d); - - return a; -} - -bool operator ==( const timespec& a, const timespec& b ) -{ - return (a.tv_sec == b.tv_sec) && (a.tv_nsec == b.tv_nsec); -} - -bool operator >( const timespec& a, const timespec& b ) -{ - if ( a.tv_sec > b.tv_sec ) { - return true; - } else if ( b.tv_sec > a.tv_sec ) { - return false; - } - - return a.tv_nsec > b.tv_nsec; -} - -bool operator >=( const timespec& a, const timespec& b ) -{ - if ( a.tv_sec > b.tv_sec ) { - return true; - } else if ( b.tv_sec > a.tv_sec ) { - return false; - } - - return a.tv_nsec >= b.tv_nsec; -} - -bool operator <( const timespec& a, const timespec& b ) -{ - if ( a.tv_sec < b.tv_sec ) { - return true; - } else if ( b.tv_sec < a.tv_sec ) { - return false; - } - - return a.tv_nsec < b.tv_nsec; -} - -bool operator <=( const timespec& a, const timespec& b ) -{ - if ( a.tv_sec < b.tv_sec ) { - return true; - } else if ( b.tv_sec < a.tv_sec ) { - return false; - } - - return a.tv_nsec <= b.tv_nsec; -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |