Skip to content

Commit

Permalink
Remove global assertion lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Nov 14, 2023
1 parent 963cb5c commit ca831ae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
10 changes: 0 additions & 10 deletions include/assert/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,15 +924,6 @@ namespace libassert::detail {
* actual assertion handling, finally
*/

struct lock {
lock();
compl lock();
lock(const lock&) = delete;
lock(lock&&) = delete;
lock& operator=(const lock&) = delete;
lock& operator=(lock&&) = delete;
};

// collection of assertion data that can be put in static storage and all passed by a single pointer
struct assert_static_parameters {
const char* name;
Expand Down Expand Up @@ -1045,7 +1036,6 @@ namespace libassert::detail {
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
Args&&... args
) {
const lock l;
const auto* args_strings = params->args_strings;
const size_t args_strings_count = count_args_strings(args_strings);
const size_t sizeof_extra_diagnostics = sizeof...(args) - 1; // - 1 for pretty function signature
Expand Down
27 changes: 16 additions & 11 deletions src/analysis.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <algorithm>
#include <initializer_list>
#include <memory>
#include <mutex>
#include <regex>
#include <set>
#include <string_view>
Expand Down Expand Up @@ -33,29 +34,30 @@ namespace libassert::detail {
return composite;
}

static const std::regex comma_re(R"(\s*,\s*)");
static const std::regex class_re(R"(\b(class|struct)\s+)");
static const std::pair<std::regex, std::string_view> basic_string = {
std::regex(R"(std(::[a-zA-Z0-9_]+)?::basic_string<char)"), "std::string"
};
static const std::pair<std::regex, std::string_view> basic_string_view = {
std::regex(R"(std(::[a-zA-Z0-9_]+)?::basic_string_view<char)"), "std::string_view"
};
static const std::pair<std::regex, std::string_view> allocator = {
std::regex(R"(,\s*std(::[a-zA-Z0-9_]+)?::allocator<)"), ""
};

LIBASSERT_ATTR_COLD
std::string prettify_type(std::string type) {
// > > -> >> replacement
// could put in analysis:: but the replacement is basic and this is more convenient for
// using in the stringifier too
replace_all_dynamic(type, "> >", ">>");
// "," -> ", " and " ," -> ", "
static const std::regex comma_re(R"(\s*,\s*)");
replace_all(type, comma_re, ", ");
// class C -> C for msvc
static const std::regex class_re(R"(\b(class|struct)\s+)");
replace_all(type, class_re, "");
// rules to replace std::basic_string -> std::string and std::basic_string_view -> std::string_view
static const std::pair<std::regex, std::string_view> basic_string = {
std::regex(R"(std(::[a-zA-Z0-9_]+)?::basic_string<char)"), "std::string"
};
static const std::pair<std::regex, std::string_view> basic_string_view = {
std::regex(R"(std(::[a-zA-Z0-9_]+)?::basic_string_view<char)"), "std::string_view"
};
// rule to replace ", std::allocator<whatever>"
static const std::pair<std::regex, std::string_view> allocator = {
std::regex(R"(,\s*std(::[a-zA-Z0-9_]+)?::allocator<)"), ""
};
replace_all_template(type, basic_string);
replace_all_template(type, basic_string_view);
replace_all_template(type, allocator);
Expand All @@ -82,7 +84,9 @@ namespace libassert::detail {
// Analysis singleton, lazy-initialize all the regex nonsense
// 8 BSS bytes and <512 bytes heap bytes not a problem
static std::unique_ptr<analysis> analysis_singleton;
static std::mutex singleton_mutex;
static analysis& get() {
std::unique_lock lock(singleton_mutex);
if(analysis_singleton == nullptr) {
analysis_singleton = std::unique_ptr<analysis>(new analysis);
}
Expand Down Expand Up @@ -707,6 +711,7 @@ namespace libassert::detail {
};

std::unique_ptr<analysis> analysis::analysis_singleton;
std::mutex analysis::singleton_mutex;

LIBASSERT_ATTR_COLD
std::string highlight(const std::string& expression) {
Expand Down
26 changes: 2 additions & 24 deletions src/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ template<typename N> class small_static_map {
};

namespace libassert::utility {
static const std::regex ansi_escape_re("\033\\[[^m]+m");

LIBASSERT_ATTR_COLD
std::string strip_colors(const std::string& str) {
static const std::regex ansi_escape_re("\033\\[[^m]+m");
return std::regex_replace(str, ansi_escape_re, "");
}

Expand Down Expand Up @@ -925,29 +926,6 @@ namespace libassert::detail {
LIBASSERT_ATTR_COLD extra_diagnostics::~extra_diagnostics() = default;
LIBASSERT_ATTR_COLD extra_diagnostics::extra_diagnostics(extra_diagnostics&&) noexcept = default;

// mingw has threading/std::mutex problems
// TODO: This was for ancient mingw. Re-evaluate if this should still be done.
#if LIBASSERT_IS_GCC && IS_WINDOWS
CRITICAL_SECTION CriticalSection;
[[gnu::constructor]] LIBASSERT_ATTR_COLD static void initialize_critical_section() {
InitializeCriticalSectionAndSpinCount(&CriticalSection, 10);
}
LIBASSERT_ATTR_COLD lock::lock() {
EnterCriticalSection(&CriticalSection);
}
LIBASSERT_ATTR_COLD lock::~lock() {
EnterCriticalSection(&CriticalSection);
}
#else
std::mutex global_thread_lock;
LIBASSERT_ATTR_COLD lock::lock() {
global_thread_lock.lock();
}
LIBASSERT_ATTR_COLD lock::~lock() {
global_thread_lock.unlock();
}
#endif

LIBASSERT_ATTR_COLD
const char* assert_type_name(assert_type t) {
switch(t) {
Expand Down

0 comments on commit ca831ae

Please sign in to comment.