Fix a data race in Comms

PiperOrigin-RevId: 374397564
Change-Id: I630a7587242b7b25364aa66158d86d53aff5c343
This commit is contained in:
Wiktor Garbacz 2021-05-18 05:48:30 -07:00 committed by Copybara-Service
parent a986278550
commit 78d749380b

View File

@ -27,6 +27,7 @@
#include <syscall.h> #include <syscall.h>
#include <unistd.h> #include <unistd.h>
#include <atomic>
#include <cerrno> #include <cerrno>
#include <cinttypes> #include <cinttypes>
#include <cstddef> #include <cstddef>
@ -218,9 +219,8 @@ bool Comms::SendTLV(uint32_t tag, size_t length, const void* value) {
} }
if (length > kWarnMsgSize) { if (length > kWarnMsgSize) {
// TODO(cblichmann): Use LOG_FIRST_N once Abseil logging is released. // TODO(cblichmann): Use LOG_FIRST_N once Abseil logging is released.
static int times_warned = 0; static std::atomic<int> times_warned = 0;
if (times_warned < 10) { if (times_warned.fetch_add(1, std::memory_order_relaxed) < 10) {
++times_warned;
SAPI_RAW_LOG( SAPI_RAW_LOG(
WARNING, WARNING,
"TLV message of size %zu detected. Please consider switching " "TLV message of size %zu detected. Please consider switching "
@ -566,12 +566,11 @@ bool Comms::RecvTL(uint32_t* tag, size_t* length) {
return false; return false;
} }
if (*length > kWarnMsgSize) { if (*length > kWarnMsgSize) {
static int times_warned = 0; static std::atomic<int> times_warned = 0;
if (times_warned < 10) { if (times_warned.fetch_add(1, std::memory_order_relaxed) < 10) {
++times_warned;
SAPI_RAW_LOG( SAPI_RAW_LOG(
WARNING, WARNING,
"TLV message of size: (%zu detected. Please consider switching to " "TLV message of size: %zu detected. Please consider switching to "
"Buffer API instead.", "Buffer API instead.",
*length); *length);
} }