From 2edc0695834ee90a09db6b86032eeab865302c55 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 24 Jan 2019 06:18:14 +0300 Subject: [PATCH] Explicitly init some std::atomic. GitOrigin-RevId: bfbe95bd1aaed9c3ade31d9a7222b223bfece347 --- benchmark/bench_crypto.cpp | 2 +- benchmark/bench_queue.cpp | 38 +++++++++------------ memprof/memprof.cpp | 3 +- td/telegram/Global.h | 4 +-- tdactor/td/actor/impl/ConcurrentScheduler.h | 2 +- tddb/td/db/binlog/ConcurrentBinlog.h | 2 +- tdutils/td/utils/HazardPointers.h | 2 +- tdutils/td/utils/MemoryLog.h | 2 +- tdutils/td/utils/MpmcQueue.h | 8 ++--- tdutils/td/utils/SharedObjectPool.h | 2 +- tdutils/td/utils/buffer.cpp | 7 ++-- tdutils/td/utils/queue.h | 6 ++-- tdutils/test/HazardPointers.cpp | 2 +- tdutils/test/MpmcWaiter.cpp | 6 ++-- tdutils/test/misc.cpp | 2 +- 15 files changed, 42 insertions(+), 46 deletions(-) diff --git a/benchmark/bench_crypto.cpp b/benchmark/bench_crypto.cpp index 29ceaa1d1..8cc067529 100644 --- a/benchmark/bench_crypto.cpp +++ b/benchmark/bench_crypto.cpp @@ -110,7 +110,7 @@ BENCH(TdRandFast, "td_rand_fast") { #if !TD_THREAD_UNSUPPORTED BENCH(SslRand, "ssl_rand_int32") { std::vector v; - std::atomic sum; + std::atomic sum{0}; for (int i = 0; i < 3; i++) { v.push_back(td::thread([&] { td::int32 res = 0; diff --git a/benchmark/bench_queue.cpp b/benchmark/bench_queue.cpp index b7c6b3490..475ffa412 100644 --- a/benchmark/bench_queue.cpp +++ b/benchmark/bench_queue.cpp @@ -28,12 +28,6 @@ #include #endif -using std::atomic; -using std::vector; - -using td::int32; -using td::uint32; - #define MODE std::memory_order_relaxed // void set_affinity(int mask) { @@ -100,7 +94,7 @@ class Backoff { }; class VarQueue { - atomic data; + std::atomic data{0}; public: void init() { @@ -218,17 +212,17 @@ const int queue_buf_size = 1 << 10; class BufferQueue { struct node { qvalue_t val; - char pad[64 - sizeof(atomic)]; + char pad[64 - sizeof(std::atomic)]; }; node q[queue_buf_size]; struct Position { - atomic i; - char pad[64 - sizeof(atomic)]; + std::atomic i{0}; + char pad[64 - sizeof(std::atomic)]; - uint32 local_read_i; - uint32 local_write_i; - char pad2[64 - sizeof(uint32) * 2]; + td::uint32 local_read_i; + td::uint32 local_write_i; + char pad2[64 - sizeof(td::uint32) * 2]; void init() { i = 0; @@ -342,7 +336,7 @@ class BufferQueue { #if TD_LINUX class BufferedFdQueue { int fd; - atomic wait_flag; + std::atomic wait_flag{0}; BufferQueue q; char pad[64]; @@ -440,7 +434,7 @@ class BufferedFdQueue { class FdQueue { int fd; - atomic wait_flag; + std::atomic wait_flag{0}; VarQueue q; char pad[64]; @@ -572,8 +566,8 @@ class QueueBenchmark2 : public td::Benchmark { int server_active_connections; int client_active_connections; - vector server_conn; - vector client_conn; + std::vector server_conn; + std::vector client_conn; public: explicit QueueBenchmark2(int connections_n = 1) : connections_n(connections_n) { @@ -615,7 +609,7 @@ class QueueBenchmark2 : public td::Benchmark { } void *server_run(void *) { - server_conn = vector(connections_n); + server_conn = std::vector(connections_n); server_active_connections = connections_n; while (server_active_connections > 0) { @@ -656,7 +650,7 @@ class QueueBenchmark2 : public td::Benchmark { } void *client_run(void *) { - client_conn = vector(connections_n); + client_conn = std::vector(connections_n); client_active_connections = connections_n; if (queries_n >= (1 << 24)) { std::fprintf(stderr, "Too big queries_n\n"); @@ -732,7 +726,7 @@ class QueueBenchmark : public td::Benchmark { } void *server_run(void *) { - vector conn(connections_n); + std::vector conn(connections_n); int active_connections = connections_n; while (active_connections > 0) { qvalue_t value = server.get(); @@ -756,7 +750,7 @@ class QueueBenchmark : public td::Benchmark { } void *client_run(void *) { - vector conn(connections_n); + std::vector conn(connections_n); if (queries_n >= (1 << 24)) { std::fprintf(stderr, "Too big queries_n\n"); std::exit(0); @@ -789,7 +783,7 @@ class QueueBenchmark : public td::Benchmark { } void *client_run2(void *) { - vector conn(connections_n); + std::vector conn(connections_n); if (queries_n >= (1 << 24)) { std::fprintf(stderr, "Too big queries_n\n"); std::exit(0); diff --git a/memprof/memprof.cpp b/memprof/memprof.cpp index 74795668f..5025b6a2b 100644 --- a/memprof/memprof.cpp +++ b/memprof/memprof.cpp @@ -72,7 +72,8 @@ static int fast_backtrace(void **buffer, int size) { return i; } -static std::atomic fast_backtrace_failed_cnt, backtrace_total_cnt; +static std::atomic fast_backtrace_failed_cnt; +static std::atomic backtrace_total_cnt; double get_fast_backtrace_success_rate() { return 1 - static_cast(fast_backtrace_failed_cnt.load(std::memory_order_relaxed)) / static_cast(std::max(std::size_t(1), backtrace_total_cnt.load(std::memory_order_relaxed))); diff --git a/td/telegram/Global.h b/td/telegram/Global.h index c6258fc64..9a5da17b2 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -378,8 +378,8 @@ class Global : public ActorContext { int32 gc_scheduler_id_; int32 slow_net_scheduler_id_; - std::atomic server_time_difference_; - std::atomic server_time_difference_was_updated_; + std::atomic server_time_difference_{0.0}; + std::atomic server_time_difference_was_updated_{false}; std::atomic close_flag_{false}; std::vector> net_stats_file_callbacks_; diff --git a/tdactor/td/actor/impl/ConcurrentScheduler.h b/tdactor/td/actor/impl/ConcurrentScheduler.h index ef0b177e3..cb4ed9734 100644 --- a/tdactor/td/actor/impl/ConcurrentScheduler.h +++ b/tdactor/td/actor/impl/ConcurrentScheduler.h @@ -86,7 +86,7 @@ class ConcurrentScheduler : private Scheduler::Callback { enum class State { Start, Run }; State state_ = State::Start; std::vector> schedulers_; - std::atomic is_finished_; + std::atomic is_finished_{false}; std::mutex at_finish_mutex_; std::vector> at_finish_; #if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED diff --git a/tddb/td/db/binlog/ConcurrentBinlog.h b/tddb/td/db/binlog/ConcurrentBinlog.h index 18a142011..6cac41398 100644 --- a/tddb/td/db/binlog/ConcurrentBinlog.h +++ b/tddb/td/db/binlog/ConcurrentBinlog.h @@ -63,7 +63,7 @@ class ConcurrentBinlog : public BinlogInterface { ActorOwn binlog_actor_; string path_; - std::atomic last_id_; + std::atomic last_id_{0}; }; } // namespace td diff --git a/tdutils/td/utils/HazardPointers.h b/tdutils/td/utils/HazardPointers.h index b607cf9b9..67ce75c92 100644 --- a/tdutils/td/utils/HazardPointers.h +++ b/tdutils/td/utils/HazardPointers.h @@ -20,7 +20,7 @@ class HazardPointers { explicit HazardPointers(size_t threads_n) : threads_(threads_n) { for (auto &data : threads_) { for (auto &ptr : data.hazard) { - ptr = nullptr; + std::atomic_init(&ptr, nullptr); } } } diff --git a/tdutils/td/utils/MemoryLog.h b/tdutils/td/utils/MemoryLog.h index 9ec11c6b4..1864a0c49 100644 --- a/tdutils/td/utils/MemoryLog.h +++ b/tdutils/td/utils/MemoryLog.h @@ -77,7 +77,7 @@ class MemoryLog : public LogInterface { private: char buffer_[buffer_size]; - std::atomic pos_; + std::atomic pos_{0}; }; } // namespace td diff --git a/tdutils/td/utils/MpmcQueue.h b/tdutils/td/utils/MpmcQueue.h index 077b9bfbf..10be19302 100644 --- a/tdutils/td/utils/MpmcQueue.h +++ b/tdutils/td/utils/MpmcQueue.h @@ -306,9 +306,9 @@ class MpmcQueueOld { MpmcQueueBlock block; //Got pad in MpmcQueueBlock }; - std::atomic write_pos_; + std::atomic write_pos_{nullptr}; char pad[TD_CONCURRENCY_PAD - sizeof(std::atomic)]; - std::atomic read_pos_; + std::atomic read_pos_{nullptr}; char pad2[TD_CONCURRENCY_PAD - sizeof(std::atomic)]; size_t block_size_; HazardPointers hazard_pointers_; @@ -438,9 +438,9 @@ class MpmcQueue { char pad[TD_CONCURRENCY_PAD - sizeof(std::atomic)]; //Got pad in MpmcQueueBlock }; - std::atomic write_pos_; + std::atomic write_pos_{nullptr}; char pad[TD_CONCURRENCY_PAD - sizeof(std::atomic)]; - std::atomic read_pos_; + std::atomic read_pos_{nullptr}; char pad2[TD_CONCURRENCY_PAD - sizeof(std::atomic)]; HazardPointers hazard_pointers_; //Got pad in HazardPointers diff --git a/tdutils/td/utils/SharedObjectPool.h b/tdutils/td/utils/SharedObjectPool.h index 09c9ac102..ebe0e90f0 100644 --- a/tdutils/td/utils/SharedObjectPool.h +++ b/tdutils/td/utils/SharedObjectPool.h @@ -34,7 +34,7 @@ class AtomicRefCnt { } private: - std::atomic cnt_; + std::atomic cnt_{0}; }; template diff --git a/tdutils/td/utils/buffer.cpp b/tdutils/td/utils/buffer.cpp index c4d394c1c..42eb2769d 100644 --- a/tdutils/td/utils/buffer.cpp +++ b/tdutils/td/utils/buffer.cpp @@ -103,13 +103,14 @@ BufferRaw *BufferAllocator::create_buffer_raw(size_t size) { new (buffer_raw) BufferRaw(); buffer_raw->data_size_ = size; buffer_raw->begin_ = 0; - buffer_raw->end_ = 0; + std::atomic_init(&buffer_raw->end_, 0); - buffer_raw->ref_cnt_.store(1, std::memory_order_relaxed); - buffer_raw->has_writer_.store(true, std::memory_order_relaxed); + std::atomic_init(&buffer_raw->ref_cnt_, 1); + std::atomic_init(&buffer_raw->has_writer_, true); buffer_raw->was_reader_ = false; return buffer_raw; } + void BufferBuilder::append(BufferSlice slice) { if (append_inplace(slice.as_slice())) { return; diff --git a/tdutils/td/utils/queue.h b/tdutils/td/utils/queue.h index ae0aab5a6..ea465564c 100644 --- a/tdutils/td/utils/queue.h +++ b/tdutils/td/utils/queue.h @@ -67,7 +67,7 @@ class SPSCBlockQueue { } struct Position { - std::atomic i; + std::atomic i{0}; char pad[64 - sizeof(std::atomic)]; uint32 local_writer_i; char pad2[64 - sizeof(uint32)]; @@ -252,7 +252,7 @@ class SPSCChainQueue { private: struct Node { BlockQueueT q_; - std::atomic is_closed_; + std::atomic is_closed_{false}; Node *next_; void init() { @@ -399,7 +399,7 @@ class PollQueue : public QueueT { private: EventFd event_fd_; - std::atomic wait_state_; + std::atomic wait_state_{0}; int writer_wait_state_; int get_wait_state() { diff --git a/tdutils/test/HazardPointers.cpp b/tdutils/test/HazardPointers.cpp index ad2ac24a4..28e148d6b 100644 --- a/tdutils/test/HazardPointers.cpp +++ b/tdutils/test/HazardPointers.cpp @@ -15,7 +15,7 @@ #if !TD_THREAD_UNSUPPORTED TEST(HazardPointers, stress) { struct Node { - std::atomic name_; + std::atomic name_{nullptr}; char pad[64]; }; int threads_n = 10; diff --git a/tdutils/test/MpmcWaiter.cpp b/tdutils/test/MpmcWaiter.cpp index 1d364b492..70d8ed6ca 100644 --- a/tdutils/test/MpmcWaiter.cpp +++ b/tdutils/test/MpmcWaiter.cpp @@ -18,7 +18,7 @@ TEST(MpmcWaiter, stress_one_one) { td::Stage check; std::vector threads; - std::atomic value; + std::atomic value{0}; size_t write_cnt = 10; td::unique_ptr waiter; size_t threads_n = 2; @@ -64,8 +64,8 @@ TEST(MpmcWaiter, stress) { std::vector threads; size_t write_n; size_t read_n; - std::atomic write_pos; - std::atomic read_pos; + std::atomic write_pos{0}; + std::atomic read_pos{0}; size_t end_pos; size_t write_cnt; size_t threads_n = 20; diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index 94c7c5d19..d85952302 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -96,7 +96,7 @@ TEST(Misc, errno_tls_bug) { #if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED EventFd test_event_fd; test_event_fd.init(); - std::atomic s(0); + std::atomic s{0}; s = 1; td::thread th([&] { while (s != 1) {