Commit 2e60986c authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Use throw_exception in folly/futures/

Summary: [Folly] Use `throw_exception` in `folly/futures/`, replacing the one-off throw functions.

Reviewed By: Orvid

Differential Revision: D7965947

fbshipit-source-id: ed5855aaccf4aa07ecb40489db9fa92090df9016
parent 407e4380
...@@ -550,7 +550,6 @@ libfolly_la_SOURCES = \ ...@@ -550,7 +550,6 @@ libfolly_la_SOURCES = \
FingerprintTables.cpp \ FingerprintTables.cpp \
futures/Barrier.cpp \ futures/Barrier.cpp \
futures/Future.cpp \ futures/Future.cpp \
futures/FutureException.cpp \
futures/ThreadWheelTimekeeper.cpp \ futures/ThreadWheelTimekeeper.cpp \
futures/test/TestExecutor.cpp \ futures/test/TestExecutor.cpp \
executors/CPUThreadPoolExecutor.cpp \ executors/CPUThreadPoolExecutor.cpp \
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
#include <folly/futures/Barrier.h> #include <folly/futures/Barrier.h>
#include <folly/lang/Exception.h>
namespace folly { namespace futures { namespace folly { namespace futures {
...@@ -40,7 +41,7 @@ Barrier::~Barrier() { ...@@ -40,7 +41,7 @@ Barrier::~Barrier() {
auto Barrier::allocateControlBlock() -> ControlBlock* { auto Barrier::allocateControlBlock() -> ControlBlock* {
auto block = static_cast<ControlBlock*>(malloc(controlBlockSize(size_))); auto block = static_cast<ControlBlock*>(malloc(controlBlockSize(size_)));
if (!block) { if (!block) {
throw std::bad_alloc(); throw_exception<std::bad_alloc>();
} }
block->valueAndReaderCount = 0; block->valueAndReaderCount = 0;
......
...@@ -235,7 +235,7 @@ void FutureBase<T>::detach() { ...@@ -235,7 +235,7 @@ void FutureBase<T>::detach() {
template <class T> template <class T>
void FutureBase<T>::throwIfInvalid() const { void FutureBase<T>::throwIfInvalid() const {
if (!core_) { if (!core_) {
throwNoState(); throw_exception<NoState>();
} }
} }
...@@ -721,7 +721,7 @@ SemiFuture<T>& SemiFuture<T>::operator=(Future<T>&& other) noexcept { ...@@ -721,7 +721,7 @@ SemiFuture<T>& SemiFuture<T>::operator=(Future<T>&& other) noexcept {
template <class T> template <class T>
Future<T> SemiFuture<T>::via(Executor* executor, int8_t priority) && { Future<T> SemiFuture<T>::via(Executor* executor, int8_t priority) && {
if (!executor) { if (!executor) {
throwNoExecutor(); throw_exception<NoExecutor>();
} }
if (auto deferredExecutor = getDeferredExecutor()) { if (auto deferredExecutor = getDeferredExecutor()) {
...@@ -1865,7 +1865,7 @@ Try<T> SemiFuture<T>::getTry(Duration dur) && { ...@@ -1865,7 +1865,7 @@ Try<T> SemiFuture<T>::getTry(Duration dur) && {
this->core_ = nullptr; this->core_ = nullptr;
if (!future.isReady()) { if (!future.isReady()) {
throwTimedOut(); throw_exception<TimedOut>();
} }
return std::move(std::move(future).getTry()); return std::move(std::move(future).getTry());
} }
...@@ -1927,7 +1927,7 @@ template <class T> ...@@ -1927,7 +1927,7 @@ template <class T>
T Future<T>::get(Duration dur) { T Future<T>::get(Duration dur) {
wait(dur); wait(dur);
if (!this->isReady()) { if (!this->isReady()) {
throwTimedOut(); throw_exception<TimedOut>();
} }
return std::move(this->value()); return std::move(this->value());
} }
...@@ -1946,7 +1946,7 @@ template <class T> ...@@ -1946,7 +1946,7 @@ template <class T>
T Future<T>::getVia(TimedDrivableExecutor* e, Duration dur) { T Future<T>::getVia(TimedDrivableExecutor* e, Duration dur) {
waitVia(e, dur); waitVia(e, dur);
if (!this->isReady()) { if (!this->isReady()) {
throwTimedOut(); throw_exception<TimedOut>();
} }
return std::move(value()); return std::move(value());
} }
...@@ -1960,7 +1960,7 @@ template <class T> ...@@ -1960,7 +1960,7 @@ template <class T>
Try<T>& Future<T>::getTryVia(TimedDrivableExecutor* e, Duration dur) { Try<T>& Future<T>::getTryVia(TimedDrivableExecutor* e, Duration dur) {
waitVia(e, dur); waitVia(e, dur);
if (!this->isReady()) { if (!this->isReady()) {
throwTimedOut(); throw_exception<TimedOut>();
} }
return result(); return result();
} }
...@@ -1994,7 +1994,7 @@ Future<T> Future<T>::filter(F&& predicate) { ...@@ -1994,7 +1994,7 @@ Future<T> Future<T>::filter(F&& predicate) {
return this->then([p = std::forward<F>(predicate)](T val) { return this->then([p = std::forward<F>(predicate)](T val) {
T const& valConstRef = val; T const& valConstRef = val;
if (!p(valConstRef)) { if (!p(valConstRef)) {
throwPredicateDoesNotObtain(); throw_exception<PredicateDoesNotObtain>();
} }
return val; return val;
}); });
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <folly/executors/TimedDrivableExecutor.h> #include <folly/executors/TimedDrivableExecutor.h>
#include <folly/futures/Promise.h> #include <folly/futures/Promise.h>
#include <folly/futures/detail/Types.h> #include <folly/futures/detail/Types.h>
#include <folly/lang/Exception.h>
// boring predeclarations and details // boring predeclarations and details
#include <folly/futures/Future-pre.h> #include <folly/futures/Future-pre.h>
...@@ -183,7 +184,7 @@ class FutureBase { ...@@ -183,7 +184,7 @@ class FutureBase {
template <typename Self> template <typename Self>
static decltype(auto) getCoreImpl(Self& self) { static decltype(auto) getCoreImpl(Self& self) {
if (!self.core_) { if (!self.core_) {
throwNoState(); throw_exception<NoState>();
} }
return *self.core_; return *self.core_;
} }
...@@ -199,7 +200,7 @@ class FutureBase { ...@@ -199,7 +200,7 @@ class FutureBase {
static decltype(auto) getCoreTryChecked(Self& self) { static decltype(auto) getCoreTryChecked(Self& self) {
auto& core = self.getCore(); auto& core = self.getCore();
if (!core.hasResult()) { if (!core.hasResult()) {
throwFutureNotReady(); throw_exception<FutureNotReady>();
} }
return core.getTry(); return core.getTry();
} }
......
/*
* Copyright 2017-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/futures/FutureException.h>
namespace folly {
[[noreturn]] void throwNoState() {
throw NoState();
}
[[noreturn]] void throwPromiseAlreadySatisfied() {
throw PromiseAlreadySatisfied();
}
[[noreturn]] void throwFutureNotReady() {
throw FutureNotReady();
}
[[noreturn]] void throwFutureAlreadyRetrieved() {
throw FutureAlreadyRetrieved();
}
[[noreturn]] void throwTimedOut() {
throw TimedOut();
}
[[noreturn]] void throwPredicateDoesNotObtain() {
throw PredicateDoesNotObtain();
}
[[noreturn]] void throwNoFutureInSplitter() { throw NoFutureInSplitter(); }
[[noreturn]] void throwNoExecutor() {
throw NoExecutor();
}
} // namespace folly
...@@ -41,29 +41,21 @@ class FOLLY_EXPORT NoState : public FutureException { ...@@ -41,29 +41,21 @@ class FOLLY_EXPORT NoState : public FutureException {
NoState() : FutureException("No state") {} NoState() : FutureException("No state") {}
}; };
[[noreturn]] void throwNoState();
class FOLLY_EXPORT PromiseAlreadySatisfied : public FutureException { class FOLLY_EXPORT PromiseAlreadySatisfied : public FutureException {
public: public:
PromiseAlreadySatisfied() : FutureException("Promise already satisfied") {} PromiseAlreadySatisfied() : FutureException("Promise already satisfied") {}
}; };
[[noreturn]] void throwPromiseAlreadySatisfied();
class FOLLY_EXPORT FutureNotReady : public FutureException { class FOLLY_EXPORT FutureNotReady : public FutureException {
public: public:
FutureNotReady() : FutureException("Future not ready") {} FutureNotReady() : FutureException("Future not ready") {}
}; };
[[noreturn]] void throwFutureNotReady();
class FOLLY_EXPORT FutureAlreadyRetrieved : public FutureException { class FOLLY_EXPORT FutureAlreadyRetrieved : public FutureException {
public: public:
FutureAlreadyRetrieved() : FutureException("Future already retrieved") {} FutureAlreadyRetrieved() : FutureException("Future already retrieved") {}
}; };
[[noreturn]] void throwFutureAlreadyRetrieved();
class FOLLY_EXPORT FutureCancellation : public FutureException { class FOLLY_EXPORT FutureCancellation : public FutureException {
public: public:
FutureCancellation() : FutureException("Future was cancelled") {} FutureCancellation() : FutureException("Future was cancelled") {}
...@@ -74,31 +66,24 @@ class FOLLY_EXPORT TimedOut : public FutureException { ...@@ -74,31 +66,24 @@ class FOLLY_EXPORT TimedOut : public FutureException {
TimedOut() : FutureException("Timed out") {} TimedOut() : FutureException("Timed out") {}
}; };
[[noreturn]] void throwTimedOut();
class FOLLY_EXPORT PredicateDoesNotObtain : public FutureException { class FOLLY_EXPORT PredicateDoesNotObtain : public FutureException {
public: public:
PredicateDoesNotObtain() : FutureException("Predicate does not obtain") {} PredicateDoesNotObtain() : FutureException("Predicate does not obtain") {}
}; };
[[noreturn]] void throwPredicateDoesNotObtain();
class FOLLY_EXPORT NoFutureInSplitter : public FutureException { class FOLLY_EXPORT NoFutureInSplitter : public FutureException {
public: public:
NoFutureInSplitter() : FutureException("No Future in this FutureSplitter") {} NoFutureInSplitter() : FutureException("No Future in this FutureSplitter") {}
}; };
[[noreturn]] void throwNoFutureInSplitter();
class FOLLY_EXPORT NoTimekeeper : public FutureException { class FOLLY_EXPORT NoTimekeeper : public FutureException {
public: public:
NoTimekeeper() : FutureException("No timekeeper available") {} NoTimekeeper() : FutureException("No timekeeper available") {}
}; };
[[noreturn]] void throwNoExecutor();
class FOLLY_EXPORT NoExecutor : public FutureException { class FOLLY_EXPORT NoExecutor : public FutureException {
public: public:
NoExecutor() : FutureException("No executor provided to via") {} NoExecutor() : FutureException("No executor provided to via") {}
}; };
} // namespace folly } // namespace folly
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <folly/futures/Future.h> #include <folly/futures/Future.h>
#include <folly/futures/SharedPromise.h> #include <folly/futures/SharedPromise.h>
#include <folly/lang/Exception.h>
namespace folly { namespace folly {
...@@ -54,7 +55,7 @@ class FutureSplitter { ...@@ -54,7 +55,7 @@ class FutureSplitter {
*/ */
Future<T> getFuture() { Future<T> getFuture() {
if (promise_ == nullptr) { if (promise_ == nullptr) {
throwNoFutureInSplitter(); throw_exception<NoFutureInSplitter>();
} }
return promise_->getSemiFuture().via(e_); return promise_->getSemiFuture().via(e_);
} }
...@@ -64,7 +65,7 @@ class FutureSplitter { ...@@ -64,7 +65,7 @@ class FutureSplitter {
*/ */
SemiFuture<T> getSemiFuture() { SemiFuture<T> getSemiFuture() {
if (promise_ == nullptr) { if (promise_ == nullptr) {
throwNoFutureInSplitter(); throw_exception<NoFutureInSplitter>();
} }
return promise_->getSemiFuture(); return promise_->getSemiFuture();
} }
......
...@@ -61,7 +61,7 @@ Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept { ...@@ -61,7 +61,7 @@ Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
template <class T> template <class T>
void Promise<T>::throwIfFulfilled() const { void Promise<T>::throwIfFulfilled() const {
if (getCore().hasResult()) { if (getCore().hasResult()) {
throwPromiseAlreadySatisfied(); throw_exception<PromiseAlreadySatisfied>();
} }
} }
...@@ -88,7 +88,7 @@ void Promise<T>::detach() { ...@@ -88,7 +88,7 @@ void Promise<T>::detach() {
template <class T> template <class T>
SemiFuture<T> Promise<T>::getSemiFuture() { SemiFuture<T> Promise<T>::getSemiFuture() {
if (retrieved_) { if (retrieved_) {
throwFutureAlreadyRetrieved(); throw_exception<FutureAlreadyRetrieved>();
} }
retrieved_ = true; retrieved_ = true;
return SemiFuture<T>(&getCore()); return SemiFuture<T>(&getCore());
......
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
#pragma once #pragma once
#include <functional>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Try.h> #include <folly/Try.h>
#include <functional>
#include <folly/futures/FutureException.h> #include <folly/futures/FutureException.h>
#include <folly/lang/Exception.h>
namespace folly { namespace folly {
...@@ -153,7 +155,7 @@ class Promise { ...@@ -153,7 +155,7 @@ class Promise {
template <typename CoreT> template <typename CoreT>
static CoreT& getCoreImpl(CoreT* core) { static CoreT& getCoreImpl(CoreT* core) {
if (!core) { if (!core) {
throwNoState(); throw_exception<NoState>();
} }
return *core; return *core;
} }
......
...@@ -122,7 +122,7 @@ void SharedPromise<T>::setTry(Try<T>&& t) { ...@@ -122,7 +122,7 @@ void SharedPromise<T>::setTry(Try<T>&& t) {
{ {
std::lock_guard<std::mutex> g(mutex_); std::lock_guard<std::mutex> g(mutex_);
if (hasValue_) { if (hasValue_) {
throwPromiseAlreadySatisfied(); throw_exception<PromiseAlreadySatisfied>();
} }
hasValue_ = true; hasValue_ = true;
try_ = std::move(t); try_ = std::move(t);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/executors/InlineExecutor.h> #include <folly/executors/InlineExecutor.h>
#include <folly/futures/Promise.h> #include <folly/futures/Promise.h>
#include <folly/lang/Exception.h>
namespace folly { namespace folly {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment