Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 4f09bed

Browse files
authored
Merge pull request #263 from kennyyu/tsan_benign_race
Annotate LOG_EVERY_N macros as a benign race for TSAN
2 parents 7bba603 + 4764ca6 commit 4f09bed

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/glog/logging.h.in

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,41 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
905905
#define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
906906
#define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
907907

908+
#if defined(__has_feature)
909+
#define _GLOG_HAS_FEATURE(...) __has_feature(__VA_ARGS__)
910+
#else
911+
#define _GLOG_HAS_FEATURE(...) 0
912+
#endif
913+
914+
#if _GLOG_HAS_FEATURE(thread_sanitizer) || __SANITIZE_THREAD__
915+
#define _GLOG_SANITIZE_THREAD 1
916+
#endif
917+
918+
#if defined(_GLOG_SANITIZE_THREAD)
919+
#define _GLOG_IFDEF_THREAD_SANITIZER(X) X
920+
#else
921+
#define _GLOG_IFDEF_THREAD_SANITIZER(X)
922+
#endif
923+
924+
#if defined(_GLOG_SANITIZE_THREAD)
925+
} // namespace google
926+
927+
// We need to identify the static variables as "benign" races
928+
// to avoid noisy reports from TSAN.
929+
extern "C" void AnnotateBenignRaceSized(
930+
const char *file,
931+
int line,
932+
const volatile void *mem,
933+
long size,
934+
const char *description);
935+
936+
namespace google {
937+
#endif
938+
908939
#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
909940
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
941+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES, sizeof(int), "")); \
942+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES_MOD_N, sizeof(int), "")); \
910943
++LOG_OCCURRENCES; \
911944
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
912945
if (LOG_OCCURRENCES_MOD_N == 1) \
@@ -916,6 +949,8 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
916949

917950
#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
918951
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
952+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES, sizeof(int), "")); \
953+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES_MOD_N, sizeof(int), "")); \
919954
++LOG_OCCURRENCES; \
920955
if (condition && \
921956
((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
@@ -925,6 +960,8 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
925960

926961
#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
927962
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
963+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES, sizeof(int), "")); \
964+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES_MOD_N, sizeof(int), "")); \
928965
++LOG_OCCURRENCES; \
929966
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
930967
if (LOG_OCCURRENCES_MOD_N == 1) \
@@ -934,6 +971,7 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
934971

935972
#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
936973
static int LOG_OCCURRENCES = 0; \
974+
_GLOG_IFDEF_THREAD_SANITIZER(AnnotateBenignRaceSized(__FILE__, __LINE__, &LOG_OCCURRENCES, sizeof(int), "")); \
937975
if (LOG_OCCURRENCES <= n) \
938976
++LOG_OCCURRENCES; \
939977
if (LOG_OCCURRENCES <= n) \

0 commit comments

Comments
 (0)