From 063b4a3d63f502ea716704321e6a9462686ccdbb Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sat, 29 Jul 2017 12:19:18 -0700 Subject: [PATCH] Outline throw statements in folly/futures/ Summary: [Folly] Outline `throw` statements in `folly/futures/`. Reviewed By: ericniebler Differential Revision: D5522791 fbshipit-source-id: 545185bc580ea8628075b9ecae46c2f19308e937 --- folly/Makefile.am | 1 + folly/futures/Future-inl.h | 6 ++-- folly/futures/FutureException.cpp | 48 +++++++++++++++++++++++++++++++ folly/futures/FutureException.h | 14 +++++++++ folly/futures/FutureSplitter.h | 4 +-- folly/futures/Promise-inl.h | 12 ++++---- folly/futures/ScheduledExecutor.h | 6 ++-- folly/futures/SharedPromise-inl.h | 2 +- folly/futures/detail/Core.h | 7 +++-- 9 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 folly/futures/FutureException.cpp diff --git a/folly/Makefile.am b/folly/Makefile.am index df98a756..54788e69 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -474,6 +474,7 @@ libfolly_la_SOURCES = \ FingerprintTables.cpp \ futures/Barrier.cpp \ futures/Future.cpp \ + futures/FutureException.cpp \ futures/ManualExecutor.cpp \ futures/QueuedImmediateExecutor.cpp \ futures/ThreadWheelTimekeeper.cpp \ diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 71ab6fba..ef3048f4 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -217,7 +217,7 @@ void Future::detach() { template void Future::throwIfInvalid() const { if (!core_) - throw NoState(); + throwNoState(); } template @@ -1202,7 +1202,7 @@ T Future::get(Duration dur) { if (isReady()) { return std::move(value()); } else { - throw TimedOut(); + throwTimedOut(); } } @@ -1240,7 +1240,7 @@ Future Future::filter(F&& predicate) { return this->then([p = std::forward(predicate)](T val) { T const& valConstRef = val; if (!p(valConstRef)) { - throw PredicateDoesNotObtain(); + throwPredicateDoesNotObtain(); } return val; }); diff --git a/folly/futures/FutureException.cpp b/folly/futures/FutureException.cpp new file mode 100644 index 00000000..da7b60b5 --- /dev/null +++ b/folly/futures/FutureException.cpp @@ -0,0 +1,48 @@ +/* + * Copyright 2017 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 + +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(); +} +} diff --git a/folly/futures/FutureException.h b/folly/futures/FutureException.h index 3e30672d..2ee69c08 100644 --- a/folly/futures/FutureException.h +++ b/folly/futures/FutureException.h @@ -39,21 +39,29 @@ class NoState : public FutureException { NoState() : FutureException("No state") {} }; +[[noreturn]] void throwNoState(); + class PromiseAlreadySatisfied : public FutureException { public: PromiseAlreadySatisfied() : FutureException("Promise already satisfied") {} }; +[[noreturn]] void throwPromiseAlreadySatisfied(); + class FutureNotReady : public FutureException { public: FutureNotReady() : FutureException("Future not ready") {} }; +[[noreturn]] void throwFutureNotReady(); + class FutureAlreadyRetrieved : public FutureException { public: FutureAlreadyRetrieved() : FutureException("Future already retrieved") {} }; +[[noreturn]] void throwFutureAlreadyRetrieved(); + class FutureCancellation : public FutureException { public: FutureCancellation() : FutureException("Future was cancelled") {} @@ -64,12 +72,18 @@ class TimedOut : public FutureException { TimedOut() : FutureException("Timed out") {} }; +[[noreturn]] void throwTimedOut(); + class PredicateDoesNotObtain : public FutureException { public: PredicateDoesNotObtain() : FutureException("Predicate does not obtain") {} }; +[[noreturn]] void throwPredicateDoesNotObtain(); + struct NoFutureInSplitter : FutureException { NoFutureInSplitter() : FutureException("No Future in this FutureSplitter") {} }; + +[[noreturn]] void throwNoFutureInSplitter(); } diff --git a/folly/futures/FutureSplitter.h b/folly/futures/FutureSplitter.h index b189d44a..e930555f 100644 --- a/folly/futures/FutureSplitter.h +++ b/folly/futures/FutureSplitter.h @@ -55,8 +55,8 @@ class FutureSplitter { * This can be called an unlimited number of times per FutureSplitter. */ Future getFuture() { - if (UNLIKELY(promise_ == nullptr)) { - throw NoFutureInSplitter(); + if (promise_ == nullptr) { + throwNoFutureInSplitter(); } return promise_->getFuture(); } diff --git a/folly/futures/Promise-inl.h b/folly/futures/Promise-inl.h index 580b044d..3c6ff173 100644 --- a/folly/futures/Promise-inl.h +++ b/folly/futures/Promise-inl.h @@ -49,18 +49,18 @@ Promise& Promise::operator=(Promise&& other) noexcept { template void Promise::throwIfFulfilled() { - if (UNLIKELY(!core_)) { - throw NoState(); + if (!core_) { + throwNoState(); } - if (UNLIKELY(core_->ready())) { - throw PromiseAlreadySatisfied(); + if (core_->ready()) { + throwPromiseAlreadySatisfied(); } } template void Promise::throwIfRetrieved() { - if (UNLIKELY(retrieved_)) { - throw FutureAlreadyRetrieved(); + if (retrieved_) { + throwFutureAlreadyRetrieved(); } } diff --git a/folly/futures/ScheduledExecutor.h b/folly/futures/ScheduledExecutor.h index 99cb2b73..ac77de9d 100644 --- a/folly/futures/ScheduledExecutor.h +++ b/folly/futures/ScheduledExecutor.h @@ -16,11 +16,13 @@ #pragma once -#include #include #include #include +#include +#include + namespace folly { // An executor that supports timed scheduling. Like RxScheduler. class ScheduledExecutor : public virtual Executor { @@ -46,7 +48,7 @@ namespace folly { /// Schedule a Func to be executed at time t, or as soon afterward as /// possible. Expect millisecond resolution at best. Must be threadsafe. virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) { - throw std::logic_error("unimplemented"); + std::__throw_logic_error("unimplemented"); } /// Get this executor's notion of time. Must be threadsafe. diff --git a/folly/futures/SharedPromise-inl.h b/folly/futures/SharedPromise-inl.h index 9def3bc6..c7b6e5ad 100644 --- a/folly/futures/SharedPromise-inl.h +++ b/folly/futures/SharedPromise-inl.h @@ -117,7 +117,7 @@ void SharedPromise::setTry(Try&& t) { { std::lock_guard g(mutex_); if (hasValue_) { - throw PromiseAlreadySatisfied(); + throwPromiseAlreadySatisfied(); } hasValue_ = true; try_ = std::move(t); diff --git a/folly/futures/detail/Core.h b/folly/futures/detail/Core.h index f88da8e5..70b1685e 100644 --- a/folly/futures/detail/Core.h +++ b/folly/futures/detail/Core.h @@ -31,6 +31,7 @@ #include #include #include +#include #include @@ -134,7 +135,7 @@ class Core final { if (ready()) { return *result_; } else { - throw FutureNotReady(); + throwFutureNotReady(); } } @@ -160,7 +161,7 @@ class Core final { case State::OnlyCallback: case State::Armed: case State::Done: - throw std::logic_error("setCallback called twice"); + std::__throw_logic_error("setCallback called twice"); FSM_END // we could always call this, it is an optimization to only call it when @@ -187,7 +188,7 @@ class Core final { case State::OnlyResult: case State::Armed: case State::Done: - throw std::logic_error("setResult called twice"); + std::__throw_logic_error("setResult called twice"); FSM_END if (transitionToArmed) { -- 2.34.1