/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <vector>
#include <folly/Optional.h>
-#include <folly/SmallLocks.h>
+#include <folly/MicroSpinLock.h>
#include <folly/futures/Try.h>
#include <folly/futures/Promise.h>
/// time there won't be any problems.
template<typename T>
class Core {
+ static_assert(!std::is_void<T>::value,
+ "void futures are not supported. Use Unit instead.");
public:
/// This must be heap-constructed. There's probably a way to enforce that in
/// code but since this is just internal detail code and I don't know how
/// off-hand, I'm punting.
- Core() {}
+ Core() : result_(), fsm_(State::Start), attached_(2) {}
explicit Core(Try<T>&& t)
: result_(std::move(t)),
template <typename F>
class LambdaBufHelper {
public:
- explicit LambdaBufHelper(F&& func) : func_(std::forward<F>(func)) {}
+ template <typename FF>
+ explicit LambdaBufHelper(FF&& func) : func_(std::forward<FF>(func)) {}
void operator()(Try<T>&& t) {
SCOPE_EXIT { this->~LambdaBufHelper(); };
func_(std::move(t));
// Move the lambda into the Core if it fits
if (sizeof(LambdaBufHelper<F>) <= lambdaBufSize) {
- auto funcLoc = static_cast<LambdaBufHelper<F>*>((void*)lambdaBuf_);
+ auto funcLoc = reinterpret_cast<LambdaBufHelper<F>*>(&lambdaBuf_);
new (funcLoc) LambdaBufHelper<F>(std::forward<F>(func));
callback_ = std::ref(*funcLoc);
} else {
// detachPromise() and setResult() should never be called in parallel
// so we don't need to protect this.
if (UNLIKELY(!result_)) {
- setResult(Try<T>(exception_wrapper(BrokenPromise())));
+ setResult(Try<T>(exception_wrapper(BrokenPromise(typeid(T).name()))));
}
detachOne();
}
}
void doCallback() {
- RequestContext::setContext(context_);
-
Executor* x = executor_;
int8_t priority;
if (x) {
if (LIKELY(x->getNumPriorities() == 1)) {
x->add([this]() mutable {
SCOPE_EXIT { detachOne(); };
+ RequestContext::setContext(context_);
+ SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_));
});
} else {
x->addWithPriority([this]() mutable {
SCOPE_EXIT { detachOne(); };
+ RequestContext::setContext(context_);
+ SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_));
}, priority);
}
} catch (...) {
+ --attached_; // Account for extra ++attached_ before try
+ RequestContext::setContext(context_);
result_ = Try<T>(exception_wrapper(std::current_exception()));
+ SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_));
}
} else {
+ RequestContext::setContext(context_);
+ SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_));
}
}
// lambdaBuf occupies exactly one cache line
static constexpr size_t lambdaBufSize = 8 * sizeof(void*);
- char lambdaBuf_[lambdaBufSize];
+ typename std::aligned_storage<lambdaBufSize>::type lambdaBuf_;
// place result_ next to increase the likelihood that the value will be
// contained entirely in one cache line
- folly::Optional<Try<T>> result_ {};
+ folly::Optional<Try<T>> result_;
std::function<void(Try<T>&&)> callback_ {nullptr};
- FSM<State> fsm_ {State::Start};
- std::atomic<unsigned char> attached_ {2};
+ FSM<State> fsm_;
+ std::atomic<unsigned char> attached_;
std::atomic<bool> active_ {true};
std::atomic<bool> interruptHandlerSet_ {false};
folly::MicroSpinLock interruptLock_ {0};
p.setException(std::move(t.exception()));
}
} else if (!threw) {
- std::get<I>(results) = std::move(t.value());
+ std::get<I>(results) = std::move(t);
}
}
~CollectVariadicContext() {
if (!threw.exchange(true)) {
- p.setValue(std::move(results));
+ p.setValue(unwrap(std::move(results)));
}
}
Promise<std::tuple<Ts...>> p;
- std::tuple<Ts...> results;
+ std::tuple<folly::Try<Ts>...> results;
std::atomic<bool> threw {false};
typedef Future<std::tuple<Ts...>> type;
+
+ private:
+ template <typename... Ts2>
+ static std::tuple<Ts...> unwrap(std::tuple<folly::Try<Ts>...>&& o,
+ Ts2&&... ts2) {
+ static_assert(sizeof...(ts2) <
+ std::tuple_size<std::tuple<folly::Try<Ts>...>>::value,
+ "Non-templated unwrap should be used instead");
+ assert(std::get<sizeof...(ts2)>(o).hasValue());
+
+ return unwrap(std::move(o),
+ std::forward<Ts2>(ts2)...,
+ std::move(*std::get<sizeof...(ts2)>(o)));
+ }
+
+ static std::tuple<Ts...> unwrap(std::tuple<folly::Try<Ts>...>&& /* o */,
+ Ts&&... ts) {
+ return std::tuple<Ts...>(std::forward<Ts>(ts)...);
+ }
};
-template <template <typename ...> class T, typename... Ts>
-void collectVariadicHelper(const std::shared_ptr<T<Ts...>>& ctx) {
+template <template <typename...> class T, typename... Ts>
+void collectVariadicHelper(const std::shared_ptr<T<Ts...>>& /* ctx */) {
// base case
}