From: Xiubo Li Date: Mon, 2 Aug 2021 06:20:52 +0000 (+0800) Subject: common/Timer: make SafeTimer a template X-Git-Tag: v16.2.7~100^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d38d68f39a4a757e31847e710649f7e3c31ba0d8;p=ceph.git common/Timer: make SafeTimer a template Signed-off-by: Xiubo Li (cherry picked from commit 215b12ae0abc781430f3e5d30991b7f5055aeb95) Conflicts: src/common/Timer.cc src/librbd/ImageCtx.h --- diff --git a/src/common/Timer.cc b/src/common/Timer.cc index eab46661c99e..b96d9d2ff719 100644 --- a/src/common/Timer.cc +++ b/src/common/Timer.cc @@ -24,18 +24,19 @@ using std::pair; using ceph::operator <<; -class SafeTimerThread : public Thread { - SafeTimer *parent; +template +class CommonSafeTimerThread : public Thread { + CommonSafeTimer *parent; public: - explicit SafeTimerThread(SafeTimer *s) : parent(s) {} + explicit CommonSafeTimerThread(CommonSafeTimer *s) : parent(s) {} void *entry() override { parent->timer_thread(); return NULL; } }; - -SafeTimer::SafeTimer(CephContext *cct_, ceph::mutex &l, bool safe_callbacks) +template +CommonSafeTimer::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 +CommonSafeTimer::~CommonSafeTimer() { ceph_assert(thread == NULL); } -void SafeTimer::init() +template +void CommonSafeTimer::init() { ldout(cct,10) << "init" << dendl; - thread = new SafeTimerThread(this); + thread = new CommonSafeTimerThread(this); thread->create("safe_timer"); } -void SafeTimer::shutdown() +template +void CommonSafeTimer::shutdown() { ldout(cct,10) << "shutdown" << dendl; if (thread) { @@ -71,7 +75,8 @@ void SafeTimer::shutdown() } } -void SafeTimer::timer_thread() +template +void CommonSafeTimer::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 +Context* CommonSafeTimer::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 +Context* CommonSafeTimer::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 +Context* CommonSafeTimer::add_event_at(CommonSafeTimer::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 +bool CommonSafeTimer::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 +void CommonSafeTimer::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 +void CommonSafeTimer::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; +template class CommonSafeTimer; diff --git a/src/common/Timer.h b/src/common/Timer.h index f543be68a766..f22956a34761 100644 --- a/src/common/Timer.h +++ b/src/common/Timer.h @@ -19,19 +19,23 @@ #include "include/common_fwd.h" #include "ceph_time.h" #include "ceph_mutex.h" +#include "fair_mutex.h" +#include class Context; -class SafeTimerThread; -class SafeTimer +template class CommonSafeTimerThread; + +template +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; + class CommonSafeTimerThread *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; +extern template class CommonSafeTimer; +using SafeTimer = class CommonSafeTimer; + #endif diff --git a/src/journal/JournalMetadata.h b/src/journal/JournalMetadata.h index 1b6911daecba..071456f56ecc 100644 --- a/src/journal/JournalMetadata.h +++ b/src/journal/JournalMetadata.h @@ -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 #include "include/ceph_assert.h" -class SafeTimer; - namespace journal { class JournalMetadata : public RefCountedObject, boost::noncopyable { diff --git a/src/journal/JournalPlayer.h b/src/journal/JournalPlayer.h index f2ab14d7b43c..a71117a836a1 100644 --- a/src/journal/JournalPlayer.h +++ b/src/journal/JournalPlayer.h @@ -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 #include -class SafeTimer; - namespace journal { class CacheManagerHandler; diff --git a/src/journal/JournalRecorder.h b/src/journal/JournalRecorder.h index 680dbcdfead5..9d8ea6c10f3d 100644 --- a/src/journal/JournalRecorder.h +++ b/src/journal/JournalRecorder.h @@ -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 #include -class SafeTimer; - namespace journal { class JournalRecorder { diff --git a/src/journal/Journaler.h b/src/journal/Journaler.h index fe44401848ac..f42513c5d67f 100644 --- a/src/journal/Journaler.h +++ b/src/journal/Journaler.h @@ -11,13 +11,13 @@ #include "journal/Future.h" #include "journal/JournalMetadataListener.h" #include "cls/journal/cls_journal_types.h" +#include "common/Timer.h" #include #include #include #include "include/ceph_assert.h" class ContextWQ; -class SafeTimer; class ThreadPool; namespace journal { diff --git a/src/journal/ObjectPlayer.h b/src/journal/ObjectPlayer.h index 41641dd150ac..b9446252a274 100644 --- a/src/journal/ObjectPlayer.h +++ b/src/journal/ObjectPlayer.h @@ -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 @@ -16,8 +17,6 @@ #include #include "include/ceph_assert.h" -class SafeTimer; - namespace journal { class ObjectPlayer : public RefCountedObject { diff --git a/src/journal/ObjectRecorder.h b/src/journal/ObjectRecorder.h index 7281879fcf72..5c5f88c86db9 100644 --- a/src/journal/ObjectRecorder.h +++ b/src/journal/ObjectRecorder.h @@ -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 #include @@ -17,8 +18,6 @@ #include #include "include/ceph_assert.h" -class SafeTimer; - namespace journal { class ObjectRecorder; diff --git a/src/librbd/ImageCtx.cc b/src/librbd/ImageCtx.cc index ed836ed8e4e4..2051ebef9bda 100644 --- a/src/librbd/ImageCtx.cc +++ b/src/librbd/ImageCtx.cc @@ -58,7 +58,7 @@ namespace librbd { namespace { -class SafeTimerSingleton : public SafeTimer { +class SafeTimerSingleton : public CommonSafeTimer { public: ceph::mutex lock = ceph::make_mutex("librbd::SafeTimerSingleton::lock"); diff --git a/src/librbd/ImageCtx.h b/src/librbd/ImageCtx.h index 31d9a67547d6..043b26efe300 100644 --- a/src/librbd/ImageCtx.h +++ b/src/librbd/ImageCtx.h @@ -14,6 +14,7 @@ #include #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 #include -class SafeTimer; - namespace neorados { class IOContext; class RADOS; diff --git a/src/librbd/Journal.h b/src/librbd/Journal.h index 64ea13fe23ac..406c0e34cbf0 100644 --- a/src/librbd/Journal.h +++ b/src/librbd/Journal.h @@ -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 class ContextWQ; -class SafeTimer; namespace journal { class Journaler; } namespace librbd { diff --git a/src/librbd/cache/pwl/AbstractWriteLog.h b/src/librbd/cache/pwl/AbstractWriteLog.h index 56ecd6c1c209..7b11542d65a8 100644 --- a/src/librbd/cache/pwl/AbstractWriteLog.h +++ b/src/librbd/cache/pwl/AbstractWriteLog.h @@ -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 class Context; -class SafeTimer; namespace librbd { diff --git a/src/librbd/cache/pwl/rwl/WriteLog.h b/src/librbd/cache/pwl/rwl/WriteLog.h index 7aea8573c917..d5c132e44630 100644 --- a/src/librbd/cache/pwl/rwl/WriteLog.h +++ b/src/librbd/cache/pwl/rwl/WriteLog.h @@ -7,6 +7,7 @@ #include #include #include +#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 { diff --git a/src/librbd/image/RemoveRequest.h b/src/librbd/image/RemoveRequest.h index 79512c932ce2..b03f8fc7c21c 100644 --- a/src/librbd/image/RemoveRequest.h +++ b/src/librbd/image/RemoveRequest.h @@ -7,11 +7,11 @@ #include "include/rados/librados.hpp" #include "librbd/ImageCtx.h" #include "librbd/image/TypeTraits.h" +#include "common/Timer.h" #include class Context; -class SafeTimer; namespace librbd { diff --git a/src/librbd/io/TypeTraits.h b/src/librbd/io/TypeTraits.h index 34703158a3e6..2f3a6b7efcf2 100644 --- a/src/librbd/io/TypeTraits.h +++ b/src/librbd/io/TypeTraits.h @@ -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 { diff --git a/src/librbd/journal/CreateRequest.h b/src/librbd/journal/CreateRequest.h index 8a4e40f88c44..6fab409c4302 100644 --- a/src/librbd/journal/CreateRequest.h +++ b/src/librbd/journal/CreateRequest.h @@ -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; diff --git a/src/librbd/journal/RemoveRequest.h b/src/librbd/journal/RemoveRequest.h index 13dff87a08e7..14b1c4dc59e6 100644 --- a/src/librbd/journal/RemoveRequest.h +++ b/src/librbd/journal/RemoveRequest.h @@ -11,13 +11,13 @@ #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; diff --git a/src/librbd/journal/ResetRequest.h b/src/librbd/journal/ResetRequest.h index 44f5ac8a6bd7..f9331f644649 100644 --- a/src/librbd/journal/ResetRequest.h +++ b/src/librbd/journal/ResetRequest.h @@ -9,11 +9,11 @@ #include "include/rados/librados.hpp" #include "include/rbd/librbd.hpp" #include "librbd/journal/TypeTraits.h" +#include "common/Timer.h" #include class Context; class ContextWQ; -class SafeTimer; namespace journal { class Journaler; } diff --git a/src/librbd/mirror/snapshot/PromoteRequest.h b/src/librbd/mirror/snapshot/PromoteRequest.h index 9fdd5ed71a5d..1d9a862a0afb 100644 --- a/src/librbd/mirror/snapshot/PromoteRequest.h +++ b/src/librbd/mirror/snapshot/PromoteRequest.h @@ -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 #include -class SafeTimer; struct Context; namespace librbd { diff --git a/src/librbd/plugin/Api.h b/src/librbd/plugin/Api.h index 2d55c17c19fe..04f77e5c3e76 100644 --- a/src/librbd/plugin/Api.h +++ b/src/librbd/plugin/Api.h @@ -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 { diff --git a/src/rgw/rgw_rados.h b/src/rgw/rgw_rados.h index 4542c4a1f917..2c576bc8e6c4 100644 --- a/src/rgw/rgw_rados.h +++ b/src/rgw/rgw_rados.h @@ -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; diff --git a/src/tools/cephfs_mirror/Mirror.cc b/src/tools/cephfs_mirror/Mirror.cc index efaa27106e51..0f01495fa864 100644 --- a/src/tools/cephfs_mirror/Mirror.cc +++ b/src/tools/cephfs_mirror/Mirror.cc @@ -27,7 +27,7 @@ namespace { const std::string SERVICE_DAEMON_MIRROR_ENABLE_FAILED_KEY("mirroring_failed"); -class SafeTimerSingleton : public SafeTimer { +class SafeTimerSingleton : public CommonSafeTimer { public: ceph::mutex timer_lock = ceph::make_mutex("cephfs::mirror::timer_lock"); diff --git a/src/tools/cephfs_mirror/ServiceDaemon.h b/src/tools/cephfs_mirror/ServiceDaemon.h index 393c7d5cffd6..83eee286d41e 100644 --- a/src/tools/cephfs_mirror/ServiceDaemon.h +++ b/src/tools/cephfs_mirror/ServiceDaemon.h @@ -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 { diff --git a/src/tools/immutable_object_cache/ObjectCacheStore.cc b/src/tools/immutable_object_cache/ObjectCacheStore.cc index 483687ffe5fc..1b1eef1e3836 100644 --- a/src/tools/immutable_object_cache/ObjectCacheStore.cc +++ b/src/tools/immutable_object_cache/ObjectCacheStore.cc @@ -23,13 +23,13 @@ namespace immutable_obj_cache { namespace { -class SafeTimerSingleton : public SafeTimer { +class SafeTimerSingleton : public CommonSafeTimer { 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() { diff --git a/src/tools/immutable_object_cache/ObjectCacheStore.h b/src/tools/immutable_object_cache/ObjectCacheStore.h index 607921320aef..51e5a77b855d 100644 --- a/src/tools/immutable_object_cache/ObjectCacheStore.h +++ b/src/tools/immutable_object_cache/ObjectCacheStore.h @@ -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" diff --git a/src/tools/rbd_mirror/ImageDeleter.h b/src/tools/rbd_mirror/ImageDeleter.h index d9f4e14a7e2c..5fe79496bc99 100644 --- a/src/tools/rbd_mirror/ImageDeleter.h +++ b/src/tools/rbd_mirror/ImageDeleter.h @@ -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 @@ -29,7 +30,6 @@ class AdminSocketHook; class Context; -class SafeTimer; namespace librbd { struct ImageCtx; namespace asio { struct ContextWQ; } diff --git a/src/tools/rbd_mirror/Threads.h b/src/tools/rbd_mirror/Threads.h index 91c923ab4a04..35c0b0f1cfa3 100644 --- a/src/tools/rbd_mirror/Threads.h +++ b/src/tools/rbd_mirror/Threads.h @@ -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 -class SafeTimer; class ThreadPool; namespace librbd { diff --git a/src/tools/rbd_mirror/image_replayer/BootstrapRequest.h b/src/tools/rbd_mirror/image_replayer/BootstrapRequest.h index 3d17ae48bc2f..f5bb8dd8a16c 100644 --- a/src/tools/rbd_mirror/image_replayer/BootstrapRequest.h +++ b/src/tools/rbd_mirror/image_replayer/BootstrapRequest.h @@ -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 class Context; -class SafeTimer; namespace journal { class CacheManagerHandler; } namespace librbd { class ImageCtx; }