From 93621f5cb826facbdd36140d4d0ddc812973378c Mon Sep 17 00:00:00 2001 From: Jesse Williamson Date: Mon, 24 Apr 2017 00:43:23 -0700 Subject: [PATCH] tools (rbd*): migrate atomic_t to std::atomic<> Signed-off-by: Jesse Williamson --- src/tools/rbd/action/MirrorPool.cc | 25 +++++++++++++------------ src/tools/rbd_mirror/ImageDeleter.cc | 10 +++++----- src/tools/rbd_mirror/ImageDeleter.h | 9 +++++---- src/tools/rbd_mirror/ImageReplayer.h | 13 +++++++------ src/tools/rbd_mirror/Mirror.cc | 16 ++++++++-------- src/tools/rbd_mirror/Mirror.h | 10 +++++----- src/tools/rbd_mirror/PoolReplayer.cc | 20 ++++++++++---------- src/tools/rbd_mirror/PoolReplayer.h | 12 ++++++------ src/tools/rbd_nbd/rbd-nbd.cc | 12 ++++++------ 9 files changed, 65 insertions(+), 62 deletions(-) diff --git a/src/tools/rbd/action/MirrorPool.cc b/src/tools/rbd/action/MirrorPool.cc index 5e51ca8e1ac..929ac446d02 100644 --- a/src/tools/rbd/action/MirrorPool.cc +++ b/src/tools/rbd/action/MirrorPool.cc @@ -4,7 +4,6 @@ #include "tools/rbd/ArgumentTypes.h" #include "tools/rbd/Shell.h" #include "tools/rbd/Utils.h" -#include "include/atomic.h" #include "include/Context.h" #include "include/stringify.h" #include "include/rbd/librbd.hpp" @@ -21,6 +20,8 @@ #include #include "include/assert.h" +#include + #define dout_context g_ceph_context #define dout_subsys ceph_subsys_rbd #undef dout_prefix @@ -319,7 +320,7 @@ private: class PromoteImageRequest : public ImageRequestBase { public: PromoteImageRequest(librados::IoCtx &io_ctx, OrderedThrottle &throttle, - const std::string &image_name, atomic_t *counter, + const std::string &image_name, std::atomic *counter, bool force) : ImageRequestBase(io_ctx, throttle, image_name), m_counter(counter), m_force(force) { @@ -334,11 +335,11 @@ protected: librbd::RBD::AioCompletion *aio_comp) override { image.aio_mirror_image_promote(m_force, aio_comp); } + void handle_execute_action(int r) override { if (r >= 0) { - m_counter->inc(); + (*m_counter)++; } - ImageRequestBase::handle_execute_action(r); } std::string get_action_type() const override { @@ -346,14 +347,14 @@ protected: } private: - atomic_t *m_counter; + std::atomic *m_counter = nullptr; bool m_force; }; class DemoteImageRequest : public ImageRequestBase { public: DemoteImageRequest(librados::IoCtx &io_ctx, OrderedThrottle &throttle, - const std::string &image_name, atomic_t *counter) + const std::string &image_name, std::atomic *counter) : ImageRequestBase(io_ctx, throttle, image_name), m_counter(counter) { } @@ -368,7 +369,7 @@ protected: } void handle_execute_action(int r) override { if (r >= 0) { - m_counter->inc(); + (*m_counter)++; } ImageRequestBase::handle_execute_action(r); } @@ -378,7 +379,7 @@ protected: } private: - atomic_t *m_counter; + std::atomic *m_counter = nullptr; }; class StatusImageRequest : public ImageRequestBase { @@ -915,12 +916,12 @@ int execute_promote(const po::variables_map &vm) { return r; } - atomic_t counter; + std::atomic counter = { 0 }; ImageRequestGenerator generator(io_ctx, &counter, vm["force"].as()); r = generator.execute(); - std::cout << "Promoted " << counter.read() << " mirrored images" << std::endl; + std::cout << "Promoted " << counter.load() << " mirrored images" << std::endl; return r; } @@ -940,11 +941,11 @@ int execute_demote(const po::variables_map &vm) { return r; } - atomic_t counter; + std::atomic counter { 0 }; ImageRequestGenerator generator(io_ctx, &counter); r = generator.execute(); - std::cout << "Demoted " << counter.read() << " mirrored images" << std::endl; + std::cout << "Demoted " << counter.load() << " mirrored images" << std::endl; return r; } diff --git a/src/tools/rbd_mirror/ImageDeleter.cc b/src/tools/rbd_mirror/ImageDeleter.cc index 59ffaa291b6..b2eb615a7e4 100644 --- a/src/tools/rbd_mirror/ImageDeleter.cc +++ b/src/tools/rbd_mirror/ImageDeleter.cc @@ -134,7 +134,7 @@ private: ImageDeleter::ImageDeleter(ContextWQ *work_queue, SafeTimer *timer, Mutex *timer_lock) - : m_running(1), + : m_running(true), m_work_queue(work_queue), m_delete_lock("rbd::mirror::ImageDeleter::Delete"), m_image_deleter_thread(this), @@ -149,7 +149,7 @@ ImageDeleter::ImageDeleter(ContextWQ *work_queue, SafeTimer *timer, ImageDeleter::~ImageDeleter() { dout(20) << "enter" << dendl; - m_running.set(0); + m_running = false; { Mutex::Locker l (m_delete_lock); m_delete_queue_cond.Signal(); @@ -164,13 +164,13 @@ ImageDeleter::~ImageDeleter() { void ImageDeleter::run() { dout(20) << "enter" << dendl; - while(m_running.read()) { + while(m_running) { m_delete_lock.Lock(); while (m_delete_queue.empty()) { dout(20) << "waiting for delete requests" << dendl; m_delete_queue_cond.Wait(m_delete_lock); - if (!m_running.read()) { + if (!m_running) { m_delete_lock.Unlock(); dout(20) << "return" << dendl; return; @@ -183,7 +183,7 @@ void ImageDeleter::run() { bool move_to_next = process_image_delete(); if (!move_to_next) { - if (!m_running.read()) { + if (!m_running) { dout(20) << "return" << dendl; return; } diff --git a/src/tools/rbd_mirror/ImageDeleter.h b/src/tools/rbd_mirror/ImageDeleter.h index 9ff990b781c..c91f2fbc6ff 100644 --- a/src/tools/rbd_mirror/ImageDeleter.h +++ b/src/tools/rbd_mirror/ImageDeleter.h @@ -15,15 +15,16 @@ #ifndef CEPH_RBD_MIRROR_IMAGEDELETER_H #define CEPH_RBD_MIRROR_IMAGEDELETER_H -#include -#include -#include "include/atomic.h" #include "common/Mutex.h" #include "common/Cond.h" #include "common/Thread.h" #include "common/Timer.h" #include "types.h" +#include +#include +#include + class ContextWQ; namespace rbd { @@ -98,7 +99,7 @@ private: bool print_failure_info=false); }; - atomic_t m_running; + std::atomic m_running { 0 }; ContextWQ *m_work_queue; diff --git a/src/tools/rbd_mirror/ImageReplayer.h b/src/tools/rbd_mirror/ImageReplayer.h index 5fc8bee0ec6..72f03f77b6c 100644 --- a/src/tools/rbd_mirror/ImageReplayer.h +++ b/src/tools/rbd_mirror/ImageReplayer.h @@ -4,11 +4,6 @@ #ifndef CEPH_RBD_MIRROR_IMAGE_REPLAYER_H #define CEPH_RBD_MIRROR_IMAGE_REPLAYER_H -#include -#include -#include - -#include "include/atomic.h" #include "common/AsyncOpTracker.h" #include "common/Mutex.h" #include "common/WorkQueue.h" @@ -23,10 +18,16 @@ #include "ImageDeleter.h" #include "ProgressContext.h" #include "types.h" -#include + #include #include +#include +#include +#include +#include +#include + class AdminSocketHook; namespace journal { diff --git a/src/tools/rbd_mirror/Mirror.cc b/src/tools/rbd_mirror/Mirror.cc index 86c6939182f..f37d2559922 100644 --- a/src/tools/rbd_mirror/Mirror.cc +++ b/src/tools/rbd_mirror/Mirror.cc @@ -215,7 +215,7 @@ Mirror::~Mirror() void Mirror::handle_signal(int signum) { - m_stopping.set(1); + m_stopping = true; { Mutex::Locker l(m_lock); m_cond.Signal(); @@ -250,7 +250,7 @@ int Mirror::init() void Mirror::run() { dout(20) << "enter" << dendl; - while (!m_stopping.read()) { + while (!m_stopping) { m_local_cluster_watcher->refresh_pools(); Mutex::Locker l(m_lock); if (!m_manual_stop) { @@ -275,7 +275,7 @@ void Mirror::print_status(Formatter *f, stringstream *ss) Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } @@ -314,7 +314,7 @@ void Mirror::start() dout(20) << "enter" << dendl; Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } @@ -330,7 +330,7 @@ void Mirror::stop() dout(20) << "enter" << dendl; Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } @@ -346,7 +346,7 @@ void Mirror::restart() dout(20) << "enter" << dendl; Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } @@ -362,7 +362,7 @@ void Mirror::flush() dout(20) << "enter" << dendl; Mutex::Locker l(m_lock); - if (m_stopping.read() || m_manual_stop) { + if (m_stopping || m_manual_stop) { return; } @@ -376,7 +376,7 @@ void Mirror::release_leader() dout(20) << "enter" << dendl; Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } diff --git a/src/tools/rbd_mirror/Mirror.h b/src/tools/rbd_mirror/Mirror.h index 4ff9d512399..88d15cfc805 100644 --- a/src/tools/rbd_mirror/Mirror.h +++ b/src/tools/rbd_mirror/Mirror.h @@ -4,19 +4,19 @@ #ifndef CEPH_RBD_MIRROR_H #define CEPH_RBD_MIRROR_H -#include -#include -#include - #include "common/ceph_context.h" #include "common/Mutex.h" -#include "include/atomic.h" #include "include/rados/librados.hpp" #include "ClusterWatcher.h" #include "PoolReplayer.h" #include "ImageDeleter.h" #include "types.h" +#include +#include +#include +#include + namespace librbd { struct ImageCtx; } namespace rbd { diff --git a/src/tools/rbd_mirror/PoolReplayer.cc b/src/tools/rbd_mirror/PoolReplayer.cc index 0dbfeffe54d..751b46c2056 100644 --- a/src/tools/rbd_mirror/PoolReplayer.cc +++ b/src/tools/rbd_mirror/PoolReplayer.cc @@ -229,7 +229,7 @@ PoolReplayer::~PoolReplayer() { delete m_asok_hook; - m_stopping.set(1); + m_stopping = true; { Mutex::Locker l(m_lock); m_cond.Signal(); @@ -410,7 +410,7 @@ void PoolReplayer::run() { dout(20) << "enter" << dendl; - while (!m_stopping.read()) { + while (!m_stopping) { std::string asok_hook_name = m_local_io_ctx.get_pool_name() + " " + m_peer.cluster_name; if (m_asok_hook_name != asok_hook_name || m_asok_hook == nullptr) { @@ -425,7 +425,7 @@ void PoolReplayer::run() if ((m_local_pool_watcher && m_local_pool_watcher->is_blacklisted()) || (m_remote_pool_watcher && m_remote_pool_watcher->is_blacklisted())) { m_blacklisted = true; - m_stopping.set(1); + m_stopping = true; break; } @@ -476,7 +476,7 @@ void PoolReplayer::start() Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } @@ -489,10 +489,10 @@ void PoolReplayer::stop(bool manual) Mutex::Locker l(m_lock); if (!manual) { - m_stopping.set(1); + m_stopping = true; m_cond.Signal(); return; - } else if (m_stopping.read()) { + } else if (m_stopping) { return; } @@ -505,7 +505,7 @@ void PoolReplayer::restart() Mutex::Locker l(m_lock); - if (m_stopping.read()) { + if (m_stopping) { return; } @@ -518,7 +518,7 @@ void PoolReplayer::flush() Mutex::Locker l(m_lock); - if (m_stopping.read() || m_manual_stop) { + if (m_stopping || m_manual_stop) { return; } @@ -531,7 +531,7 @@ void PoolReplayer::release_leader() Mutex::Locker l(m_lock); - if (m_stopping.read() || !m_leader_watcher) { + if (m_stopping || !m_leader_watcher) { return; } @@ -541,7 +541,7 @@ void PoolReplayer::release_leader() void PoolReplayer::handle_update(const std::string &mirror_uuid, ImageIds &&added_image_ids, ImageIds &&removed_image_ids) { - if (m_stopping.read()) { + if (m_stopping) { return; } diff --git a/src/tools/rbd_mirror/PoolReplayer.h b/src/tools/rbd_mirror/PoolReplayer.h index 620b6db9a5e..55fdcedf3c3 100644 --- a/src/tools/rbd_mirror/PoolReplayer.h +++ b/src/tools/rbd_mirror/PoolReplayer.h @@ -4,16 +4,10 @@ #ifndef CEPH_RBD_MIRROR_POOL_REPLAYER_H #define CEPH_RBD_MIRROR_POOL_REPLAYER_H -#include -#include -#include -#include - #include "common/AsyncOpTracker.h" #include "common/Cond.h" #include "common/Mutex.h" #include "common/WorkQueue.h" -#include "include/atomic.h" #include "include/rados/librados.hpp" #include "ClusterWatcher.h" @@ -22,6 +16,12 @@ #include "ImageDeleter.h" #include "types.h" +#include +#include +#include +#include +#include + class AdminSocketHook; namespace librbd { class ImageCtx; } diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 8ba83208b2e..62ccd2c1fe6 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -113,7 +113,6 @@ public: NBDServer(int _fd, librbd::Image& _image) : fd(_fd) , image(_image) - , terminated(false) , lock("NBDServer::Locker") , reader_thread(*this, &NBDServer::reader_entry) , writer_thread(*this, &NBDServer::writer_entry) @@ -121,11 +120,12 @@ public: {} private: - atomic_t terminated; + std::atomic terminated = { false }; void shutdown() { - if (terminated.compare_and_swap(false, true)) { + bool expected = false; + if (terminated.compare_exchange_weak(expected, true)) { ::shutdown(fd, SHUT_RDWR); Mutex::Locker l(lock); @@ -172,7 +172,7 @@ private: IOContext *wait_io_finish() { Mutex::Locker l(lock); - while(io_finished.empty() && !terminated.read()) + while(io_finished.empty() && !terminated) cond.Wait(lock); if (io_finished.empty()) @@ -234,7 +234,7 @@ private: void reader_entry() { - while (!terminated.read()) { + while (!terminated) { ceph::unique_ptr ctx(new IOContext()); ctx->server = this; @@ -309,7 +309,7 @@ private: void writer_entry() { - while (!terminated.read()) { + while (!terminated) { dout(20) << __func__ << ": waiting for io request" << dendl; ceph::unique_ptr ctx(wait_io_finish()); if (!ctx) { -- 2.39.5