From patchwork Sat Dec 17 01:49:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [3,of,3] chg: remove locks From: Jun Wu X-Patchwork-Id: 17941 Message-Id: <43d03fac159c7b3c1d5a.1481939391@x1c> To: Date: Sat, 17 Dec 2016 01:49:51 +0000 # HG changeset patch # User Jun Wu # Date 1481938885 0 # Sat Dec 17 01:41:25 2016 +0000 # Node ID 43d03fac159c7b3c1d5ae1ca720ad2d431984aa3 # Parent 6c9ce8399350d8287599cd802b91adf73db08759 # Available At https://bitbucket.org/quark-zju/hg-draft # hg pull https://bitbucket.org/quark-zju/hg-draft -r 43d03fac159c chg: remove locks See the previous two patches for why we don't need locks any more. diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c --- a/contrib/chg/chg.c +++ b/contrib/chg/chg.c @@ -34,8 +34,6 @@ struct cmdserveropts { char initsockname[UNIX_PATH_MAX]; char redirectsockname[UNIX_PATH_MAX]; - char lockfile[UNIX_PATH_MAX]; size_t argsize; const char **args; - int lockfd; int sockdirfd; }; @@ -43,5 +41,4 @@ struct cmdserveropts { static void initcmdserveropts(struct cmdserveropts *opts) { memset(opts, 0, sizeof(struct cmdserveropts)); - opts->lockfd = -1; opts->sockdirfd = -1; } @@ -51,5 +48,4 @@ static void freecmdserveropts(struct cmd opts->args = NULL; opts->argsize = 0; - assert(opts->lockfd == -1 && "should be closed by unlockcmdserver()"); if (opts->sockdirfd >= 0) { close(opts->sockdirfd); @@ -158,11 +154,7 @@ static void setcmdserveropts(struct cmds const char *basename = (envsockname) ? envsockname : sockdir; const char *sockfmt = (envsockname) ? "%s" : "%s/server"; - const char *lockfmt = (envsockname) ? "%s.lock" : "%s/lock"; r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); if (r < 0 || (size_t)r >= sizeof(opts->sockname)) abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); - r = snprintf(opts->lockfile, sizeof(opts->lockfile), lockfmt, basename); - if (r < 0 || (size_t)r >= sizeof(opts->lockfile)) - abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u", opts->sockname, (unsigned)getpid()); @@ -171,37 +163,4 @@ static void setcmdserveropts(struct cmds } -/* - * Acquire a file lock that indicates a client is trying to start and connect - * to a server, before executing a command. The lock is released upon exit or - * explicit unlock. Will block if the lock is held by another process. - */ -static void lockcmdserver(struct cmdserveropts *opts) -{ - if (opts->lockfd == -1) { - opts->lockfd = open(opts->lockfile, - O_RDWR | O_CREAT | O_NOFOLLOW, 0600); - if (opts->lockfd == -1) - abortmsgerrno("cannot create lock file %s", - opts->lockfile); - fsetcloexec(opts->lockfd); - } - int r = flock(opts->lockfd, LOCK_EX); - if (r == -1) - abortmsgerrno("cannot acquire lock"); -} - -/* - * Release the file lock held by calling lockcmdserver. Will do nothing if - * lockcmdserver is not called. - */ -static void unlockcmdserver(struct cmdserveropts *opts) -{ - if (opts->lockfd == -1) - return; - flock(opts->lockfd, LOCK_UN); - close(opts->lockfd); - opts->lockfd = -1; -} - static const char *gethgcmd(void) { @@ -309,8 +268,6 @@ static hgclient_t *connectcmdserver(stru return hgc; - lockcmdserver(opts); hgc = hgc_open(sockname); if (hgc) { - unlockcmdserver(opts); debugmsg("cmdserver is started by another process"); return hgc; @@ -335,5 +292,4 @@ static hgclient_t *connectcmdserver(stru } - unlockcmdserver(opts); return hgc; }