]> git.apps.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>
Wed, 25 Aug 2021 01:42:33 +0000 (09:42 +0800)
Signed-off-by: Xiubo Li <xiubli@redhat.com>
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 17547962086e691b3e2c7ce181220e95ab0162c8..48c79d613d074b1ced6d7b89748489d5f60632b8 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,7 +161,8 @@ Context* SafeTimer::add_event_at(SafeTimer::clock_t::time_point when, Context *c
   return callback;
 }
 
-Context* SafeTimer::add_event_at(ceph::real_clock::time_point when, Context *callback)
+template <class Mutex>
+Context* CommonSafeTimer<Mutex>::add_event_at(ceph::real_clock::time_point when, Context *callback)
 {
   ceph_assert(ceph_mutex_is_locked(lock));
   // convert from real_clock to mono_clock
@@ -165,7 +174,8 @@ Context* SafeTimer::add_event_at(ceph::real_clock::time_point when, Context *cal
   return add_event_at(mono_atime, callback);
 }
 
-bool SafeTimer::cancel_event(Context *callback)
+template <class Mutex>
+bool CommonSafeTimer<Mutex>::cancel_event(Context *callback)
 {
   ceph_assert(ceph_mutex_is_locked(lock));
   
@@ -183,7 +193,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));
@@ -197,7 +208,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 = "";
@@ -208,3 +220,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 033d1103a660dcf58a7e9052dc71fb754e6075c5..fb70bad15a847402242be628b8585fe27bef964d 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 e6891d281349a73c05b304f4ec2c88f8e750b6ac..0e3d40e7008923b728d3ae44e6b78eecb88ee95c 100644 (file)
@@ -59,7 +59,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 2fe67c408ede7d92a19d8d326f7dfe41d2c9d6df..729b962be4f2cc08377554f8e9cff4cb07354041 100644 (file)
@@ -13,6 +13,7 @@
 #include <string>
 #include <vector>
 
+#include "common/Timer.h"
 #include "common/ceph_mutex.h"
 #include "common/config_proxy.h"
 #include "common/event_socket.h"
@@ -35,8 +36,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 a0a20cd694579ce1a1a91118caf2943b5accb335..17174c438cd79be9cf0c31c40fe61d50984f661c 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 a29047e8e375d7364ff6556475ea9686feb3f24f..46325ea89a6034e73de05468166a97ea9fd6054f 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 be39b4ede0d5d13ee818b3ec82ab860d959f172f..3d6d667662e7921bc1628cee7d151e6952edff75 100644 (file)
@@ -11,6 +11,7 @@
 #include "include/Context.h"
 #include "common/RefCountedObj.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"
@@ -38,7 +39,6 @@
 struct D3nDataCache;
 
 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 bbd3fadff7cdd2ebbdb33bbb9bd85c85f188fed8..d1fc911f43cdf5dd5b500880796a2026941a745b 100644 (file)
@@ -18,13 +18,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 4cf4ca0d9a607b5c18ed3cef976433d9066d934a..4eb2b174161385dffa7f784b57a22b3105f21318 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; }