Skip to content

Commit

Permalink
src/glog/logging.h.in: fix race conditions in LOG_EVERY_N
Browse files Browse the repository at this point in the history
Fixes issue #439 by protecting critical sections with mutexes.
  • Loading branch information
aesophor committed Nov 5, 2019
1 parent 0bf00f9 commit db43bf9
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -895,39 +895,60 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \

#define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
#define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
#define LOG_OCCURRENCES_MUTEX LOG_EVERY_N_VARNAME(occurrences_mutex_, __LINE__)
#define SHOULD_LOG_MESSAGE LOG_EVERY_N_VARNAME(should_log_message_, __LINE__)

#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
static Mutex LOG_OCCURRENCES_MUTEX; \
bool SHOULD_LOG_MESSAGE = false; \
LOG_OCCURRENCES_MUTEX.Lock(); \
++LOG_OCCURRENCES; \
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
if (LOG_OCCURRENCES_MOD_N == 1) \
SHOULD_LOG_MESSAGE = LOG_OCCURRENCES_MOD_N == 1; \
LOG_OCCURRENCES_MUTEX.Unlock(); \
if (SHOULD_LOG_MESSAGE) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
static Mutex LOG_OCCURRENCES_MUTEX; \
bool SHOULD_LOG_MESSAGE = false; \
LOG_OCCURRENCES_MUTEX.Lock(); \
++LOG_OCCURRENCES; \
if (condition && \
((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
SHOULD_LOG_MESSAGE = condition && \
((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n)); \
LOG_OCCURRENCES_MUTEX.Unlock(); \
if (SHOULD_LOG_MESSAGE) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
static Mutex LOG_OCCURRENCES_MUTEX; \
bool SHOULD_LOG_MESSAGE = false; \
LOG_OCCURRENCES_MUTEX.Lock(); \
++LOG_OCCURRENCES; \
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
if (LOG_OCCURRENCES_MOD_N == 1) \
SHOULD_LOG_MESSAGE = LOG_OCCURRENCES_MOD_N == 1; \
LOG_OCCURRENCES_MUTEX.Unlock(); \
if (SHOULD_LOG_MESSAGE) \
@ac_google_namespace@::ErrnoLogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0; \
if (LOG_OCCURRENCES <= n) \
++LOG_OCCURRENCES; \
if (LOG_OCCURRENCES <= n) \
static Mutex LOG_OCCURRENCES_MUTEX; \
bool SHOULD_LOG_MESSAGE = false; \
LOG_OCCURRENCES_MUTEX.Lock(); \
if (LOG_OCCURRENCES <= n) ++LOG_OCCURRENCES; \
SHOULD_LOG_MESSAGE = LOG_OCCURRENCES <= n; \
LOG_OCCURRENCES_MUTEX.Unlock(); \
if (SHOULD_LOG_MESSAGE) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()
Expand Down

0 comments on commit db43bf9

Please sign in to comment.