Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [BUG] zsystem:34: flock: invalid timeout value: '0'



Roman Perepelitsa (Saturday 2020-06-27):
> Timeout strictly between 0 and 1 microsecond is not much different
> from timeout between 1 and 2 microseconds. You can either round up or
> down in both cases. Rounding up is the right thing to do for sleeps
> and timeouts because it allows you to provide a guarantee.

That's a good point.  I don't think we can offer an actual guarantee
(or at least I don't have time to really think it through), but
rounding up can be done, alternative patch below.

					Best,
					Cedric Ware.


diff --git a/Src/Modules/system.c b/Src/Modules/system.c
index 972aa0767..ecd4e2546 100644
--- a/Src/Modules/system.c
+++ b/Src/Modules/system.c
@@ -597,7 +597,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		 * a 32-bit int and CLOCK_MONOTONIC is not supported, in which
 		 * case there is a Y2038 problem anyway.
 		 */
-		if (timeout < 1e-6 || timeout > 1073741823.) {
+		if (timeout > 1073741823.) {
 		    zwarnnam(nam, "flock: invalid timeout value: '%s'",
 			     optarg);
 		    return 1;
@@ -623,7 +623,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		    timeout_param.type = MN_FLOAT;
 		    timeout_param.u.d = (double)timeout_param.u.l;
 		}
-		timeout_param.u.d *= 1e6;
+		timeout_param.u.d = ceil(timeout_param.u.d * 1e6);
 		if (timeout_param.u.d < 1
 		    || timeout_param.u.d > 0.999 * LONG_MAX) {
 		    zwarnnam(nam, "flock: invalid interval value: '%s'",
@@ -704,7 +704,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 	zgettime_monotonic_if_available(&now);
 	end.tv_sec = now.tv_sec;
 	end.tv_nsec = now.tv_nsec;
-	end.tv_nsec += modf(timeout, &timeout_s) * 1000000000L;
+	end.tv_nsec += ceil(modf(timeout, &timeout_s) * 1000000000L);
 	end.tv_sec += timeout_s;
 	if (end.tv_nsec >= 1000000000L) {
 	    end.tv_nsec -= 1000000000L;
diff --git a/Test/V14system.ztst b/Test/V14system.ztst
index b8af96cda..100daab08 100644
--- a/Test/V14system.ztst
+++ b/Test/V14system.ztst
@@ -13,27 +13,26 @@
 %test
 
   (
-    zsystem flock -t 0.1 -i 0.000001 $tst_dir/file
+    zsystem flock -t 0 -i 0.000001 $tst_dir/file    &&
+    zsystem flock -t 0.1 -i 0.000001 $tst_dir/file  &&
+    zsystem flock -t 0.1 -i 0.0000001 $tst_dir/file &&
+    zsystem flock -t 1 -i 0.000001 $tst_dir/file
   )
 0:zsystem flock valid time arguments
 
   (
-    zsystem flock -t -1         $tst_dir/file ||
-    zsystem flock -t 0.49e-6    $tst_dir/file ||
     zsystem flock -t 1073741824 $tst_dir/file ||
     zsystem flock -t 1e100      $tst_dir/file ||
     zsystem flock -i -1         $tst_dir/file ||
-    zsystem flock -i 0.49e-6    $tst_dir/file ||
+    zsystem flock -i 0          $tst_dir/file ||
     zsystem flock -i 1e100      $tst_dir/file
   )
 1:zsystem flock invalid time arguments
-?(eval):zsystem:2: flock: invalid timeout value: '-1'
-?(eval):zsystem:3: flock: invalid timeout value: '0.49e-6'
-?(eval):zsystem:4: flock: invalid timeout value: '1073741824'
-?(eval):zsystem:5: flock: invalid timeout value: '1e100'
-?(eval):zsystem:6: flock: invalid interval value: '-1'
-?(eval):zsystem:7: flock: invalid interval value: '0.49e-6'
-?(eval):zsystem:8: flock: invalid interval value: '1e100'
+?(eval):zsystem:2: flock: invalid timeout value: '1073741824'
+?(eval):zsystem:3: flock: invalid timeout value: '1e100'
+?(eval):zsystem:4: flock: invalid interval value: '-1'
+?(eval):zsystem:5: flock: invalid interval value: '0'
+?(eval):zsystem:6: flock: invalid interval value: '1e100'
 
   (
     # Lock file for 1 second in the background.



Messages sorted by: Reverse Date, Date, Thread, Author