-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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. |
src/glog/logging.h.in
Outdated
|
|
||
| #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.
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
|
@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:
CheckCXXSourceCompilesto check the availability ofstd::atomic<>std::atomic<int>