2010-10-22 01:37:25 +00:00
|
|
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
2007-10-17 23:34:54 +00:00
|
|
|
// vim: ts=8 sw=2 smarttab
|
|
|
|
/*
|
|
|
|
* Ceph - scalable distributed file system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2010-10-22 01:37:25 +00:00
|
|
|
* License version 2.1, as published by the Free Software
|
2007-10-17 23:34:54 +00:00
|
|
|
* Foundation. See file COPYING.
|
2010-10-22 01:37:25 +00:00
|
|
|
*
|
2007-10-17 23:34:54 +00:00
|
|
|
*/
|
|
|
|
|
2010-06-12 13:04:11 +00:00
|
|
|
#ifndef CEPH_TIMER_H
|
|
|
|
#define CEPH_TIMER_H
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
#include "Cond.h"
|
2010-10-22 01:37:25 +00:00
|
|
|
#include "Mutex.h"
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2011-06-15 20:47:46 +00:00
|
|
|
class CephContext;
|
2010-10-22 01:37:25 +00:00
|
|
|
class Context;
|
2010-11-12 00:38:02 +00:00
|
|
|
class SafeTimerThread;
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2010-11-12 00:38:02 +00:00
|
|
|
class SafeTimer
|
2010-10-22 01:37:25 +00:00
|
|
|
{
|
2010-11-12 00:38:02 +00:00
|
|
|
// This class isn't supposed to be copied
|
|
|
|
SafeTimer(const SafeTimer &rhs);
|
|
|
|
SafeTimer& operator=(const SafeTimer &rhs);
|
2010-10-22 01:37:25 +00:00
|
|
|
|
2011-06-15 20:47:46 +00:00
|
|
|
CephContext *cct;
|
2010-11-12 00:38:02 +00:00
|
|
|
Mutex& lock;
|
|
|
|
Cond cond;
|
2012-09-14 17:30:17 +00:00
|
|
|
bool safe_callbacks;
|
2010-10-22 01:37:25 +00:00
|
|
|
|
2010-11-12 00:38:02 +00:00
|
|
|
friend class SafeTimerThread;
|
|
|
|
SafeTimerThread *thread;
|
2010-10-22 01:37:25 +00:00
|
|
|
|
2010-11-12 00:38:02 +00:00
|
|
|
void timer_thread();
|
|
|
|
void _shutdown();
|
2010-11-03 22:36:09 +00:00
|
|
|
|
2010-11-12 00:38:02 +00:00
|
|
|
std::multimap<utime_t, Context*> schedule;
|
|
|
|
std::map<Context*, std::multimap<utime_t, Context*>::iterator> events;
|
|
|
|
bool stopping;
|
2010-10-22 01:37:25 +00:00
|
|
|
|
2010-11-12 00:38:02 +00:00
|
|
|
void dump(const char *caller = 0) const;
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
public:
|
2012-09-14 17:30:17 +00:00
|
|
|
/* 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);
|
2007-10-17 23:34:54 +00:00
|
|
|
~SafeTimer();
|
|
|
|
|
2010-10-22 01:37:25 +00:00
|
|
|
/* 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. */
|
2010-11-15 21:23:42 +00:00
|
|
|
void init();
|
2010-10-22 01:37:25 +00:00
|
|
|
void shutdown();
|
|
|
|
|
|
|
|
/* Schedule an event in the future
|
|
|
|
* Call with the event_lock LOCKED */
|
|
|
|
void add_event_after(double seconds, Context *callback);
|
|
|
|
void 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();
|
|
|
|
|
2007-10-17 23:34:54 +00:00
|
|
|
};
|
|
|
|
#endif
|