Add support for TFO connections
[folly.git] / folly / detail / UncaughtExceptionCounter.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <exception>
20
21 #if defined(__GNUG__) || defined(__CLANG__)
22 #define FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS
23 namespace __cxxabiv1 {
24 // forward declaration (originally defined in unwind-cxx.h from from libstdc++)
25 struct __cxa_eh_globals;
26 // declared in cxxabi.h from libstdc++-v3
27 extern "C" __cxa_eh_globals* __cxa_get_globals() noexcept;
28 }
29 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && \
30     (_MSC_VER < 1900) // MSVC++ 8.0 or greater
31 #define FOLLY_EXCEPTION_COUNT_USE_GETPTD
32 // forward declaration (originally defined in mtdll.h from MSVCRT)
33 struct _tiddata;
34 extern "C" _tiddata* _getptd(); // declared in mtdll.h from MSVCRT
35 #elif defined(_MSC_VER) && (_MSC_VER >= 1900) // MSVC++ 2015
36 #define FOLLY_EXCEPTION_COUNT_USE_STD
37 #else
38 // Raise an error when trying to use this on unsupported platforms.
39 #error "Unsupported platform, don't include this header."
40 #endif
41
42
43 namespace folly { namespace detail {
44
45 /**
46  * Used to check if a new uncaught exception was thrown by monitoring the
47  * number of uncaught exceptions.
48  *
49  * Usage:
50  *  - create a new UncaughtExceptionCounter object
51  *  - call isNewUncaughtException() on the new object to check if a new
52  *    uncaught exception was thrown since the object was created
53  */
54 class UncaughtExceptionCounter {
55  public:
56   UncaughtExceptionCounter() noexcept
57       : exceptionCount_(getUncaughtExceptionCount()) {}
58
59   UncaughtExceptionCounter(const UncaughtExceptionCounter& other) noexcept
60       : exceptionCount_(other.exceptionCount_) {}
61
62   bool isNewUncaughtException() noexcept {
63     return getUncaughtExceptionCount() > exceptionCount_;
64   }
65
66  private:
67   int getUncaughtExceptionCount() noexcept;
68
69   int exceptionCount_;
70 };
71
72 /**
73  * Returns the number of uncaught exceptions.
74  *
75  * This function is based on Evgeny Panasyuk's implementation from here:
76  * http://fburl.com/15190026
77  */
78 inline int UncaughtExceptionCounter::getUncaughtExceptionCount() noexcept {
79 #if defined(FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS)
80   // __cxa_get_globals returns a __cxa_eh_globals* (defined in unwind-cxx.h).
81   // The offset below returns __cxa_eh_globals::uncaughtExceptions.
82   return *(reinterpret_cast<unsigned int*>(static_cast<char*>(
83       static_cast<void*>(__cxxabiv1::__cxa_get_globals())) + sizeof(void*)));
84 #elif defined(FOLLY_EXCEPTION_COUNT_USE_GETPTD)
85   // _getptd() returns a _tiddata* (defined in mtdll.h).
86   // The offset below returns _tiddata::_ProcessingThrow.
87   return *(reinterpret_cast<int*>(static_cast<char*>(
88       static_cast<void*>(_getptd())) + sizeof(void*) * 28 + 0x4 * 8));
89 #elif defined(FOLLY_EXCEPTION_COUNT_USE_STD)
90   return std::uncaught_exceptions();
91 #endif
92 }
93
94 }} // namespaces