8000 optimise wait and detach · arangodb/arangodb@178e61e · GitHub
[go: up one dir, main page]

Skip to content

Commit 178e61e

Browse files
committed
optimise wait and detach
1 parent b5f1894 commit 178e61e

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.11.14 (XXXX-XX-XX)
22
---------------------
33

4+
* Optimise wait and detach in futures
5+
46
* Updated ArangoDB Starter to v0.18.15.
57

68
* Updated arangosync to v2.19.15.

arangod/Aql/ModificationExecutorHelpers.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,15 @@ AqlValue ModificationExecutorHelpers::getDocumentOrNull(
247247
// while to avoid delays:
248248
void ModificationExecutorHelpers::waitAndDetach(
249249
futures::Future<OperationResult>& future) {
250+
250251
if (!future.isReady()) {
251252
{
252-
auto const spinTime = std::chrono::milliseconds(10);
253-
auto const start = std::chrono::steady_clock::now();
254-
while (!future.isReady() &&
255-
std::chrono::steady_clock::now() - start < spinTime) {
256-
basics::cpu_relax();
257-
}
253+
using namespace std::literals::chrono_literals;
254+
auto const end = std::chrono::steady_clock::now() + 100ms;
255+
auto const cb = [&]() noexcept {
256+
return end - std::chrono::steady_clock::now();
257+
};
258+
future.wait(cb);
258259
}
259260
if (!future.isReady()) {
260261
auto const detachTime = std::chrono::milliseconds(

lib/Futures/include/Futures/Future.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <condition_variable>
2828
#include <mutex>
2929
#include <thread>
30+
#include <functional>
3031

3132
#include "Futures/Exceptions.h"
3233
#include "Futures/Promise.h"
@@ -132,7 +133,7 @@ struct EmptyConstructor {};
132133

133134
// uses a condition_variable to wait
134135
template<typename T>
135-
void waitImpl(Future<T>& f) {
136+
void waitImpl(Future<T>& f, std::function<std::chrono::steady_clock::duration const ()> cb = nullptr) {
136137
if (f.isReady()) {
137138
return; // short-circuit
138139
}
@@ -153,7 +154,13 @@ void waitImpl(Future<T>& f) {
153154
cv.notify_one();
154155
});
155156
std::unique_lock<std::mutex> lock(m);
156-
cv.wait(lock, [&ret] { return ret.isReady(); });
157+
158+
if (cb != nullptr) {
159+
while (cv.wait_for(lock, cb(), [&ret] { return ret.isReady(); })) {}
160+
} else {
161+
cv.wait(lock, [&ret] { return ret.isReady(); });
162+
}
163+
157164
f = std::move(ret);
158165
}
159166

@@ -270,6 +277,7 @@ class Future {
270277
Try<T> const&& result() const&& { return std::move(getStateTryChecked()); }
271278

272279
/// Blocks until this Future is complete.
280+
void wait(std::function<std::chrono::steady_clock::duration const () > cb) { detail::waitImpl(*this, cb); }
273281
void wait() { detail::waitImpl(*this); }
274282

275283
/// When this Future has completed, execute func which is a function that

0 commit comments

Comments
 (0)
0