]> Kevux Git Server - fll/commitdiff
Revert: Rewrite timing logic in controller, and use different logic.
authorKevin Day <Kevin@kevux.org>
Sun, 7 Jul 2024 21:08:48 +0000 (16:08 -0500)
committerKevin Day <Kevin@kevux.org>
Sun, 7 Jul 2024 21:16:05 +0000 (16:16 -0500)
This reverts commit 9b21b888e52d9a7268c231bc0d520ec0be9bfcb8.

This is not actually needed because a 32-bit long should not overflow with just 1000000000.
The logic I replaced it with was sloppy anyway and left out some cases.

The long representing nanoseconds can store greater than 999999999.
Make sure that is handled before adding to the tv_nsec.

level_3/controller/c/controller/private-controller.c

index 9ce6e92c17e2852491498434aa728b074cc1a0bb..c9e2f16ba5f26473e930977c1449e51f314630c3 100644 (file)
@@ -708,13 +708,29 @@ extern "C" {
     gettimeofday(&now, 0);
 
     time->tv_sec = now.tv_sec + seconds;
-    time->tv_nsec = now.tv_usec * 1000;
+    time->tv_nsec = (now.tv_usec * 1000);
 
-    // If result would be greater than 1 second, then increment seconds without overflowing.
-    if (time->tv_nsec > 500000000 && nanoseconds > 500000000) {
+    // Do not allow for nanoseconds to be too large.
+    if (nanoseconds > 999999999) {
+      long ns = nanoseconds;
+
+      do {
+        ns -= 1000000000;
+        ++(time->tv_sec);
+
+      } while (ns > 999999999);
+
+      time->tv_nsec += ns;
+    }
+    else {
+      time->tv_nsec += nanoseconds;
+    }
+
+    // If tv_nsec is 1 second or greater, then increment seconds.
+    if (time->tv_nsec > 999999999) {
       ++(time->tv_sec);
 
-      time->tv_nsec = (time->tv_nsec - 500000000) + (nanoseconds - 500000000);
+      time->tv_nsec -= 1000000000;
     }
   }
 #endif // _di_controller_time_