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 = \
FingerprintTables.cpp \
futures/Barrier.cpp \
futures/Future.cpp \
futures/FutureException.cpp \
futures/ThreadWheelTimekeeper.cpp \
futures/test/TestExecutor.cpp \
executors/CPUThreadPoolExecutor.cpp \
......
......@@ -15,6 +15,7 @@
*/
#include <folly/futures/Barrier.h>
#include <folly/lang/Exception.h>
namespace folly { namespace futures {
......@@ -40,7 +41,7 @@ Barrier::~Barrier() {
auto Barrier::allocateControlBlock() -> ControlBlock* {
auto block = static_cast<ControlBlock*>(malloc(controlBlockSize(size_)));
if (!block) {
throw std::bad_alloc();
throw_exception<std::bad_alloc>();
}
block->valueAndReaderCount = 0;
......
......@@ -235,7 +235,7 @@ void FutureBase<T>::detach() {
template <class T>
void FutureBase<T>::throwIfInvalid() const {
if (!core_) {
throwNoState();
throw_exception<NoState>();
}
}
......@@ -721,7 +721,7 @@ SemiFuture<T>& SemiFuture<T>::operator=(Future<T>&& other) noexcept {
template <class T>
Future<T> SemiFuture<T>::via(Executor* executor, int8_t priority) && {
if (!executor) {
throwNoExecutor();
throw_exception<NoExecutor>();
}
if (auto deferredExecutor = getDeferredExecutor()) {
......@@ -1865,7 +1865,7 @@ Try<T> SemiFuture<T>::getTry(Duration dur) && {
this->core_ = nullptr;
if (!future.isReady()) {
throwTimedOut();
throw_exception<TimedOut>();
}
return std::move(std::move(future).getTry());
}
......@@ -1927,7 +1927,7 @@ template <class T>
T Future<T>::get(Duration dur) {
wait(dur);
if (!this->isReady()) {
throwTimedOut();
throw_exception<TimedOut>();
}
return std::move(this->value());
}
......@@ -1946,7 +1946,7 @@ template <class T>
T Future<T>::getVia(TimedDrivableExecutor* e, Duration dur) {
waitVia(e, dur);
if (!this->isReady()) {
throwTimedOut();
throw_exception<TimedOut>();
}
return std::move(value());
}
......@@ -1960,7 +1960,7 @@ template <class T>
Try<T>& Future<T>::getTryVia(TimedDrivableExecutor* e, Duration dur) {
waitVia(e, dur);
if (!this->isReady()) {
throwTimedOut();
throw_exception<TimedOut>();
}
return result();
}
......@@ -1994,7 +1994,7 @@ Future<T> Future<T>::filter(F&& predicate) {
return this->then([p = std::forward<F>(predicate)](T val) {
T const& valConstRef = val;
if (!p(valConstRef)) {
throwPredicateDoesNotObtain();
throw_exception<PredicateDoesNotObtain>();
}
return val;
});
......
......@@ -35,6 +35,7 @@
#include <folly/executors/TimedDrivableExecutor.h>
#include <folly/futures/Promise.h>
#include <folly/futures/detail/Types.h>
#include <folly/lang/Exception.h>
// boring predeclarations and details
#include <folly/futures/Future-pre.h>
......@@ -183,7 +184,7 @@ class FutureBase {
template <typename Self>
static decltype(auto) getCoreImpl(Self& self) {
if (!self.core_) {
throwNoState();
throw_exception<NoState>();
}
return *self.core_;
}
......@@ -199,7 +200,7 @@ class FutureBase {
static decltype(auto) getCoreTryChecked(Self& self) {
auto& core = self.getCore();
if (!core.hasResult()) {
throwFutureNotReady();
throw_exception<FutureNotReady>();
}
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 {
NoState() : FutureException("No state") {}
};
[[noreturn]] void throwNoState();
class FOLLY_EXPORT PromiseAlreadySatisfied : public FutureException {
public:
PromiseAlreadySatisfied() : FutureException("Promise already satisfied") {}
};
[[noreturn]] void throwPromiseAlreadySatisfied();
class FOLLY_EXPORT FutureNotReady : public FutureException {
public:
FutureNotReady() : FutureException("Future not ready") {}
};
[[noreturn]] void throwFutureNotReady();
class FOLLY_EXPORT FutureAlreadyRetrieved : public FutureException {
public:
FutureAlreadyRetrieved() : FutureException("Future already retrieved") {}
};
[[noreturn]] void throwFutureAlreadyRetrieved();
class FOLLY_EXPORT FutureCancellation : public FutureException {
public:
FutureCancellation() : FutureException("Future was cancelled") {}
......@@ -74,31 +66,24 @@ class FOLLY_EXPORT TimedOut : public FutureException {
TimedOut() : FutureException("Timed out") {}
};
[[noreturn]] void throwTimedOut();
class FOLLY_EXPORT PredicateDoesNotObtain : public FutureException {
public:
PredicateDoesNotObtain() : FutureException("Predicate does not obtain") {}
};
[[noreturn]] void throwPredicateDoesNotObtain();
class FOLLY_EXPORT NoFutureInSplitter : public FutureException {
public:
NoFutureInSplitter() : FutureException("No Future in this FutureSplitter") {}
};
[[noreturn]] void throwNoFutureInSplitter();
class FOLLY_EXPORT NoTimekeeper : public FutureException {
public:
NoTimekeeper() : FutureException("No timekeeper available") {}
};
[[noreturn]] void throwNoExecutor();
class FOLLY_EXPORT NoExecutor : public FutureException {
public:
NoExecutor() : FutureException("No executor provided to via") {}
};
} // namespace folly
......@@ -18,6 +18,7 @@
#include <folly/futures/Future.h>
#include <folly/futures/SharedPromise.h>
#include <folly/lang/Exception.h>
namespace folly {
......@@ -54,7 +55,7 @@ class FutureSplitter {
*/
Future<T> getFuture() {
if (promise_ == nullptr) {
throwNoFutureInSplitter();
throw_exception<NoFutureInSplitter>();
}
return promise_->getSemiFuture().via(e_);
}
......@@ -64,7 +65,7 @@ class FutureSplitter {
*/
SemiFuture<T> getSemiFuture() {
if (promise_ == nullptr) {
throwNoFutureInSplitter();
throw_exception<NoFutureInSplitter>();
}
return promise_->getSemiFuture();
}
......
......@@ -61,7 +61,7 @@ Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
template <class T>
void Promise<T>::throwIfFulfilled() const {
if (getCore().hasResult()) {
throwPromiseAlreadySatisfied();
throw_exception<PromiseAlreadySatisfied>();
}
}
......@@ -88,7 +88,7 @@ void Promise<T>::detach() {
template <class T>
SemiFuture<T> Promise<T>::getSemiFuture() {
if (retrieved_) {
throwFutureAlreadyRetrieved();
throw_exception<FutureAlreadyRetrieved>();
}
retrieved_ = true;
return SemiFuture<T>(&getCore());
......
......@@ -16,10 +16,12 @@
#pragma once
#include <functional>
#include <folly/Portability.h>
#include <folly/Try.h>
#include <functional>
#include <folly/futures/FutureException.h>
#include <folly/lang/Exception.h>
namespace folly {
......@@ -153,7 +155,7 @@ class Promise {
template <typename CoreT>
static CoreT& getCoreImpl(CoreT* core) {
if (!core) {
throwNoState();
throw_exception<NoState>();
}
return *core;
}
......
......@@ -122,7 +122,7 @@ void SharedPromise<T>::setTry(Try<T>&& t) {
{
std::lock_guard<std::mutex> g(mutex_);
if (hasValue_) {
throwPromiseAlreadySatisfied();
throw_exception<PromiseAlreadySatisfied>();
}
hasValue_ = true;
try_ = std::move(t);
......
......@@ -19,6 +19,7 @@
#include <folly/Portability.h>
#include <folly/executors/InlineExecutor.h>
#include <folly/futures/Promise.h>
#include <folly/lang/Exception.h>
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