Change in osmo-trx[master]: Timeval: Move implementation to use clock_gettime and timespec

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Wed Dec 12 17:07:45 UTC 2018


Pau Espin Pedrol has uploaded this change for review. ( https://gerrit.osmocom.org/12280


Change subject: Timeval: Move implementation to use clock_gettime and timespec
......................................................................

Timeval: Move implementation to use clock_gettime and timespec

According to gettimeofday manual:
"Applications should use the clock_gettime() function instead of the
obsolescent gettimeofday() function."

Furthermore, it may be desirable in the future to use other clocks such
as monotonic.

Change-Id: I2286998c5eefbf3c3dfb105c223daec7a1083803
---
M CommonLibs/Timeval.cpp
M CommonLibs/Timeval.h
2 files changed, 28 insertions(+), 28 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/80/12280/1

diff --git a/CommonLibs/Timeval.cpp b/CommonLibs/Timeval.cpp
index 991e241..21072fc 100644
--- a/CommonLibs/Timeval.cpp
+++ b/CommonLibs/Timeval.cpp
@@ -34,36 +34,33 @@
 	now();
 	unsigned sec = offset/1000;
 	unsigned msec = offset%1000;
-	mTimeval.tv_usec += msec*1000;
-	mTimeval.tv_sec += sec;
-	if (mTimeval.tv_usec>1000000) {
-		mTimeval.tv_usec -= 1000000;
-		mTimeval.tv_sec += 1;
+	mTimespec.tv_nsec += msec*1000*1000;
+	mTimespec.tv_sec += sec;
+	if (mTimespec.tv_nsec > 1000*1000*1000) {
+		mTimespec.tv_nsec -= 1000*1000*1000;
+		mTimespec.tv_sec += 1;
 	}
 }
 
 
 struct timespec Timeval::timespec() const
 {
-	struct timespec retVal;
-	retVal.tv_sec = mTimeval.tv_sec;
-	retVal.tv_nsec = 1000 * (long)mTimeval.tv_usec;
-	return retVal;
+	return mTimespec;
 }
 
 
 bool Timeval::passed() const
 {
 	Timeval nowTime;
-	if (nowTime.mTimeval.tv_sec < mTimeval.tv_sec) return false;
-	if (nowTime.mTimeval.tv_sec > mTimeval.tv_sec) return true;
-	if (nowTime.mTimeval.tv_usec >= mTimeval.tv_usec) return true;
+	if (nowTime.mTimespec.tv_sec < mTimespec.tv_sec) return false;
+	if (nowTime.mTimespec.tv_sec > mTimespec.tv_sec) return true;
+	if (nowTime.mTimespec.tv_nsec >= mTimespec.tv_nsec) return true;
 	return false;
 }
 
 double Timeval::seconds() const
 {
-	return ((double)mTimeval.tv_sec) + 1e-6*((double)mTimeval.tv_usec);
+	return ((double)mTimespec.tv_sec) + 1e-9*((double)mTimespec.tv_nsec);
 }
 
 
@@ -72,8 +69,8 @@
 {
 	// 2^31 milliseconds is just over 4 years.
 	int32_t deltaS = other.sec() - sec();
-	int32_t deltaUs = other.usec() - usec();
-	return 1000*deltaS + deltaUs/1000;
+	int32_t deltaNs = other.nsec() - nsec();
+	return 1000*deltaS + deltaNs/1000000;
 }
 	
 
@@ -89,7 +86,7 @@
 
 ostream& operator<<(ostream& os, const struct timespec& ts)
 {
-	os << ts.tv_sec << "," << ts.tv_nsec;
+	os << ts.tv_sec << "," << ts.tv_nsec/1000;
 	return os;
 }
 
diff --git a/CommonLibs/Timeval.h b/CommonLibs/Timeval.h
index c497864..b780af1 100644
--- a/CommonLibs/Timeval.h
+++ b/CommonLibs/Timeval.h
@@ -42,12 +42,12 @@
 
 	private:
 
-	struct timeval mTimeval;
+	struct timespec mTimespec;
 
 	public:
 
-	/** Set the value to gettimeofday. */
-	void now() { gettimeofday(&mTimeval,NULL); }
+	/** Set the value to current time. */
+	void now() { clock_gettime(CLOCK_REALTIME, &mTimespec); }
 
 	/** Set the value to gettimeofday plus an offset. */
 	void future(unsigned ms);
@@ -55,16 +55,18 @@
 	//@{
 	Timeval(unsigned sec, unsigned usec)
 	{
-		mTimeval.tv_sec = sec;
-		mTimeval.tv_usec = usec;
+		mTimespec.tv_sec = sec;
+		mTimespec.tv_nsec = usec*1000;
 	}
 
 	Timeval(const struct timeval& wTimeval)
-		:mTimeval(wTimeval)
-	{}
+	{
+		mTimespec.tv_sec = wTimeval.tv_sec;
+		mTimespec.tv_nsec = wTimeval.tv_sec*1000;
+	}
 
 	/**
-		Create a Timeval offset into the future.
+		Create a Timespec offset into the future.
 		@param offset milliseconds
 	*/
 	Timeval(unsigned offset=0) { future(offset); }
@@ -76,8 +78,9 @@
 	/** Return total seconds. */
 	double seconds() const;
 
-	uint32_t sec() const { return mTimeval.tv_sec; }
-	uint32_t usec() const { return mTimeval.tv_usec; }
+	uint32_t sec() const { return mTimespec.tv_sec; }
+	uint32_t usec() const { return mTimespec.tv_nsec / 1000; }
+	uint32_t nsec() const { return mTimespec.tv_nsec; }
 
 	/** Return differnce from other (other-self), in ms. */
 	long delta(const Timeval& other) const;
@@ -88,11 +91,11 @@
 	/** Remaining time in ms. */
 	long remaining() const { return -elapsed(); }
 
-	/** Return true if the time has passed, as per gettimeofday. */
+	/** Return true if the time has passed, as per clock_gettime(CLOCK_REALTIME). */
 	bool passed() const;
 
 	/** Add a given number of minutes to the time. */
-	void addMinutes(unsigned minutes) { mTimeval.tv_sec += minutes*60; }
+	void addMinutes(unsigned minutes) { mTimespec.tv_sec += minutes*60; }
 
 };
 

-- 
To view, visit https://gerrit.osmocom.org/12280
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2286998c5eefbf3c3dfb105c223daec7a1083803
Gerrit-Change-Number: 12280
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181212/f050784c/attachment.htm>


More information about the gerrit-log mailing list