Uses std::atomic to define global variable
authorTaku Kudo <taku@google.com>
Mon, 13 Jun 2022 17:00:43 +0000 (02:00 +0900)
committerKentaro Hayashi <kenhys@xdump.org>
Mon, 21 Nov 2022 13:43:46 +0000 (13:43 +0000)
Signed-off-by: Kentaro Hayashi <kenhys@gmail.com>
Gbp-Pq: Name 0006-Uses-std-atomic-to-define-global-variable.patch

src/common.h
src/util.cc

index 759563497f6ff121b42607ee360702cc348e7674..6ec4c09344f1c74568cec4fda3dc89b7d7ebe0da 100644 (file)
@@ -50,19 +50,6 @@ typedef uint32_t char32;
 typedef uint32_t uint32;
 typedef uint64_t uint64;
 
-static constexpr uint8 kuint8max = ((uint8)0xFF);
-static constexpr uint16 kuint16max = ((uint16)0xFFFF);
-static constexpr uint32 kuint32max = ((uint32)0xFFFFFFFF);
-static constexpr uint64 kuint64max = ((uint64)(0xFFFFFFFFFFFFFFFF));
-static constexpr int8 kint8min = ((int8)~0x7F);
-static constexpr int8 kint8max = ((int8)0x7F);
-static constexpr int16 kint16min = ((int16)~0x7FFF);
-static constexpr int16 kint16max = ((int16)0x7FFF);
-static constexpr int32 kint32min = ((int32)~0x7FFFFFFF);
-static constexpr int32 kint32max = ((int32)0x7FFFFFFF);
-static constexpr int64 kint64min = ((int64)(~0x7FFFFFFFFFFFFFFF));
-static constexpr int64 kint64max = ((int64)(0x7FFFFFFFFFFFFFFF));
-
 static constexpr uint32 kUnicodeError = 0xFFFD;
 
 #if defined(OS_WIN) && defined(UNICODE) && defined(_UNICODE)
index 8da16c42e60806c3f551390d8061364f7cdd234f..f99c73a55b09589ffe808db2d55ab53dd80a9115 100644 (file)
 
 #include "util.h"
 
+#include <atomic>
 #include <iostream>
 
 namespace sentencepiece {
 
 namespace {
 constexpr unsigned int kDefaultSeed = static_cast<unsigned int>(-1);
-static unsigned int g_seed = kDefaultSeed;
-static int g_minloglevel = 0;
+static std::atomic<unsigned int> g_seed = kDefaultSeed;
+static std::atomic<int> g_minloglevel = 0;
 }  // namespace
 
 void SetRandomGeneratorSeed(unsigned int seed) {
-  if (seed != kDefaultSeed) g_seed = seed;
+  if (seed != kDefaultSeed) g_seed.store(seed);
 }
 
 uint32 GetRandomGeneratorSeed() {
-  return g_seed == kDefaultSeed ? std::random_device{}() : g_seed;
+  return g_seed == kDefaultSeed ? std::random_device{}() : g_seed.load();
 }
 
 namespace logging {
-int GetMinLogLevel() { return g_minloglevel; }
-void SetMinLogLevel(int v) { g_minloglevel = v; }
+int GetMinLogLevel() { return g_minloglevel.load(); }
+void SetMinLogLevel(int v) { g_minloglevel.store(v); }
 }  // namespace logging
 
 namespace string_util {