]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/Timer: make SafeTimer a template
authorXiubo Li <xiubli@redhat.com>
Mon, 2 Aug 2021 06:20:52 +0000 (14:20 +0800)
committerXiubo Li <xiubli@redhat.com>
Mon, 13 Sep 2021 06:30:49 +0000 (14:30 +0800)
Signed-off-by: Xiubo Li <xiubli@redhat.com>
(cherry picked from commit 215b12ae0abc781430f3e5d30991b7f5055aeb95)

 Conflicts:
src/common/Timer.cc
src/librbd/ImageCtx.h

28 files changed:
src/common/Timer.cc
src/common/Timer.h
src/journal/JournalMetadata.h
src/journal/JournalPlayer.h
src/journal/JournalRecorder.h
src/journal/Journaler.h
src/journal/ObjectPlayer.h
src/journal/ObjectRecorder.h
src/librbd/ImageCtx.cc
src/librbd/ImageCtx.h
src/librbd/Journal.h
src/librbd/cache/pwl/AbstractWriteLog.h
src/librbd/cache/pwl/rwl/WriteLog.h
src/librbd/image/RemoveRequest.h
src/librbd/io/TypeTraits.h
src/librbd/journal/CreateRequest.h
src/librbd/journal/RemoveRequest.h
src/librbd/journal/ResetRequest.h
src/librbd/mirror/snapshot/PromoteRequest.h
src/librbd/plugin/Api.h
src/rgw/rgw_rados.h
src/tools/cephfs_mirror/Mirror.cc
src/tools/cephfs_mirror/ServiceDaemon.h
src/tools/immutable_object_cache/ObjectCacheStore.cc
src/tools/immutable_object_cache/ObjectCacheStore.h
src/tools/rbd_mirror/ImageDeleter.h
src/tools/rbd_mirror/Threads.h
src/tools/rbd_mirror/image_replayer/BootstrapRequest.h

index eab46661c99e10a65d6e5f9173bd83a31cf06efd..b96d9d2ff7197ff570107974b702e62ff0a419c6 100644 (file)
@@ -24,18 +24,19 @@ using std::pair;
 
 using ceph::operator <<;
 
-class SafeTimerThread : public Thread {
-  SafeTimer *parent;
+template <class Mutex>
+class CommonSafeTimerThread : public Thread {
+  CommonSafeTimer<Mutex> *parent;
 public:
-  explicit SafeTimerThread(SafeTimer *s) : parent(s) {}
+  explicit CommonSafeTimerThread(CommonSafeTimer<Mutex> *s) : parent(s) {}
   void *entry() override {
     parent->timer_thread();
     return NULL;
   }
 };
 
-
-SafeTimer::SafeTimer(CephContext *cct_, ceph::mutex &l, bool safe_callbacks)
+template <class Mutex>
+CommonSafeTimer<Mutex>::CommonSafeTimer(CephContext *cct_, Mutex &l, bool safe_callbacks)
   : cct(cct_), lock(l),
     safe_callbacks(safe_callbacks),
     thread(NULL),
@@ -43,19 +44,22 @@ SafeTimer::SafeTimer(CephContext *cct_, ceph::mutex &l, bool safe_callbacks)
 {
 }
 
-SafeTimer::~SafeTimer()
+template <class Mutex>
+CommonSafeTimer<Mutex>::~CommonSafeTimer()
 {
   ceph_assert(thread == NULL);
 }
 
-void SafeTimer::init()
+template <class Mutex>
+void CommonSafeTimer<Mutex>::init()
 {
   ldout(cct,10) << "init" << dendl;
-  thread = new SafeTimerThread(this);
+  thread = new CommonSafeTimerThread<Mutex>(this);
   thread->create("safe_timer");
 }
 
-void SafeTimer::shutdown()
+template <class Mutex>
+void CommonSafeTimer<Mutex>::shutdown()
 {
   ldout(cct,10) << "shutdown" << dendl;
   if (thread) {
@@ -71,7 +75,8 @@ void SafeTimer::shutdown()
   }
 }
 
-void SafeTimer::timer_thread()
+template <class Mutex>
+void CommonSafeTimer<Mutex>::timer_thread()
 {
   std::unique_lock l{lock};
   ldout(cct,10) << "timer_thread starting" << dendl;
@@ -115,12 +120,14 @@ void SafeTimer::timer_thread()
   ldout(cct,10) << "timer_thread exiting" << dendl;
 }
 
-Context* SafeTimer::add_event_after(double seconds, Context *callback)
+template <class Mutex>
+Context* CommonSafeTimer<Mutex>::add_event_after(double seconds, Context *callback)
 {
   return add_event_after(ceph::make_timespan(seconds), callback);
 }
 
-Context* SafeTimer::add_event_after(ceph::timespan duration, Context *callback)
+template <class Mutex>
+Context* CommonSafeTimer<Mutex>::add_event_after(ceph::timespan duration, Context *callback)
 {
   ceph_assert(ceph_mutex_is_locked(lock));
 
@@ -128,7 +135,8 @@ Context* SafeTimer::add_event_after(ceph::timespan duration, Context *callback)
   return add_event_at(when, callback);
 }
 
-Context* SafeTimer::add_event_at(SafeTimer::clock_t::time_point when, Context *callback)
+template <class Mutex>
+Context* CommonSafeTimer<Mutex>::add_event_at(CommonSafeTimer<Mutex>::clock_t::time_point when, Context *callback)
 {
   ceph_assert(ceph_mutex_is_locked(lock));
   ldout(cct,10) << __func__ << " " << when << " -> " << callback << dendl;
@@ -153,10 +161,11 @@ Context* SafeTimer::add_event_at(SafeTimer::clock_t::time_point when, Context *c
   return callback;
 }
 
-bool SafeTimer::cancel_event(Context *callback)
+template <class Mutex>
+bool CommonSafeTimer<Mutex>::cancel_event(Context *callback)
 {
   ceph_assert(ceph_mutex_is_locked(lock));
-  
+
   auto p = events.find(callback);
   if (p == events.end()) {
     ldout(cct,10) << "cancel_event " << callback << " not found" << dendl;
@@ -171,7 +180,8 @@ bool SafeTimer::cancel_event(Context *callback)
   return true;
 }
 
-void SafeTimer::cancel_all_events()
+template <class Mutex>
+void CommonSafeTimer<Mutex>::cancel_all_events()
 {
   ldout(cct,10) << "cancel_all_events" << dendl;
   ceph_assert(ceph_mutex_is_locked(lock));
@@ -185,7 +195,8 @@ void SafeTimer::cancel_all_events()
   }
 }
 
-void SafeTimer::dump(const char *caller) const
+template <class Mutex>
+void CommonSafeTimer<Mutex>::dump(const char *caller) const
 {
   if (!caller)
     caller = "";
@@ -196,3 +207,6 @@ void SafeTimer::dump(const char *caller) const
        ++s)
     ldout(cct,10) << " " << s->first << "->" << s->second << dendl;
 }
+
+template class CommonSafeTimer<ceph::mutex>;
+template class CommonSafeTimer<ceph::fair_mutex>;
index f543be68a766402836556145c5d057d999e0efa2..f22956a347615eabffe30317e6d5464e7fb63bad 100644 (file)
 #include "include/common_fwd.h"
 #include "ceph_time.h"
 #include "ceph_mutex.h"
+#include "fair_mutex.h"
+#include <condition_variable>
 
 class Context;
-class SafeTimerThread;
 
-class SafeTimer
+template <class Mutex> class CommonSafeTimerThread;
+
+template <class Mutex>
+class CommonSafeTimer
 {
   CephContext *cct;
-  ceph::mutex& lock;
-  ceph::condition_variable cond;
+  Mutex& lock;
+  std::condition_variable_any cond;
   bool safe_callbacks;
 
-  friend class SafeTimerThread;
-  SafeTimerThread *thread;
+  friend class CommonSafeTimerThread<Mutex>;
+  class CommonSafeTimerThread<Mutex> *thread;
 
   void timer_thread();
   void _shutdown();
@@ -47,8 +51,8 @@ class SafeTimer
 
 public:
   // This class isn't supposed to be copied
-  SafeTimer(const SafeTimer&) = delete;
-  SafeTimer& operator=(const SafeTimer&) = delete;
+  CommonSafeTimer(const CommonSafeTimer&) = delete;
+  CommonSafeTimer& operator=(const CommonSafeTimer&) = delete;
 
   /* Safe callbacks determines whether callbacks are called with the lock
    * held.
@@ -60,8 +64,8 @@ public:
    * If you are able to relax requirements on cancelled callbacks, then
    * setting safe_callbacks = false eliminates the lock cycle issue.
    * */
-  SafeTimer(CephContext *cct, ceph::mutex &l, bool safe_callbacks=true);
-  virtual ~SafeTimer();
+  CommonSafeTimer(CephContext *cct, Mutex &l, bool safe_callbacks=true);
+  virtual ~CommonSafeTimer();
 
   /* Call with the event_lock UNLOCKED.
    *
@@ -96,4 +100,8 @@ public:
 
 };
 
+extern template class CommonSafeTimer<ceph::mutex>;
+extern template class CommonSafeTimer<ceph::fair_mutex>;
+using SafeTimer = class CommonSafeTimer<ceph::mutex>;
+
 #endif
index 1b6911daecbad689f9126649b9d0e7e07e50f12d..071456f56ecc02a4b64d4e129eb378785c11f1fa 100644 (file)
@@ -9,6 +9,7 @@
 #include "include/rados/librados.hpp"
 #include "common/AsyncOpTracker.h"
 #include "common/Cond.h"
+#include "common/Timer.h"
 #include "common/ceph_mutex.h"
 #include "common/RefCountedObj.h"
 #include "common/WorkQueue.h"
@@ -23,8 +24,6 @@
 #include <string>
 #include "include/ceph_assert.h"
 
-class SafeTimer;
-
 namespace journal {
 
 class JournalMetadata : public RefCountedObject, boost::noncopyable {
index f2ab14d7b43c87805e2abccc34b49794de60dd14..a71117a836a19e3d79ac4cdf9e4b11da88646447 100644 (file)
@@ -8,6 +8,7 @@
 #include "include/Context.h"
 #include "include/rados/librados.hpp"
 #include "common/AsyncOpTracker.h"
+#include "common/Timer.h"
 #include "journal/JournalMetadata.h"
 #include "journal/ObjectPlayer.h"
 #include "journal/Types.h"
@@ -16,8 +17,6 @@
 #include <boost/optional.hpp>
 #include <map>
 
-class SafeTimer;
-
 namespace journal {
 
 class CacheManagerHandler;
index 680dbcdfead58a2fb4017ad01325bbe80eed2878..9d8ea6c10f3d924a73dc78a72b6d7b82ace440b7 100644 (file)
@@ -9,6 +9,7 @@
 #include "include/rados/librados.hpp"
 #include "common/ceph_mutex.h"
 #include "common/containers.h"
+#include "common/Timer.h"
 #include "journal/Future.h"
 #include "journal/FutureImpl.h"
 #include "journal/JournalMetadata.h"
@@ -16,8 +17,6 @@
 #include <map>
 #include <string>
 
-class SafeTimer;
-
 namespace journal {
 
 class JournalRecorder {
index fe44401848aca931297f2561cf292221cc43105b..f42513c5d67fe5fe40e0214e17f5c2e41d438500 100644 (file)
 #include "journal/Future.h"
 #include "journal/JournalMetadataListener.h"
 #include "cls/journal/cls_journal_types.h"
+#include "common/Timer.h"
 #include <list>
 #include <map>
 #include <string>
 #include "include/ceph_assert.h"
 
 class ContextWQ;
-class SafeTimer;
 class ThreadPool;
 
 namespace journal {
index 41641dd150ac90ca3d2be7b65d8792b98815ab4a..b9446252a2744e40feae44ea80bf88c028c07586 100644 (file)
@@ -8,6 +8,7 @@
 #include "include/interval_set.h"
 #include "include/rados/librados.hpp"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "common/RefCountedObj.h"
 #include "journal/Entry.h"
 #include <list>
@@ -16,8 +17,6 @@
 #include <boost/unordered_map.hpp>
 #include "include/ceph_assert.h"
 
-class SafeTimer;
-
 namespace journal {
 
 class ObjectPlayer : public RefCountedObject {
index 7281879fcf722027fae6857cd7e805de482e74ce..5c5f88c86db96f130ccb979620c587310adb5c62 100644 (file)
@@ -10,6 +10,7 @@
 #include "common/ceph_mutex.h"
 #include "common/RefCountedObj.h"
 #include "common/WorkQueue.h"
+#include "common/Timer.h"
 #include "journal/FutureImpl.h"
 #include <list>
 #include <map>
@@ -17,8 +18,6 @@
 #include <boost/noncopyable.hpp>
 #include "include/ceph_assert.h"
 
-class SafeTimer;
-
 namespace journal {
 
 class ObjectRecorder;
index ed836ed8e4e41dd40665d48097aa34060f22cd05..2051ebef9bda1381449136c15471d4d65e11e38b 100644 (file)
@@ -58,7 +58,7 @@ namespace librbd {
 
 namespace {
 
-class SafeTimerSingleton : public SafeTimer {
+class SafeTimerSingleton : public CommonSafeTimer<ceph::mutex> {
 public:
   ceph::mutex lock = ceph::make_mutex("librbd::SafeTimerSingleton::lock");
 
index 31d9a67547d62998e98da6feb18fc3043e254aba..043b26efe300d97f7e4a6341c64490a1c3b4bb5f 100644 (file)
@@ -14,6 +14,7 @@
 #include <vector>
 
 #include "common/allocator.h"
+#include "common/Timer.h"
 #include "common/ceph_mutex.h"
 #include "common/config_proxy.h"
 #include "common/event_socket.h"
@@ -36,8 +37,6 @@
 #include <boost/lockfree/policies.hpp>
 #include <boost/lockfree/queue.hpp>
 
-class SafeTimer;
-
 namespace neorados {
 class IOContext;
 class RADOS;
index 64ea13fe23ac9f8c811d38cad34c92156703c5c4..406c0e34cbf0eccc65d302a11bdcac32502a613f 100644 (file)
@@ -10,6 +10,7 @@
 #include "include/rados/librados_fwd.hpp"
 #include "common/AsyncOpTracker.h"
 #include "common/Cond.h"
+#include "common/Timer.h"
 #include "common/RefCountedObj.h"
 #include "journal/Future.h"
 #include "journal/JournalMetadataListener.h"
@@ -27,7 +28,6 @@
 #include <unordered_map>
 
 class ContextWQ;
-class SafeTimer;
 namespace journal { class Journaler; }
 
 namespace librbd {
index 56ecd6c1c209111beee6bb702af15195a5dfe439..7b11542d65a8a827a366058d6b597a8ca98b3ac8 100644 (file)
@@ -4,6 +4,7 @@
 #ifndef CEPH_LIBRBD_CACHE_PARENT_WRITE_LOG
 #define CEPH_LIBRBD_CACHE_PARENT_WRITE_LOG
 
+#include "common/Timer.h"
 #include "common/RWLock.h"
 #include "common/WorkQueue.h"
 #include "common/AsyncOpTracker.h"
@@ -20,7 +21,6 @@
 #include <list>
 
 class Context;
-class SafeTimer;
 
 namespace librbd {
 
index 7aea8573c917323b1e5a8aee4b13a0fbe9cfe370..d5c132e44630407c51e854ff27c2cbff5847067e 100644 (file)
@@ -7,6 +7,7 @@
 #include <functional>
 #include <libpmemobj.h>
 #include <list>
+#include "common/Timer.h"
 #include "common/RWLock.h"
 #include "common/WorkQueue.h"
 #include "common/AsyncOpTracker.h"
@@ -21,7 +22,6 @@
 #include "librbd/cache/pwl/rwl/Builder.h"
 
 class Context;
-class SafeTimer;
 
 namespace librbd {
 
index 79512c932ce2ab7006aab11a1932add8b0f3023b..b03f8fc7c21cb48c4d4bd4b4585c9120fa39fd79 100644 (file)
@@ -7,11 +7,11 @@
 #include "include/rados/librados.hpp"
 #include "librbd/ImageCtx.h"
 #include "librbd/image/TypeTraits.h"
+#include "common/Timer.h"
 
 #include <list>
 
 class Context;
-class SafeTimer;
 
 namespace librbd {
 
index 34703158a3e68abf20b298fd336bc2de1b3292cb..2f3a6b7efcf2959e6eaea41eab9a3f2e51b4c305 100644 (file)
@@ -4,7 +4,7 @@
 #ifndef CEPH_LIBRBD_IO_TYPE_TRAITS_H
 #define CEPH_LIBRBD_IO_TYPE_TRAITS_H
 
-class SafeTimer;
+#include "common/Timer.h"
 
 namespace librbd {
 namespace io {
index 8a4e40f88c442d37bdfae5653f87df37f8e987ba..6fab409c43021792ce49acc8e8753104853b5b35 100644 (file)
@@ -9,6 +9,7 @@
 #include "include/rados/librados.hpp"
 #include "include/rbd/librbd.hpp"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "librbd/ImageCtx.h"
 #include "journal/Journaler.h"
 #include "librbd/journal/Types.h"
@@ -20,7 +21,6 @@ using journal::Journaler;
 
 class Context;
 class ContextWQ;
-class SafeTimer;
 
 namespace journal {
   class Journaler;
index 13dff87a08e7e548675eb2ad7781e6564d7ef2e7..14b1c4dc59e6db8c19d7bd3c9fd9ebb50eec2b86 100644 (file)
 #include "librbd/ImageCtx.h"
 #include "journal/Journaler.h"
 #include "librbd/journal/TypeTraits.h"
+#include "common/Timer.h"
 
 using librados::IoCtx;
 using journal::Journaler;
 
 class Context;
 class ContextWQ;
-class SafeTimer;
 
 namespace journal {
   class Journaler;
index 44f5ac8a6bd7e07545b565eec8b56f10217f33aa..f9331f6446490ff3884ba4decd45a3dfe74b983f 100644 (file)
@@ -9,11 +9,11 @@
 #include "include/rados/librados.hpp"
 #include "include/rbd/librbd.hpp"
 #include "librbd/journal/TypeTraits.h"
+#include "common/Timer.h"
 #include <string>
 
 class Context;
 class ContextWQ;
-class SafeTimer;
 
 namespace journal { class Journaler; }
 
index 9fdd5ed71a5d9c1d893c9d3f8dc9dc80e27b9a65..1d9a862a0afb3ef53390ab6f88f4b3d27d434743 100644 (file)
@@ -7,12 +7,12 @@
 #include "include/buffer.h"
 #include "include/rbd/librbd.hpp"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "librbd/internal.h"
 
 #include <string>
 #include <set>
 
-class SafeTimer;
 struct Context;
 
 namespace librbd {
index 2d55c17c19fe06128bbdd1a46e57d62a42f61bef..04f77e5c3e76d6ff34e93c0ea9f298ccffd70cc8 100644 (file)
@@ -4,6 +4,7 @@
 #ifndef CEPH_LIBRBD_PLUGIN_API_H
 #define CEPH_LIBRBD_PLUGIN_API_H
 
+#include "common/Timer.h"
 #include "common/ceph_mutex.h"
 #include "include/common_fwd.h"
 #include "include/int_types.h"
@@ -13,8 +14,6 @@
 
 namespace ZTracer { struct Trace; }
 
-class SafeTimer;
-
 namespace librbd {
 
 namespace io {
index 4542c4a1f9172746b641e698d78fa7f79f49a7ef..2c576bc8e6c4bb35c4203d9f4b966ae37211460f 100644 (file)
@@ -12,6 +12,7 @@
 #include "common/RefCountedObj.h"
 #include "common/RWLock.h"
 #include "common/ceph_time.h"
+#include "common/Timer.h"
 #include "rgw_common.h"
 #include "cls/rgw/cls_rgw_types.h"
 #include "cls/version/cls_version_types.h"
@@ -32,7 +33,6 @@
 #include "services/svc_bi_rados.h"
 
 class RGWWatcher;
-class SafeTimer;
 class ACLOwner;
 class RGWGC;
 class RGWMetaNotifier;
index efaa27106e51da94863bae15c4910c781c379202..0f01495fa8648030b8a5cb55d1a315789196623d 100644 (file)
@@ -27,7 +27,7 @@ namespace {
 
 const std::string SERVICE_DAEMON_MIRROR_ENABLE_FAILED_KEY("mirroring_failed");
 
-class SafeTimerSingleton : public SafeTimer {
+class SafeTimerSingleton : public CommonSafeTimer<ceph::mutex> {
 public:
   ceph::mutex timer_lock = ceph::make_mutex("cephfs::mirror::timer_lock");
 
index 393c7d5cffd65674d06e96a0dac6a58fa23ff25d..83eee286d41eb687706d2780b22e4eb2eaec18ca 100644 (file)
@@ -5,11 +5,10 @@
 #define CEPHFS_MIRROR_SERVICE_DAEMON_H
 
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "mds/FSMap.h"
 #include "Types.h"
 
-class SafeTimer;
-
 namespace cephfs {
 namespace mirror {
 
index 483687ffe5fc9af95fb2fb38d36d7a4abbccc924..1b1eef1e38360e196eba63475ed8341fba8d7e9b 100644 (file)
@@ -23,13 +23,13 @@ namespace immutable_obj_cache {
 
 namespace {
 
-class SafeTimerSingleton : public SafeTimer {
+class SafeTimerSingleton : public CommonSafeTimer<ceph::mutex> {
 public:
   ceph::mutex lock = ceph::make_mutex
     ("ceph::immutable_object_cache::SafeTimerSingleton::lock");
 
   explicit SafeTimerSingleton(CephContext *cct)
-      : SafeTimer(cct, lock, true) {
+      : CommonSafeTimer(cct, lock, true) {
     init();
   }
   ~SafeTimerSingleton() {
index 607921320aef4541f4f6c9565ab08ee501e15f11..51e5a77b855d01e1cf28ff95ab57f2799503d88b 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "common/ceph_context.h"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "common/Throttle.h"
 #include "common/Cond.h"
 #include "include/rados/librados.hpp"
index d9f4e14a7e2ccb7eedf0d5cf7a852e830eb31690..5fe79496bc998797c039262c86f7ad76801f5e82 100644 (file)
@@ -18,6 +18,7 @@
 #include "include/utime.h"
 #include "common/AsyncOpTracker.h"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "tools/rbd_mirror/Types.h"
 #include "tools/rbd_mirror/image_deleter/Types.h"
 #include <atomic>
@@ -29,7 +30,6 @@
 
 class AdminSocketHook;
 class Context;
-class SafeTimer;
 namespace librbd {
 struct ImageCtx;
 namespace asio { struct ContextWQ; }
index 91c923ab4a04e30860610756c2422ad23af9c236..35c0b0f1cfa3e86e7fbc319a1611989cede8d3f7 100644 (file)
@@ -7,9 +7,9 @@
 #include "include/common_fwd.h"
 #include "include/rados/librados_fwd.hpp"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include <memory>
 
-class SafeTimer;
 class ThreadPool;
 
 namespace librbd {
index 3d17ae48bc2fd26fda5212c00bbcb1617e5592ef..f5bb8dd8a16cf181c34ec7cbdd2782f93bd39661 100644 (file)
@@ -7,6 +7,7 @@
 #include "include/int_types.h"
 #include "include/rados/librados.hpp"
 #include "common/ceph_mutex.h"
+#include "common/Timer.h"
 #include "cls/rbd/cls_rbd_types.h"
 #include "librbd/mirror/Types.h"
 #include "tools/rbd_mirror/CancelableRequest.h"
@@ -14,7 +15,6 @@
 #include <string>
 
 class Context;
-class SafeTimer;
 
 namespace journal { class CacheManagerHandler; }
 namespace librbd { class ImageCtx; }