From cacff1be886ed975bbef1b17151b25c633711256 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 23 Mar 2009 01:05:54 +0000 Subject: * No longer block while waiting for a lock on a store path. Instead poll for it (i.e. if we can't acquire the lock, then let the main select() loop wait for at most a few seconds and then try again). This improves parallelism: if two nix-store processes are both trying to build a path at the same time, the second one shouldn't block; it should first see if it can build other goals. Also, it prevents the deadlocks that have been occuring in Hydra lately, where a process waits for a lock held by another process that's waiting for a lock held by the first. The downside is that polling isn't really elegant, but POSIX doesn't provide a way to wait for locks in a select() loop. The only solution would be to spawn a thread for each lock to do a blocking fcntl() and then signal the main thread, but that would require pthreads. --- src/libstore/pathlocks.cc | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/libstore/pathlocks.cc') diff --git a/src/libstore/pathlocks.cc b/src/libstore/pathlocks.cc index f8753bcb66e0..cad893726720 100644 --- a/src/libstore/pathlocks.cc +++ b/src/libstore/pathlocks.cc @@ -141,9 +141,9 @@ PathLocks::PathLocks(const PathSet & paths, const string & waitMsg) } -void PathLocks::lockPaths(const PathSet & _paths, const string & waitMsg) +bool PathLocks::lockPaths(const PathSet & _paths, + const string & waitMsg, bool wait) { - /* May be called only once! */ assert(fds.empty()); /* Note that `fds' is built incrementally so that the destructor @@ -174,8 +174,15 @@ void PathLocks::lockPaths(const PathSet & _paths, const string & waitMsg) /* Acquire an exclusive lock. */ if (!lockFile(fd, ltWrite, false)) { - if (waitMsg != "") printMsg(lvlError, waitMsg); - lockFile(fd, ltWrite, true); + if (wait) { + if (waitMsg != "") printMsg(lvlError, waitMsg); + lockFile(fd, ltWrite, true); + } else { + /* Failed to lock this path; release all other + locks. */ + unlock(); + return false; + } } debug(format("lock acquired on `%1%'") % lockPath); @@ -199,6 +206,8 @@ void PathLocks::lockPaths(const PathSet & _paths, const string & waitMsg) fds.push_back(FDPair(fd.borrow(), lockPath)); lockedPaths.insert(lockPath); } + + return true; } -- cgit 1.4.1