Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
Loading...
Searching...
No Matches
sync.hh
Go to the documentation of this file.
1
#pragma once
3
4
#include <cstdlib>
5
#include <mutex>
6
#include <shared_mutex>
7
#include <condition_variable>
8
#include <cassert>
9
10
#include "
nix/util/error.hh
"
11
12
namespace
nix {
13
30
template
<
class
T,
class
M,
class
WL,
class
RL,
class
CV>
31
class
SyncBase
32
{
33
private
:
34
M mutex;
35
T data;
36
37
public
:
38
39
using
element_type = T;
40
41
SyncBase() {}
42
43
SyncBase(
const
T & data)
44
: data(data)
45
{
46
}
47
48
SyncBase(T && data) noexcept
49
: data(std::move(data))
50
{
51
}
52
53
template
<
typename
... Ts>
54
SyncBase(Ts &&... args)
55
requires
requires
{ T{std::forward<Ts>(args)...}; }
56
: data(std::forward<Ts>(args)...)
57
{
58
}
59
60
SyncBase(SyncBase && other) noexcept
61
: data(std::move(*other.lock()))
62
{
63
}
64
65
template
<
class
L>
66
class
Lock
67
{
68
protected
:
69
SyncBase * s;
70
L lk;
71
friend
SyncBase;
72
73
Lock(SyncBase * s)
74
: s(s)
75
, lk(s->mutex)
76
{
77
}
78
public
:
79
Lock(Lock && l) =
delete
;
80
Lock(
const
Lock & l) =
delete
;
81
Lock & operator=(Lock && l) =
delete
;
82
Lock & operator=(
const
Lock & l) =
delete
;
83
84
~Lock() {}
85
86
void
wait(CV & cv)
87
{
88
assert(s);
89
cv.wait(lk);
90
}
91
92
template
<
class
Predicate>
93
void
wait(CV & cv, Predicate pred)
94
{
95
assert(s);
96
cv.wait(lk, std::move(pred));
97
}
98
99
template
<
class
Rep,
class
Period>
100
std::cv_status wait_for(CV & cv,
const
std::chrono::duration<Rep, Period> & duration)
101
{
102
assert(s);
103
return
cv.wait_for(lk, duration);
104
}
105
106
template
<
class
Rep,
class
Period,
class
Predicate>
107
bool
wait_for(CV & cv,
const
std::chrono::duration<Rep, Period> & duration, Predicate pred)
108
{
109
assert(s);
110
return
cv.wait_for(lk, duration, pred);
111
}
112
113
template
<
class
Clock,
class
Duration>
114
std::cv_status wait_until(CV & cv,
const
std::chrono::time_point<Clock, Duration> & duration)
115
{
116
assert(s);
117
return
cv.wait_until(lk, duration);
118
}
119
};
120
121
struct
WriteLock
: Lock<WL>
122
{
123
using
Lock<WL>::Lock;
124
125
T * operator->()
126
{
127
return
&WriteLock::s->data;
128
}
129
130
T & operator*()
131
{
132
return
WriteLock::s->data;
133
}
134
};
135
139
WriteLock
lock
()
140
{
141
return
WriteLock(
this
);
142
}
143
144
struct
ReadLock
: Lock<RL>
145
{
146
using
Lock<RL>::Lock;
147
148
const
T * operator->()
149
{
150
return
&ReadLock::s->data;
151
}
152
153
const
T & operator*()
154
{
155
return
ReadLock::s->data;
156
}
157
};
158
163
ReadLock
readLock
()
const
164
{
165
return
ReadLock(
const_cast<
SyncBase *
>
(
this
));
166
}
167
};
168
169
template
<
class
T>
170
using
Sync =
171
SyncBase<T, std::mutex, std::unique_lock<std::mutex>, std::unique_lock<std::mutex>, std::condition_variable>;
172
173
template
<
class
T>
174
using
SharedSync = SyncBase<
175
T,
176
std::shared_mutex,
177
std::unique_lock<std::shared_mutex>,
178
std::shared_lock<std::shared_mutex>,
179
std::condition_variable>;
180
181
}
// namespace nix
nix::SyncBase::readLock
ReadLock readLock() const
Definition
sync.hh:163
nix::SyncBase::lock
WriteLock lock()
Definition
sync.hh:139
error.hh
This file defines two main structs/classes used in nix error handling.
nix::SyncBase::ReadLock
Definition
sync.hh:145
nix::SyncBase::WriteLock
Definition
sync.hh:122
libutil
include
nix
util
sync.hh
Generated by
1.18.0