Outline throw statements in folly/futures/
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 29 Jul 2017 19:19:18 +0000 (12:19 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 29 Jul 2017 19:20:50 +0000 (12:20 -0700)
Summary: [Folly] Outline `throw` statements in `folly/futures/`.

Reviewed By: ericniebler

Differential Revision: D5522791

fbshipit-source-id: 545185bc580ea8628075b9ecae46c2f19308e937

folly/Makefile.am
folly/futures/Future-inl.h
folly/futures/FutureException.cpp [new file with mode: 0644]
folly/futures/FutureException.h
folly/futures/FutureSplitter.h
folly/futures/Promise-inl.h
folly/futures/ScheduledExecutor.h
folly/futures/SharedPromise-inl.h
folly/futures/detail/Core.h

index df98a75643d3750f5198295e515b3cf89093d0ae..54788e69e5d4287ea1d899851392b88e72c958bf 100644 (file)
@@ -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 \
index 71ab6fba141681303f8ca6d998525b67bd54b315..ef3048f473ffb7221b81b8a30fa9d06d7da25be6 100644 (file)
@@ -217,7 +217,7 @@ void Future<T>::detach() {
 template <class T>
 void Future<T>::throwIfInvalid() const {
   if (!core_)
-    throw NoState();
+    throwNoState();
 }
 
 template <class T>
@@ -1202,7 +1202,7 @@ T Future<T>::get(Duration dur) {
   if (isReady()) {
     return std::move(value());
   } else {
-    throw TimedOut();
+    throwTimedOut();
   }
 }
 
@@ -1240,7 +1240,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)) {
-      throw PredicateDoesNotObtain();
+      throwPredicateDoesNotObtain();
     }
     return val;
   });
diff --git a/folly/futures/FutureException.cpp b/folly/futures/FutureException.cpp
new file mode 100644 (file)
index 0000000..da7b60b
--- /dev/null
@@ -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 <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();
+}
+}
index 3e30672db3605b68ae03aa3dbbc119ba3f181167..2ee69c08bd388fec01ab10d6b44efced52f82c48 100644 (file)
@@ -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();
 }
index b189d44aadd87cef8db11ea6cc12d9f547af4a3a..e930555fd01c76bf4b5d3b43fe3b39de9dec596a 100644 (file)
@@ -55,8 +55,8 @@ class FutureSplitter {
    * This can be called an unlimited number of times per FutureSplitter.
    */
   Future<T> getFuture() {
-    if (UNLIKELY(promise_ == nullptr)) {
-      throw NoFutureInSplitter();
+    if (promise_ == nullptr) {
+      throwNoFutureInSplitter();
     }
     return promise_->getFuture();
   }
index 580b044dafe91518f89907312ff1266d5d49214a..3c6ff17345965552b303bb943638fe0c046e0f4e 100644 (file)
@@ -49,18 +49,18 @@ Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
 
 template <class T>
 void Promise<T>::throwIfFulfilled() {
-  if (UNLIKELY(!core_)) {
-    throw NoState();
+  if (!core_) {
+    throwNoState();
   }
-  if (UNLIKELY(core_->ready())) {
-    throw PromiseAlreadySatisfied();
+  if (core_->ready()) {
+    throwPromiseAlreadySatisfied();
   }
 }
 
 template <class T>
 void Promise<T>::throwIfRetrieved() {
-  if (UNLIKELY(retrieved_)) {
-    throw FutureAlreadyRetrieved();
+  if (retrieved_) {
+    throwFutureAlreadyRetrieved();
   }
 }
 
index 99cb2b73caf56a29e6c8110c3c853b7ec781c2aa..ac77de9d3c6a9015913b03854803bdda5edf05a2 100644 (file)
 
 #pragma once
 
-#include <folly/Executor.h>
 #include <chrono>
 #include <memory>
 #include <stdexcept>
 
+#include <folly/Executor.h>
+#include <folly/portability/BitsFunctexcept.h>
+
 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.
index 9def3bc68658820600853a9380f604b9000a8cf1..c7b6e5adf2852159e6fcf49bb125ce4e020c6166 100644 (file)
@@ -117,7 +117,7 @@ void SharedPromise<T>::setTry(Try<T>&& t) {
   {
     std::lock_guard<std::mutex> g(mutex_);
     if (hasValue_) {
-      throw PromiseAlreadySatisfied();
+      throwPromiseAlreadySatisfied();
     }
     hasValue_ = true;
     try_ = std::move(t);
index f88da8e5a245a383d4b4e69a52f5a0747b27779e..70b1685e62cc345e07b83ce965ba4dc4165e08df 100644 (file)
@@ -31,6 +31,7 @@
 #include <folly/Utility.h>
 #include <folly/futures/FutureException.h>
 #include <folly/futures/detail/FSM.h>
+#include <folly/portability/BitsFunctexcept.h>
 
 #include <folly/io/async/Request.h>
 
@@ -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) {