X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Fcommon%2FTimer.h;fp=src%2Fceph%2Fsrc%2Fcommon%2FTimer.h;h=8fd478a9934999d4bbee05ef8bf26a44f1113178;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/common/Timer.h b/src/ceph/src/common/Timer.h new file mode 100644 index 0000000..8fd478a --- /dev/null +++ b/src/ceph/src/common/Timer.h @@ -0,0 +1,94 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_TIMER_H +#define CEPH_TIMER_H + +#include "Cond.h" +#include "Mutex.h" + +class CephContext; +class Context; +class SafeTimerThread; + +class SafeTimer +{ + CephContext *cct; + Mutex& lock; + Cond cond; + bool safe_callbacks; + + friend class SafeTimerThread; + SafeTimerThread *thread; + + void timer_thread(); + void _shutdown(); + + std::multimap schedule; + std::map::iterator> events; + bool stopping; + + void dump(const char *caller = 0) const; + +public: + // This class isn't supposed to be copied + SafeTimer(const SafeTimer&) = delete; + SafeTimer& operator=(const SafeTimer&) = delete; + + /* Safe callbacks determines whether callbacks are called with the lock + * held. + * + * safe_callbacks = true (default option) guarantees that a cancelled + * event's callback will never be called. + * + * Under some circumstances, holding the lock can cause lock cycles. + * If you are able to relax requirements on cancelled callbacks, then + * setting safe_callbacks = false eliminates the lock cycle issue. + * */ + SafeTimer(CephContext *cct, Mutex &l, bool safe_callbacks=true); + ~SafeTimer(); + + /* Call with the event_lock UNLOCKED. + * + * Cancel all events and stop the timer thread. + * + * If there are any events that still have to run, they will need to take + * the event_lock first. */ + void init(); + void shutdown(); + + /* Schedule an event in the future + * Call with the event_lock LOCKED */ + Context* add_event_after(double seconds, Context *callback); + Context* add_event_at(utime_t when, Context *callback); + + /* Cancel an event. + * Call with the event_lock LOCKED + * + * Returns true if the callback was cancelled. + * Returns false if you never addded the callback in the first place. + */ + bool cancel_event(Context *callback); + + /* Cancel all events. + * Call with the event_lock LOCKED + * + * When this function returns, all events have been cancelled, and there are no + * more in progress. + */ + void cancel_all_events(); + +}; + +#endif