diff --git a/src/common/Timer.cc b/src/common/Timer.cc index 17547962086..48c79d613d0 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,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 +Context* CommonSafeTimer::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 +bool CommonSafeTimer::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 +void CommonSafeTimer::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 +void CommonSafeTimer::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; +template class CommonSafeTimer; diff --git a/src/common/Timer.h b/src/common/Timer.h index 033d1103a66..fb70bad15a8 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 1b6911daecb..071456f56ec 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 f2ab14d7b43..a71117a836a 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 680dbcdfead..9d8ea6c10f3 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 fe44401848a..f42513c5d67 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 41641dd150a..b9446252a27 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 7281879fcf7..5c5f88c86db 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 e6891d28134..0e3d40e7008 100644 --- a/src/librbd/ImageCtx.cc +++ b/src/librbd/ImageCtx.cc @@ -59,7 +59,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 2fe67c408ed..729b962be4f 100644 --- a/src/librbd/ImageCtx.h +++ b/src/librbd/ImageCtx.h @@ -13,6 +13,7 @@ #include #include +#include "common/Timer.h" #include "common/ceph_mutex.h" #include "common/config_proxy.h" #include "common/event_socket.h" @@ -35,8 +36,6 @@ #include #include -class SafeTimer; - namespace neorados { class IOContext; class RADOS; diff --git a/src/librbd/Journal.h b/src/librbd/Journal.h index 64ea13fe23a..406c0e34cbf 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 a0a20cd6945..17174c438cd 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 a29047e8e37..46325ea89a6 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 79512c932ce..b03f8fc7c21 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 34703158a3e..2f3a6b7efcf 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 8a4e40f88c4..6fab409c430 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 13dff87a08e..14b1c4dc59e 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 44f5ac8a6bd..f9331f64464 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 9fdd5ed71a5..1d9a862a0af 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 2d55c17c19f..04f77e5c3e7 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 be39b4ede0d..3d6d667662e 100644 --- a/src/rgw/rgw_rados.h +++ b/src/rgw/rgw_rados.h @@ -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; diff --git a/src/tools/cephfs_mirror/Mirror.cc b/src/tools/cephfs_mirror/Mirror.cc index efaa27106e5..0f01495fa86 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 393c7d5cffd..83eee286d41 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 bbd3fadff7c..d1fc911f43c 100644 --- a/src/tools/immutable_object_cache/ObjectCacheStore.cc +++ b/src/tools/immutable_object_cache/ObjectCacheStore.cc @@ -18,13 +18,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 4cf4ca0d9a6..4eb2b174161 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 d9f4e14a7e2..5fe79496bc9 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 91c923ab4a0..35c0b0f1cfa 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 3d17ae48bc2..f5bb8dd8a16 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; }