X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FExceptionWrapper.h;h=0d028714231fa5ad6fde57055c41b6734b13356f;hp=953ce4d479b1ec0127e9d2927b7d8226cd79daa6;hb=48d3f5e1691033d00ecc2ef0c3ced83ce148705a;hpb=9d4d4b000cc218ee0c9c430257222dd63ad766ff diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index 953ce4d4..0d028714 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * 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. @@ -13,349 +13,686 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* + * Author: Eric Niebler + */ -#ifndef FOLLY_EXCEPTIONWRAPPER_H -#define FOLLY_EXCEPTIONWRAPPER_H +#pragma once #include +#include #include +#include #include -#include -#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wpotentially-evaluated-expression" +// GCC gets confused about lambda scopes and issues shadow-local warnings for +// parameters in totally different functions. +FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS +#endif + +#define FOLLY_EXCEPTION_WRAPPER_H_INCLUDED namespace folly { -/* - * Throwing exceptions can be a convenient way to handle errors. Storing - * exceptions in an exception_ptr makes it easy to handle exceptions in a - * different thread or at a later time. exception_ptr can also be used in a very - * generic result/exception wrapper. - * - * However, there are some issues with throwing exceptions and - * std::exception_ptr. These issues revolve around throw being expensive, - * particularly in a multithreaded environment (see - * ExceptionWrapperBenchmark.cpp). - * - * Imagine we have a library that has an API which returns a result/exception - * wrapper. Let's consider some approaches for implementing this wrapper. - * First, we could store a std::exception. This approach loses the derived - * exception type, which can make exception handling more difficult for users - * that prefer rethrowing the exception. We could use a folly::dynamic for every - * possible type of exception. This is not very flexible - adding new types of - * exceptions requires a change to the result/exception wrapper. We could use an - * exception_ptr. However, constructing an exception_ptr as well as accessing - * the error requires a call to throw. That means that there will be two calls - * to throw in order to process the exception. For performance sensitive - * applications, this may be unacceptable. - * - * exception_wrapper is designed to handle exception management for both - * convenience and high performance use cases. make_exception_wrapper is - * templated on derived type, allowing us to rethrow the exception properly for - * users that prefer convenience. These explicitly named exception types can - * therefore be handled without any peformance penalty. exception_wrapper is - * also flexible enough to accept any type. If a caught exception is not of an - * explicitly named type, then std::exception_ptr is used to preserve the - * exception state. For performance sensitive applications, the accessor methods - * can test or extract a pointer to a specific exception type with very little - * overhead. - * - * Example usage: - * - * exception_wrapper globalExceptionWrapper; - * - * // Thread1 - * void doSomethingCrazy() { - * int rc = doSomethingCrazyWithLameReturnCodes(); - * if (rc == NAILED_IT) { - * globalExceptionWrapper = exception_wrapper(); - * } else if (rc == FACE_PLANT) { - * globalExceptionWrapper = make_exception_wrapper(); - * } else if (rc == FAIL_WHALE) { - * globalExceptionWrapper = make_exception_wrapper(); - * } - * } - * - * // Thread2: Exceptions are ok! - * void processResult() { - * try { - * globalExceptionWrapper.throwException(); - * } catch (const FacePlantException& e) { - * LOG(ERROR) << "FACEPLANT!"; - * } catch (const FailWhaleException& e) { - * LOG(ERROR) << "FAILWHALE!"; - * } - * } - * - * // Thread2: Exceptions are bad! - * void processResult() { - * auto ep = globalExceptionWrapper.get(); - * if (!ep.with_exception([&]( - * FacePlantException& faceplant) { - * LOG(ERROR) << "FACEPLANT"; - * })) { - * ep.with_exception([&]( - * FailWhaleException& failwhale) { - * LOG(ERROR) << "FAILWHALE!"; - * }); - * } - * } - * - */ -class exception_wrapper { - public: - exception_wrapper() : throwfn_(nullptr) { } - - void throwException() const { - if (throwfn_) { - throwfn_(item_.get()); - } else if (eptr_) { - std::rethrow_exception(eptr_); - } - } +#define FOLLY_REQUIRES_DEF(...) \ + _t(__VA_ARGS__), long>> - explicit operator bool() const { - return item_ || eptr_; - } +#define FOLLY_REQUIRES(...) FOLLY_REQUIRES_DEF(__VA_ARGS__) = __LINE__ - // This implementation is similar to std::exception_ptr's implementation - // where two exception_wrappers are equal when the address in the underlying - // reference field both point to the same exception object. The reference - // field remains the same when the exception_wrapper is copied or when - // the exception_wrapper is "rethrown". - bool operator==(const exception_wrapper& a) const { - if (item_) { - return a.item_ && item_.get() == a.item_.get(); - } else { - return eptr_ == a.eptr_; - } - } +namespace exception_wrapper_detail { - bool operator!=(const exception_wrapper& a) const { - return !(*this == a); - } +template