fix thread unsafety in occasional logging when using std::atomic
This PR is to address https://github.com/google/glog/issues/804
Note:
- Declare
LOG_OCCURRENCES_MOD_Nas a local, non-atomic variable and usefetch_add % nto get the exact modulo remainder. - Initialize
SOME_KIND_OF_LOG_EVERY_Nfrom0to1sincefetch_addwill get the previous value. - Add an extra condition when
n == 1. - Introduce a new variable
LOG_OCCURRENCES_MOD_Nin the macroSOME_KIND_OF_LOG_FIRST_N. Although the name seems unrelated, I prefer not to introduce another macro to name it. - Remove the usage
AnnotateBenignRaceSizedfor variables unlikely to be under a multi-thread situation.
I only fix the atomic version and I have no idea if other versions have a similar problem. Would you mind taking a look? @sergiud @drigz Thanks.
Aside question:
I doubt if AnnotateBenignRaceSized will take effect on std::atomic since no data race will happen.
Since a fix is not straightforward to get right, I suggest that you add unit tests.
I make some updates. Please take a look if you have time. The tests I added are most likely to fail on the master branch.
I'm very sorry for the delay. I will look at the PR right after 0.6 release.
I'm very sorry for the delay. I will look at the PR right after 0.6 release.
No problem.
I'll close the PR since it's heavily diverged from master. @Nimrod0901 if you want continue working on this please rebase.