* [PATCH] Allow zsystem flock to query without second-lasting timeout @ 2016-09-10 15:49 Sebastian Gniazdowski 2016-09-10 17:01 ` Bart Schaefer 0 siblings, 1 reply; 3+ messages in thread From: Sebastian Gniazdowski @ 2016-09-10 15:49 UTC (permalink / raw) To: Zsh hackers list [-- Attachment #1: Type: text/plain, Size: 486 bytes --] Hello, attached patch is dumb, and allows to use negative timeout, which results in a quick (non-waiting) single fcntl() call. Such timeout can already be passed to zsystem flock, because (Src/Modules/system.c): timeout = (time_t)mathevali(optarg); and time_t is signed on Darwin, a linux box that a person tested and I guess it's always signed because "time can be negative" – but a knowledge here would be needed. Best regards, Sebastian Gniazdowski [-- Attachment #2: system_flock.diff --] [-- Type: text/plain, Size: 473 bytes --] diff --git a/Src/Modules/system.c b/Src/Modules/system.c index 1ee61c0..2769f75 100644 --- a/Src/Modules/system.c +++ b/Src/Modules/system.c @@ -636,7 +636,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) lck.l_start = 0; lck.l_len = 0; /* lock the whole file */ - if (timeout > 0) { + if (timeout != 0) { time_t end = time(NULL) + (time_t)timeout; while (fcntl(flock_fd, F_SETLK, &lck) < 0) { if (errflag) ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Allow zsystem flock to query without second-lasting timeout 2016-09-10 15:49 [PATCH] Allow zsystem flock to query without second-lasting timeout Sebastian Gniazdowski @ 2016-09-10 17:01 ` Bart Schaefer 2016-09-10 22:32 ` Sebastian Gniazdowski 0 siblings, 1 reply; 3+ messages in thread From: Bart Schaefer @ 2016-09-10 17:01 UTC (permalink / raw) To: Zsh hackers list On Sep 10, 5:49pm, Sebastian Gniazdowski wrote: } } attached patch is dumb, and allows to use negative timeout, which } results in a quick (non-waiting) single fcntl() call. I think a better approach would be to allow a zero timeout. (Why was there already a cast of time_t to time_t in the time(NULL) expression?) Hopefully no one is already using the undocumented behavior that "zsystem flock -t 0 ..." nevertheless waited forever. diff --git a/Src/Modules/system.c b/Src/Modules/system.c index 1ee61c0..afaec26 100644 --- a/Src/Modules/system.c +++ b/Src/Modules/system.c @@ -521,7 +521,7 @@ static int bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) { int cloexec = 1, unlock = 0, readlock = 0; - time_t timeout = 0; + zlong timeout = -1; char *fdvar = NULL; #ifdef HAVE_FCNTL_H struct flock lck; @@ -573,7 +573,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) } else { optarg = *args++; } - timeout = (time_t)mathevali(optarg); + timeout = mathevali(optarg); break; case 'u': @@ -650,7 +650,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) sleep(1); } } else { - while (fcntl(flock_fd, F_SETLKW, &lck) < 0) { + while (fcntl(flock_fd, timeout == 0 ? F_SETLK : F_SETLKW, &lck) < 0) { if (errflag) return 1; if (errno == EINTR) ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Allow zsystem flock to query without second-lasting timeout 2016-09-10 17:01 ` Bart Schaefer @ 2016-09-10 22:32 ` Sebastian Gniazdowski 0 siblings, 0 replies; 3+ messages in thread From: Sebastian Gniazdowski @ 2016-09-10 22:32 UTC (permalink / raw) To: Bart Schaefer; +Cc: Zsh hackers list Really cool, thanks, I will use this via is-at-least, now I've included cropped-down version of util-linux/flock in my plugin, it amazingly compiles on OS X (so probably also FreeBSD), and of course Linux Best regards, Sebastian Gniazdowski On 10 September 2016 at 19:01, Bart Schaefer <schaefer@brasslantern.com> wrote: > On Sep 10, 5:49pm, Sebastian Gniazdowski wrote: > } > } attached patch is dumb, and allows to use negative timeout, which > } results in a quick (non-waiting) single fcntl() call. > > I think a better approach would be to allow a zero timeout. (Why was > there already a cast of time_t to time_t in the time(NULL) expression?) > > Hopefully no one is already using the undocumented behavior that > "zsystem flock -t 0 ..." nevertheless waited forever. > > > diff --git a/Src/Modules/system.c b/Src/Modules/system.c > index 1ee61c0..afaec26 100644 > --- a/Src/Modules/system.c > +++ b/Src/Modules/system.c > @@ -521,7 +521,7 @@ static int > bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) > { > int cloexec = 1, unlock = 0, readlock = 0; > - time_t timeout = 0; > + zlong timeout = -1; > char *fdvar = NULL; > #ifdef HAVE_FCNTL_H > struct flock lck; > @@ -573,7 +573,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) > } else { > optarg = *args++; > } > - timeout = (time_t)mathevali(optarg); > + timeout = mathevali(optarg); > break; > > case 'u': > @@ -650,7 +650,7 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) > sleep(1); > } > } else { > - while (fcntl(flock_fd, F_SETLKW, &lck) < 0) { > + while (fcntl(flock_fd, timeout == 0 ? F_SETLK : F_SETLKW, &lck) < 0) { > if (errflag) > return 1; > if (errno == EINTR) ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-09-10 23:40 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2016-09-10 15:49 [PATCH] Allow zsystem flock to query without second-lasting timeout Sebastian Gniazdowski 2016-09-10 17:01 ` Bart Schaefer 2016-09-10 22:32 ` Sebastian Gniazdowski
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).