Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
pathlocks.hh
Go to the documentation of this file.
1#pragma once
3
8#include <chrono>
9#include <kj/async.h>
10#include <kj/common.h>
11
12namespace nix {
13
19AutoCloseFD openLockFile(const Path & path, bool create);
20
21enum LockType { ltRead, ltWrite };
22
23void lockFile(int fd, LockType lockType, NeverAsync = {});
24kj::Promise<Result<void>> lockFileAsync(int fd, LockType lockType);
31bool unsafeLockFileSingleThreaded(int fd, LockType lockType, std::chrono::seconds timeout);
32bool tryLockFile(int fd, LockType lockType);
33void unlockFile(int fd);
34
35class PathLock
36{
37 friend kj::Promise<Result<PathLock>> lockPathAsync(const Path & path, std::string_view waitMsg);
38 friend PathLock lockPath(const Path & path, std::string_view waitMsg, NeverAsync);
39 friend std::optional<PathLock> tryLockPath(const Path & path);
40
41 AutoCloseFD fd;
42 Path path;
43
44 PathLock(AutoCloseFD fd, const Path & path): fd(std::move(fd)), path(path) {}
45
46 static std::optional<PathLock>
47 lockImpl(const Path & path, std::string_view waitMsg, bool wait, NeverAsync = {});
48
49public:
50 PathLock(PathLock &&) = default;
51 PathLock & operator=(PathLock &&) = default;
52 ~PathLock();
53
54 void unlock();
55};
56
57kj::Promise<Result<PathLock>> lockPathAsync(const Path & path, std::string_view waitMsg = "");
58PathLock lockPath(const Path & path, std::string_view waitMsg = "", NeverAsync = {});
59std::optional<PathLock> tryLockPath(const Path & path);
60
61using PathLocks = std::list<PathLock>;
62
63PathLocks lockPaths(const PathSet & paths, std::string_view waitMsg = "", NeverAsync = {});
64std::optional<PathLocks> tryLockPaths(const PathSet & paths);
65
66class FdLock
67{
68 struct Unlocker
69 {
70 void operator()(AutoCloseFD * fd)
71 {
72 try {
73 unlockFile(fd->get());
74 } catch (SysError &) {
75 ignoreExceptionInDestructor();
76 }
77 }
78 };
79
80 std::unique_ptr<AutoCloseFD, Unlocker> fd;
81
82 explicit FdLock(AutoCloseFD & fd): fd(&fd) {}
83
84public:
85 static constexpr struct DontWait { explicit DontWait() = default; } dont_wait;
86
87 FdLock(AutoCloseFD & fd, LockType lockType, DontWait);
88 FdLock(AutoCloseFD & fd, LockType lockType, std::string_view waitMsg, NeverAsync = {});
89
90 static kj::Promise<Result<FdLock>>
91 lockAsync(AutoCloseFD & fd, LockType lockType, std::string_view waitMsg);
92
93 bool valid() const { return bool(fd); }
94};
95
96}
Definition file-descriptor.hh:51
Definition pathlocks.hh:36
Definition error.hh:281
This file defines two main structs/classes used in nix error handling.
Definition types.hh:172
std::string Path
Definition types.hh:28