cacff1be88
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.
51 lines
1.1 KiB
C++
51 lines
1.1 KiB
C++
#ifndef __PATHLOCKS_H
|
|
#define __PATHLOCKS_H
|
|
|
|
#include "types.hh"
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
/* Open (possibly create) a lock file and return the file descriptor.
|
|
-1 is returned if create is false and the lock could not be opened
|
|
because it doesn't exist. Any other error throws an exception. */
|
|
int openLockFile(const Path & path, bool create);
|
|
|
|
/* Delete an open lock file. Both must be called to be fully portable
|
|
between Unix and Windows. */
|
|
void deleteLockFilePreClose(const Path & path, int fd);
|
|
void deleteLockFilePostClose(const Path & path);
|
|
|
|
enum LockType { ltRead, ltWrite, ltNone };
|
|
|
|
bool lockFile(int fd, LockType lockType, bool wait);
|
|
|
|
|
|
class PathLocks
|
|
{
|
|
private:
|
|
typedef std::pair<int, Path> FDPair;
|
|
list<FDPair> fds;
|
|
bool deletePaths;
|
|
|
|
public:
|
|
PathLocks();
|
|
PathLocks(const PathSet & paths,
|
|
const string & waitMsg = "");
|
|
bool lockPaths(const PathSet & _paths,
|
|
const string & waitMsg = "",
|
|
bool wait = true);
|
|
~PathLocks();
|
|
void unlock();
|
|
void setDeletion(bool deletePaths);
|
|
};
|
|
|
|
|
|
bool pathIsLockedByMe(const Path & path);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* !__PATHLOCKS_H */
|