Repository navigation
Expand file tree
/
Copy pathlocking.cpp
More file actions
242 lines (223 loc) · 8.6 KB
/
Copy pathlocking.cpp
File metadata and controls
242 lines (223 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <teaql/runtime/detail/context/locking.hpp>
#include <charconv>
namespace teaql::detail {
LockInstant LockInstant::now() {
auto elapsed = std::chrono::steady_clock::now().time_since_epoch();
auto seconds = std::chrono::floor<std::chrono::seconds>(elapsed);
return {seconds.count(),
std::uint32_t(
std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed - seconds).count())};
}
LockInstant LockInstant::add_millis(std::uint64_t millis) const {
auto increment = millis / 1000;
auto ns = std::uint64_t(nanos) + (millis % 1000) * 1'000'000;
increment += ns / 1'000'000'000;
auto room = std::uint64_t(std::numeric_limits<std::int64_t>::max()) - std::uint64_t(seconds);
if (increment > room) {
throw std::overflow_error("local lock deadline overflow");
}
return {std::bit_cast<std::int64_t>(std::uint64_t(seconds) + increment),
std::uint32_t(ns % 1'000'000'000)};
}
std::chrono::nanoseconds LockInstant::wait_from(LockInstant current) const {
if (*this <= current) {
return std::chrono::nanoseconds::zero();
}
// Bounded wait slices avoid chrono's native time_point overflow for leases
// hundreds of millions of years long. Every wake rechecks the same deadline.
auto whole = std::uint64_t(seconds) - std::uint64_t(current.seconds);
if (whole > 1) {
return std::chrono::seconds(1);
}
auto ns = std::int64_t(whole * 1'000'000'000) + nanos - current.nanos;
return std::chrono::nanoseconds(std::min<std::int64_t>(ns, 1'000'000'000));
}
bool LocalLocks::acquire(std::string_view key,
std::uint64_t owner,
std::uint64_t timeout,
std::uint64_t expiry) {
resource_arena(resource_).check_thread();
auto deadline = LockInstant::now().add_millis(timeout);
std::unique_lock lock(mutex_);
for (;;) {
auto now = LockInstant::now();
auto current = entries_.find(key);
if (current == entries_.end() || current->second.owner == owner ||
(current->second.expiry && now >= *current->second.expiry)) {
auto expires = expiry ? std::optional(now.add_millis(expiry)) : std::nullopt;
if (current == entries_.end()) {
entries_.emplace(String(key, resource_), Entry{owner, expires});
} else {
current->second = Entry{owner, expires};
}
return true;
}
if (!timeout || now >= deadline) {
return false;
}
auto wake = current->second.expiry ? std::min(deadline, *current->second.expiry) : deadline;
changed_.wait_for(lock, wake.wait_from(now));
}
}
void LocalLocks::release(std::string_view key, std::uint64_t owner) {
resource_arena(resource_).check_thread();
std::lock_guard lock(mutex_);
auto current = entries_.find(key);
if (current != entries_.end() && current->second.owner == owner) {
entries_.erase(current);
changed_.notify_all();
}
}
LocalLocks& ContextLockAccess::local(const UserContext& user) {
user.check_public();
auto& global = user.global_;
if (!global.local_locks_) {
global.local_locks_ = &global.make_resource<LocalLocks>();
}
return *global.local_locks_;
}
std::uint64_t ContextLockAccess::owner(const UserContext& user) {
user.check_public();
return (std::uint64_t(user.global_.runtime_identity_) << 20) | user.identity_;
}
const String& ContextLockAccess::remote_owner(const UserContext& user) {
user.check_public();
if (!user.remote_lock_owner_) {
auto r = user.memory_resource();
String token("teaql:", r);
auto append = [&](auto value) {
char bytes[32];
auto end = std::to_chars(bytes, bytes + sizeof(bytes), value).ptr;
token.append(bytes, end);
};
#if defined(_WIN32)
append(::_getpid());
#else
append(::getpid());
#endif
token += ':';
append(owner(user));
token += ':';
auto created = user.remote_lock_created_at_;
if (created.unix_seconds() > 0) {
append(created.unix_seconds());
char ns[9];
auto rest = created.subsec_nanos();
for (std::size_t i = 9; i != 0; --i) {
ns[i - 1] = '0' + rest % 10;
rest /= 10;
}
token.append(ns, 9);
} else {
append(created.unix_seconds() < 0 ? 0u : created.subsec_nanos());
}
user.remote_lock_owner_ =
&const_cast<ContextArena&>(user.memory_).make_payload<String>(std::move(token));
}
return *user.remote_lock_owner_;
}
std::optional<ResourceBorrow<const RemoteLockProvider>>
ContextLockAccess::remote(const UserContext& user) {
user.check_public();
auto* entry = user.resources_ ? user.resources_->find<RemoteLockProvider>() : nullptr;
if (!entry) {
return {};
}
return ResourceBorrow<const RemoteLockProvider>(
std::allocator_arg,
Allocator(user.memory_resource()),
*static_cast<RemoteLockProvider*>(entry->object),
*entry->owner,
entry->slot);
}
class RemoteLockOperation {
public:
using allocator_type = Allocator;
RemoteLockOperation(std::allocator_arg_t,
Allocator a,
const UserContext& user,
std::string_view key,
std::uint64_t timeout,
std::uint64_t expiry,
bool unlock)
: user_(std::allocator_arg, a, user, resource_arena(a.resource())), key_(key, a.resource()),
timeout_(timeout), expiry_(expiry), unlock_(unlock) {}
std::optional<bool> poll(WakeSignal& wake) {
auto& user = user_.get();
if (!child_) {
auto provider = ContextLockAccess::remote(user);
if (!provider) {
return true;
}
provider_.emplace(std::move(*provider));
const auto& owner = ContextLockAccess::remote_owner(user);
child_ = unlock_ ? &provider_->get().unlock_remote(user.memory_resource(), key_, owner)
: &provider_->get().try_remote_lock(
user.memory_resource(), key_, owner, timeout_, expiry_);
if (child_->memory_resource() != user.memory_resource()) {
child_ = nullptr;
throw std::invalid_argument("Remote lock result must belong to its UserContext");
}
}
(void)provider_->get();
forwarding_.attach(child_->wake_signal(), wake);
auto* ready = child_->poll();
if (!ready) {
return {};
}
return *ready;
}
void finish_and_wait() noexcept {
forwarding_.reset();
}
void cancel_and_wait() noexcept {
forwarding_.reset();
if (child_) {
child_->cancel();
}
}
private:
ResourceBorrow<const UserContext> user_;
String key_;
std::uint64_t timeout_, expiry_;
bool unlock_;
std::optional<ResourceBorrow<const RemoteLockProvider>> provider_;
AsyncOperation<bool>* child_ = nullptr;
WakeSignal::Forward forwarding_;
};
}
namespace teaql {
bool UserContext::try_local_lock(std::string_view key,
std::uint64_t timeout,
std::uint64_t expiry) const {
check_public();
Arena::Quiescence active;
active.attach(detail::resource_arena(memory_resource()), nullptr, [](void*) noexcept {
std::terminate();
});
return detail::ContextLockAccess::local(*this).acquire(
key, detail::ContextLockAccess::owner(*this), timeout, expiry);
}
void UserContext::unlock_local(std::string_view key) const {
check_public();
Arena::Quiescence active;
active.attach(detail::resource_arena(memory_resource()), nullptr, [](void*) noexcept {
std::terminate();
});
if (global_.local_locks_) {
global_.local_locks_->release(key, detail::ContextLockAccess::owner(*this));
}
}
AsyncOperation<bool>& UserContext::try_remote_lock(std::string_view key,
std::uint64_t timeout,
std::uint64_t expiry) const {
check_public();
return make_operation<bool, detail::RemoteLockOperation>(
memory_resource(), *this, key, timeout, expiry, false);
}
AsyncOperation<bool>& UserContext::unlock_remote(std::string_view key) const {
check_public();
return make_operation<bool, detail::RemoteLockOperation>(
memory_resource(), *this, key, 0, 0, true);
}
}