-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make assertions thread-safe #2948
base: devel
Are you sure you want to change the base?
Make assertions thread-safe #2948
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## devel #2948 +/- ##
==========================================
- Coverage 91.01% 90.99% -0.02%
==========================================
Files 198 200 +2
Lines 8599 8626 +27
==========================================
+ Hits 7826 7849 +23
- Misses 773 777 +4 |
…y of Catch2 macros TODO: revert this ugliness when catchorg/Catch2#2948 is merged
The sentence “at some point that might be desirable” is a bit vague. It would be more helpful to either clarify what you mean or remove it. If you're suggesting the possibility of refining the approach in the future (e.g., focusing on static storage), consider expanding on that idea. |
Clarify the environment under which these benchmarks were taken (e.g., specific hardware, OS, and configuration), as this can significantly affect the results. Also, mention if these benchmarks are reflective of real-world usage or more of a worst-case scenario. |
Clarify the impact of this change on larger test suites, as that could be relevant for performance-sensitive environments. |
m_resultCapture.handleUnexpectedExceptionNotThrown( m_assertionInfo, m_reaction ); | ||
} | ||
|
||
void AssertionHandler::handleThrowingCallSkipped() { | ||
auto lock = take_global_lock(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codecov is complaining for this line, please check:
Check warning on line 90 in src/catch2/internal/catch_assertion_handler.cpp
Codecov
/ codecov/patch
src/catch2/internal/catch_assertion_handler.cpp#L90
Added line #L90 was not covered by tests
|
||
#include <mutex> | ||
|
||
std::recursive_mutex global_lock; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain the rationale behind choosing std::recursive_mutex over other types of mutexes.
Hi @shahsb, I am awaiting further feedback from the maintainer who I have been in contact with regarding this PR over discord. I'm a bit confused by your reviews, are you involved with the Catch2 project? |
Description
This PR makes Catch2 assertion and logging macros thread-safe, allowing threads spawned in test cases to perform checks. It does this by introducing a global lock and locking at entry points called from these macros. This was easier and safer than trying to track down every use of static storage in the library and locking only around those, however, at some point that might be desirable.
A
std::recursive_mutex
is used for the global lock. There is some overhead associated with taking out the lock, however, it's extremely minimal in the uncontested (single-thread) case. In a benchmark locally I found that for a debug build on linux the overhead of the lock is around 300ns on my machine, meaning that a user would have to perform a million assertions in order to add a second of run-time overhead to their program.On release the overhead is far smaller:
MSVC debug:
If this impact is deemed too high I have ideas for reducing the overhead in debug mode and optimizing for the uncontested case.
GitHub Issues
#99
#246
#875
#1043
#1169
#1252
#1302
#1714
#1904
#2641
And probably others