Summary:
Issue #80 points out several places in glog where TSAN discovers
false positives. One of these places is in the `LOG_EVERY_N` macros.
These macros are implemented by maintaining a static unprotected
integer counter, and TSAN will report data races on these counters.
Here is a minimum example to reproduce the data race:
```
void logging() {
for (int i = 0; i < 300; ++i) {
LOG_EVERY_N(INFO, 2) << "foo";
}
}
int main() {
auto t1 = std::thread(logging);
auto t2 = std::thread(logging);
t1.join();
t2.join();
return 0;
}
```
And here is the TSAN report:
```
WARNING: ThreadSanitizer: data race (pid=776850)
Write of size 4 at 0x558de483f684 by thread T2:
#0 logging()
#1 void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>)
#2 std::_Bind_simple<void (*())()>::operator()()
#3 std:🧵:_Impl<std::_Bind_simple<void (*())()> >::_M_run()
#4 execute_native_thread_routine
Previous write of size 4 at 0x558de483f684 by thread T1:
#0 logging()
#1 void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>)
#2 std::_Bind_simple<void (*())()>::operator()()
#3 std:🧵:_Impl<std::_Bind_simple<void (*())()> >::_M_run()
#4 execute_native_thread_routine
Location is global '<null>' at 0x000000000000 (main+0x00000011c684)
Thread T2 (tid=776857, running) created by main thread at:
#0 pthread_create
#1 __gthread_create
#2 std:🧵:_M_start_thread(std::shared_ptr<std:🧵:_Impl_base>, void (*)())
#3 main
Thread T1 (tid=776856, running) created by main thread at:
#0 pthread_create
#1 __gthread_create
#2 std:🧵:_M_start_thread(std::shared_ptr<std:🧵:_Impl_base>, void (*)())
#3 main
SUMMARY: ThreadSanitizer: data race in logging()
```
To avoid noisy TSAN reports and also avoid adding a performance hit, this
change will mark these counters as benign races so that TSAN will not report
them. This change will only have an effect if we are compiling with TSAN;
there are no changes if we are not building with TSAN.
With this change, the above example no longer reports a data race when built
and run with TSAN.
Glog uses a pre-C++11 compile time assert to verify the validity of
the severity parameter for LOG_EVERY_N. Unfortunately, some compilers
will complain about the usage of LOG_EVERY_N with
"-Wunused-local-typedef" due to the way the compile time assert is
constructed. This makes it impossible to use LOG_EVERY_N with this
warning treated as an error.
The fix simply removes the assert entirely. This is safe to do since
you can't put anything invalid into the severity parameters without
generating a compile error elsewhere. This has been safe to do ever
since the GLOG_ prefixes were added as part of 6febec361e.
Fixes#223
Commit changes to src/windows/glog/logging.h that were missed in
2df0ca34aa. Because a change to src/glog/logging.h.in was made,
src/windows/preprocess.sh needed to be run.
The gflags project updated their CMake config last year with a
`gflags` ALIAS target. This can be used instead of the legacy
`${gflags_LIBRARIES}` and `${gflags_INCLUDE_DIRS}` variables. It also
looks cleaner.
Fixes#198