-
Notifications
You must be signed in to change notification settings - Fork 2.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
Fix race conditions in LOG_EVERY_N #492
Conversation
A mutex is quite heavy. Would thread-local storage possibly suffice? cc @os12 |
There is no need for a mutex. These counters just need to become 👎 |
Glog still uses C++03. There should be atleast a fallback mechanism for pre C++11 compilers. |
db43bf9
to
257fa09
Compare
src/glog/logging.h.in
Outdated
@@ -897,7 +897,7 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \ | |||
#define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__) | |||
|
|||
#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \ | |||
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \ | |||
thread_local int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \ |
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.
This looks wrong to me. Imagine the same LOG_EVERY_XXX()
called from several threads concurrently - every thread will get its own count now...
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.
I've made the counters std::atomic<int>
, and now the only thing left is a fallback mechanism for pre C++11 compilers. Do you have any idea?
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.
Both GCC and MSVC provide atomic intrinsics. Please see https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html and https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedincrement.
257fa09
to
456ef67
Compare
src/glog/logging.h.in
Outdated
// Microsoft Visual Studio 14 (2015) sets __cplusplus==199711 despite | ||
// reasonably good C++11 support, so we set LANG_CXX for it and | ||
// newer versions (_MSC_VER >= 1900). | ||
#define SUPPORT_CPLUSPLUS11 (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ |
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.
std::atomic<>
availability should be checked in CMakeLists.txt
using CheckCXXSymbolExists
or CheckCXXSourceCompiles
module.
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.
Will do!
* Use CMakeLists.txt to check the availability of std::atomic<> * Declare the static integer counters as std::atomic<int> instead of int if C++11's atomic is available
456ef67
to
997fe7f
Compare
@aesophor Could you please resolve the conflict? |
@sergiud I've resolved the conflict and CI has passed successfully. Thanks! |
Fixes issue #439
by protecting critical sections with mutexes.Note:
CheckCXXSourceCompiles
to check the availability ofstd::atomic<>
std::atomic<int>