Skip to content

Commit

Permalink
move folly::uncaught_exceptions
Browse files Browse the repository at this point in the history
Summary: It belongs with the other exception utilies. No need to keep it split out.

Reviewed By: Gownta

Differential Revision: D58552548

fbshipit-source-id: 63992b6e5e89b0afc22402b23ad8297fb0c4749c
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jun 14, 2024
1 parent ea731d6 commit 4618cf8
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 326 deletions.
4 changes: 1 addition & 3 deletions folly/lang/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ cpp_library(
srcs = ["UncaughtExceptions.cpp"],
headers = ["UncaughtExceptions.h"],
exported_deps = [
"//folly:cpp_attributes",
"//folly:likely",
"//folly:portability",
":exception",
],
)
35 changes: 35 additions & 0 deletions folly/lang/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@

#include <folly/lang/New.h>

#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)

namespace __cxxabiv1 {

struct __cxa_eh_globals {
void* caught_exceptions_;
unsigned int uncaught_exceptions_;
};

#if defined(__GLIBCXX__)
extern "C" [[gnu::const]] __cxa_eh_globals* __cxa_get_globals() noexcept;
#else
extern "C" __cxa_eh_globals* __cxa_get_globals();
#endif

} // namespace __cxxabiv1

#endif

namespace folly {

namespace detail {

unsigned int* uncaught_exceptions_ptr() noexcept {
assert(kIsGlibcxx || kIsLibcpp);
#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
return &__cxxabiv1::__cxa_get_globals()->uncaught_exceptions_;
#endif
return nullptr;
}

} // namespace detail

} // namespace folly

// Accesses std::type_info and std::exception_ptr internals. Since these vary
// by platform and library, import or copy the structure and function
// signatures from each platform and library.
Expand Down
26 changes: 26 additions & 0 deletions folly/lang/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,32 @@ catch_exception(Try&& t, Catch&& c, CatchA&&... a) noexcept(
#endif
}

namespace detail {

unsigned int* uncaught_exceptions_ptr() noexcept;

} // namespace detail

/// uncaught_exceptions
///
/// An accelerated version of std::uncaught_exceptions.
///
/// mimic: std::uncaught_exceptions, c++17
[[FOLLY_ATTR_GNU_PURE]] FOLLY_EXPORT FOLLY_ALWAYS_INLINE int
uncaught_exceptions() noexcept {
#if defined(__APPLE__)
return std::uncaught_exceptions();
#elif defined(_CPPLIB_VER)
return std::uncaught_exceptions();
#elif defined(__has_feature) && !FOLLY_HAS_FEATURE(cxx_thread_local)
return std::uncaught_exceptions();
#else
thread_local unsigned int* ct;
return to_signed(
FOLLY_LIKELY(!!ct) ? *ct : *(ct = detail::uncaught_exceptions_ptr()));
#endif
}

namespace detail {
#if FOLLY_APPLE_IOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
Expand Down
28 changes: 0 additions & 28 deletions folly/lang/UncaughtExceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,3 @@
*/

#include <folly/lang/UncaughtExceptions.h>

#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)

namespace __cxxabiv1 {
struct __cxa_eh_globals {
void* caught_exceptions_;
unsigned int uncaught_exceptions_;
};
extern "C" __cxa_eh_globals* __cxa_get_globals();
} // namespace __cxxabiv1

#endif

namespace folly {

namespace detail {

unsigned int* uncaught_exceptions_ptr() noexcept {
assert(kIsGlibcxx || kIsLibcpp);
#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
return &__cxxabiv1::__cxa_get_globals()->uncaught_exceptions_;
#endif
return nullptr;
}

} // namespace detail

} // namespace folly
36 changes: 1 addition & 35 deletions folly/lang/UncaughtExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,4 @@

#pragma once

#include <cassert>
#include <exception>

#include <folly/CppAttributes.h>
#include <folly/Likely.h>
#include <folly/Portability.h>

namespace folly {

namespace detail {

unsigned int* uncaught_exceptions_ptr() noexcept;

} // namespace detail

// uncaught_exceptions
//
// An accelerated version of std::uncaught_exceptions.
//
// mimic: std::uncaught_exceptions, c++17
[[FOLLY_ATTR_GNU_PURE]] FOLLY_EXPORT FOLLY_ALWAYS_INLINE int
uncaught_exceptions() noexcept {
#if defined(__APPLE__)
return std::uncaught_exceptions();
#elif defined(_CPPLIB_VER)
return std::uncaught_exceptions();
#elif defined(__has_feature) && !FOLLY_HAS_FEATURE(cxx_thread_local)
return std::uncaught_exceptions();
#else
thread_local unsigned int* ct;
return FOLLY_LIKELY(!!ct) ? *ct : *(ct = detail::uncaught_exceptions_ptr());
#endif
}

} // namespace folly
#include <folly/lang/Exception.h>
24 changes: 0 additions & 24 deletions folly/lang/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -220,27 +220,3 @@ cpp_unittest(
"//folly/portability:gtest",
],
)

cpp_benchmark(
name = "uncaught_exceptions_bench",
srcs = ["UncaughtExceptionsBench.cpp"],
deps = [
"//folly:benchmark",
"//folly/lang:hint",
"//folly/lang:keep",
"//folly/lang:uncaught_exceptions",
],
)

cpp_unittest(
name = "uncaught_exceptions_test",
srcs = ["UncaughtExceptionsTest.cpp"],
headers = [],
supports_static_listing = False,
deps = [
"//folly:conv",
"//folly/functional:invoke",
"//folly/lang:uncaught_exceptions",
"//folly/portability:gtest",
],
)
28 changes: 28 additions & 0 deletions folly/lang/test/ExceptionBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#include <folly/Benchmark.h>
#include <folly/lang/Keep.h>

extern "C" FOLLY_KEEP int check_std_uncaught_exceptions() {
return std::uncaught_exceptions();
}

extern "C" FOLLY_KEEP int check_folly_uncaught_exceptions() {
return folly::uncaught_exceptions();
}

namespace {

template <int I>
Expand Down Expand Up @@ -68,6 +76,26 @@ check_folly_exception_ptr_get_object_hint_vmi( //
return folly::exception_ptr_get_object_hint<A0>(ptr, folly::tag<B1, C, B2>);
}

BENCHMARK(std_uncaught_exceptions, iters) {
int s = 0;
while (iters--) {
int u = std::uncaught_exceptions();
folly::compiler_must_not_predict(u);
s ^= u;
}
folly::compiler_must_not_elide(s);
}

BENCHMARK(folly_uncaught_exceptions, iters) {
int s = 0;
while (iters--) {
int u = folly::uncaught_exceptions();
folly::compiler_must_not_predict(u);
s ^= u;
}
folly::compiler_must_not_elide(s);
}

BENCHMARK(get_object_fail, iters) {
folly::BenchmarkSuspender braces;
bool result = false;
Expand Down
20 changes: 20 additions & 0 deletions folly/lang/test/ExceptionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ TEST_F(ExceptionTest, rethrow_current_exception) {
std::runtime_error);
}

TEST_F(ExceptionTest, uncaught_exception) {
struct dtor {
unsigned expected;
explicit dtor(unsigned e) noexcept : expected{e} {}
~dtor() {
EXPECT_EQ(expected, std::uncaught_exceptions());
EXPECT_EQ(expected, folly::uncaught_exceptions());
}
};
try {
dtor obj{0};
} catch (...) {
}
try {
dtor obj{1};
throw std::exception();
} catch (...) {
}
}

TEST_F(ExceptionTest, exception_ptr_empty) {
auto ptr = std::exception_ptr();
EXPECT_EQ(nullptr, folly::exception_ptr_get_type(ptr));
Expand Down
49 changes: 0 additions & 49 deletions folly/lang/test/UncaughtExceptionsBench.cpp

This file was deleted.

Loading

0 comments on commit 4618cf8

Please sign in to comment.