commit 34cd5bd02930630a98b6a71abe11c2b105f24e05
Author: Hamlet <_hamlet@libero.it>
Date:   Sun Jan 15 16:41:42 2017 -0600

    Don't use timestamp counter as timer.
    
    Linux-specific: the old implementation using CPU timestamp counters to
    estimate the time elapsed is incompatible with multi-core systems.

diff --git a/sys/linux/main.cpp b/sys/linux/main.cpp
index dbb72d5d5..cfe9da78a 100644
--- a/sys/linux/main.cpp
+++ b/sys/linux/main.cpp
@@ -16,6 +16,17 @@
  $Author$ (Author of last commit)
  
 ******************************************************************************/
+
+// define DO_USE_RDTSC only if we want AND we can use the timestamp counter
+#if (USE_RDTSC) and defined( __i386__ )
+# define DO_USE_RDTSC 1
+#else
+# define DO_USE_RDTSC 0
+// # define LINUX_CLOCK CLOCK_REALTIME // which clock to use for timing
+// CLOCK_THREAD_CPUTIME_ID is Linux-specific (2.6.12+); see `man 2 clock_gettime`
+# define LINUX_CLOCK CLOCK_THREAD_CPUTIME_ID // which clock to use for timing
+#endif
+
 #include "../../idlib/precompiled.h"
 #include "../posix/posix_public.h"
 #include "../sys_local.h"
@@ -27,6 +38,8 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>
+#include <cstring> // strerror()
+#include <ctime> // clock_gettime()
 
 #ifdef ID_MCHECK
 #include <mcheck.h>
@@ -285,7 +298,13 @@ Sys_GetClockticks
 ===============
 */
 double Sys_GetClockTicks( void ) {
-#if defined( __i386__ )
+#if (DO_USE_RDTSC)
+	// it appears like every core has its own timestamp counter,
+	// and they (may?) count different cycles;
+	// so if the thread jumps from one core to another between the
+	// "start = Sys_GetClockTicks();" and the "end = Sys_GetClockTicks();",
+	// the "stop - start" number is nonsense.
+	// Even `MeasureClockTicks()` might be affected.
 	unsigned long lo, hi;
 
 	__asm__ __volatile__ (
@@ -298,8 +317,12 @@ double Sys_GetClockTicks( void ) {
 						  "pop %%ebx\n"
 						  : "=r" (lo), "=r" (hi) );
 	return (double) lo + (double) 0xFFFFFFFF * hi;
-#else
-#error unsupported CPU
+#else // do not use the timestamp counter
+	// this is poors' magic inspired from GNU implementation of the standard
+	// library std::chrono::system_clock::now():
+	timespec timePoint;
+	clock_gettime(LINUX_CLOCK, &timePoint);
+	return ((double) timePoint.tv_sec) * 1e9 + (double) timePoint.tv_nsec;
 #endif
 }
 
@@ -309,12 +332,16 @@ MeasureClockTicks
 ===============
 */
 double MeasureClockTicks( void ) {
+#if (DO_USE_RDTSC)
 	double t0, t1;
 
 	t0 = Sys_GetClockTicks( );
 	Sys_Sleep( 1000 );
 	t1 = Sys_GetClockTicks( );	
 	return t1 - t0;
+#else
+	return 1e9; // the value returned by Sys_GetClockTicks() is nanoseconds
+#endif
 }
 
 /*
