common/Timer: make SafeTimer a template

Signed-off-by: Xiubo Li <xiubli@redhat.com>
This commit is contained in:
Xiubo Li 2021-08-02 14:20:52 +08:00
parent 98ace572a1
commit 215b12ae0a
28 changed files with 77 additions and 61 deletions

View 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>;

View File

@ -19,19 +19,23 @@
#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

View 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 {

View 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;

View 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 {

View File

@ -11,13 +11,13 @@
#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 {

View 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 {

View 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;

View 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");

View 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;

View 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 {

View 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 {

View 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 {

View 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 {

View 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 {

View 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;

View File

@ -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;

View 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; }

View 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 {

View 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 {

View 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;

View 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");

View 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 {

View 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() {

View 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"

View 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; }

View 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 {

View 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; }