/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#pragma once
+#include <folly/Function.h>
#include <folly/futures/Future.h>
#include <folly/futures/Promise.h>
+#include <exception>
+#include <memory>
+#include <stdexcept>
+#include <vector>
namespace folly {
namespace fibers {
: executor_(executor),
state_(new DispatchState(std::move(dispatchFunc))) {}
- Future<ResultT> add(ValueT&& value) {
+ Future<ResultT> add(ValueT value) {
if (state_->values.empty()) {
executor_.add([state = state_]() { dispatchFunctionWrapper(*state); });
}
state.values.swap(values);
state.promises.swap(promises);
- auto results = state.dispatchFunc(std::move(values));
- if (results.size() != promises.size()) {
- throw std::logic_error(
- "Unexpected number of results returned from dispatch function");
- }
- for (size_t i = 0; i < promises.size(); i++) {
- promises[i].setValue(std::move(results[i]));
+ try {
+ auto results = state.dispatchFunc(std::move(values));
+ if (results.size() != promises.size()) {
+ throw std::logic_error(
+ "Unexpected number of results returned from dispatch function");
+ }
+
+ for (size_t i = 0; i < promises.size(); i++) {
+ promises[i].setValue(std::move(results[i]));
+ }
+ } catch (const std::exception& ex) {
+ for (size_t i = 0; i < promises.size(); i++) {
+ promises[i].setException(
+ exception_wrapper(std::current_exception(), ex));
+ }
+ } catch (...) {
+ for (size_t i = 0; i < promises.size(); i++) {
+ promises[i].setException(exception_wrapper(std::current_exception()));
+ }
}
}
ExecutorT& executor_;
std::shared_ptr<DispatchState> state_;
};
-}
-}
+} // namespace fibers
+} // namespace folly