File tree Expand file tree Collapse file tree 3 files changed +19
-8
lines changed
lib/Futures/include/Futures Expand file tree Collapse file tree 3 files changed +19
-8
lines changed Original file line number Diff line number Diff line change 1
1
v3.11.14 (XXXX-XX-XX)
2
2
---------------------
3
3
4
+ * Optimise wait and detach in futures
5
+
4
6
* Updated ArangoDB Starter to v0.18.15.
5
7
6
8
* Updated arangosync to v2.19.15.
Original file line number Diff line number Diff line change @@ -247,14 +247,15 @@ AqlValue ModificationExecutorHelpers::getDocumentOrNull(
247
247
// while to avoid delays:
248
248
void ModificationExecutorHelpers::waitAndDetach (
249
249
futures::Future<OperationResult>& future) {
250
+
250
251
if (!future.isReady ()) {
251
252
{
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);
258
259
}
259
260
if (!future.isReady ()) {
260
261
auto const detachTime = std::chrono::milliseconds (
Original file line number Diff line number Diff line change 27
27
#include < condition_variable>
28
28
#include < mutex>
29
29
#include < thread>
30
+ #include < functional>
30
31
31
32
#include " Futures/Exceptions.h"
32
33
#include " Futures/Promise.h"
@@ -132,7 +133,7 @@ struct EmptyConstructor {};
132
133
133
134
// uses a condition_variable to wait
134
135
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 ) {
136
137
if (f.isReady ()) {
137
138
return ; // short-circuit
138
139
}
@@ -153,7 +154,13 @@ void waitImpl(Future<T>& f) {
153
154
cv.notify_one ();
154
155
});
155
156
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
+
157
164
f = std::move (ret);
158
165
}
159
166
@@ -270,6 +277,7 @@ class Future {
270
277
Try<T> const && result() const && { return std::move (getStateTryChecked ()); }
271
278
272
279
// / Blocks until this Future is complete.
280
+ void wait (std::function<std::chrono::steady_clock::duration const () > cb) { detail::waitImpl (*this , cb); }
273
281
void wait () { detail::waitImpl (*this ); }
274
282
275
283
// / When this Future has completed, execute func which is a function that
You can’t perform that action at this time.
0 commit comments