diff -ur zsh-5.7.1.orig/Src/Modules/system.c zsh-5.7.1/Src/Modules/system.c --- zsh-5.7.1.orig/Src/Modules/system.c 2018-12-14 08:50:17.000000000 +0100 +++ zsh-5.7.1/Src/Modules/system.c 2019-07-29 20:19:48.835060428 +0200 @@ -532,6 +532,8 @@ { int cloexec = 1, unlock = 0, readlock = 0; zlong timeout = -1; + long timeout_retry = 1e6; + mnumber timeout_param; char *fdvar = NULL; #ifdef HAVE_FCNTL_H struct flock lck; @@ -583,7 +585,35 @@ } else { optarg = *args++; } - timeout = mathevali(optarg); + timeout_param = matheval(optarg); + if (!(timeout_param.type & MN_FLOAT)) { + timeout_param.type = MN_FLOAT; + timeout_param.u.d = (double)timeout_param.u.l; + } + timeout = (zlong)(timeout_param.u.d * 1e6); + break; + + case 'p': + /* retry period in seconds */ + if (optptr[1]) { + optarg = optptr + 1; + optptr += strlen(optarg) - 1; + } else if (!*args) { + zwarnnam(nam, + "flock: option %c requires a numeric retry period", + opt); + return 1; + } else { + optarg = *args++; + } + timeout_param = matheval(optarg); + if (!(timeout_param.type & MN_FLOAT)) { + timeout_param.type = MN_FLOAT; + timeout_param.u.d = (double)timeout_param.u.l; + } + zlong timeout_retry_tmp = timeout_param.u.d * 1e6; + timeout_retry = (timeout_retry_tmp > LONG_MAX) ? + LONG_MAX : timeout_retry_tmp; break; case 'u': @@ -647,7 +677,7 @@ lck.l_len = 0; /* lock the whole file */ if (timeout > 0) { - time_t end = time(NULL) + (time_t)timeout; + zlong end = time_clock_us() + timeout; while (fcntl(flock_fd, F_SETLK, &lck) < 0) { if (errflag) { zclose(flock_fd); @@ -658,11 +688,15 @@ zwarnnam(nam, "failed to lock file %s: %e", args[0], errno); return 1; } - if (time(NULL) >= end) { + zlong now = time_clock_us(); + if (now >= end) { zclose(flock_fd); return 2; } - sleep(1); + if (now + timeout_retry > end) { + timeout_retry = end - now; + } + zsleep(timeout_retry); } } else { while (fcntl(flock_fd, timeout == 0 ? F_SETLK : F_SETLKW, &lck) < 0) { diff -ur zsh-5.7.1.orig/Src/utils.c zsh-5.7.1/Src/utils.c --- zsh-5.7.1.orig/Src/utils.c 2019-02-01 01:37:34.000000000 +0100 +++ zsh-5.7.1/Src/utils.c 2019-07-29 20:24:57.827073902 +0200 @@ -2724,6 +2724,26 @@ } /* + * Return the current time in microseconds, using the system's + * monotonic clock if supported, the wall clock if not. + */ + +/**/ +zlong +time_clock_us(void) +{ +#if defined(HAS_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * (zlong)1e6 + ts.tv_nsec / 1000; +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * (zlong)1e6 + tv.tv_usec; +#endif +} + +/* * Sleep for the given number of microseconds --- must be within * range of a long at the moment, but this is only used for * limited internal purposes.