[uClibc] clock() for long-running process

Atsushi Nemoto anemo at mba.ocn.ne.jp
Fri Apr 30 13:58:08 UTC 2004


Current clock() implementation will return -1 after the process run
2148 sec.

Surely man 3 clock states:

  If the processor time used is not available or its value cannot be
  represented, the function returns the value (clock_t)-1.

But returning -1 will cause some problems for long-running process.
Just ignoring overflow seems to be a best solution for me.

How about this patch?

diff -u -r1.21 time.c
--- a/time.c	11 Feb 2004 23:48:40 -0000	1.21
+++ b/time.c	30 Apr 2004 13:06:24 -0000
@@ -410,17 +410,12 @@
 #endif
 
 #if (CLK_TCK == CLOCKS_PER_SEC)
-	return (t <= LONG_MAX) ? t : -1;
+	return t;
 #elif (CLOCKS_PER_SEC % CLK_TCK) == 0
-	return (t <= (LONG_MAX / (CLOCKS_PER_SEC/CLK_TCK)))
-		? t * (CLOCKS_PER_SEC/CLK_TCK)
-		: -1;
+	return t * (CLOCKS_PER_SEC/CLK_TCK);
 #else
-	return (t <= ((LONG_MAX / CLOCKS_PER_SEC) * CLK_TCK
-				  + ((LONG_MAX % CLOCKS_PER_SEC) * CLK_TCK) / CLOCKS_PER_SEC))
-		? (((t / CLK_TCK) * CLOCKS_PER_SEC)
-		   + (((t % CLK_TCK) * CLOCKS_PER_SEC) / CLK_TCK))
-		: -1;
+	return ((t / CLK_TCK) * CLOCKS_PER_SEC)
+		+ (((t % CLK_TCK) * CLOCKS_PER_SEC) / CLK_TCK);
 #endif
 }
 
---
Atsushi Nemoto



More information about the uClibc mailing list