]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/Timer.h
078a172b25b187e4b4747f9625e3f84d55ff06f4
[ceph.git] / ceph / src / common / Timer.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef CEPH_TIMER_H
16 #define CEPH_TIMER_H
17
18 #include "Cond.h"
19 #include "Mutex.h"
20
21 class CephContext;
22 class Context;
23 class SafeTimerThread;
24
25 class SafeTimer
26 {
27 CephContext *cct;
28 Mutex& lock;
29 Cond cond;
30 bool safe_callbacks;
31
32 friend class SafeTimerThread;
33 SafeTimerThread *thread;
34
35 void timer_thread();
36 void _shutdown();
37
38 std::multimap<utime_t, Context*> schedule;
39 std::map<Context*, std::multimap<utime_t, Context*>::iterator> events;
40 bool stopping;
41
42 void dump(const char *caller = 0) const;
43
44 public:
45 // This class isn't supposed to be copied
46 SafeTimer(const SafeTimer&) = delete;
47 SafeTimer& operator=(const SafeTimer&) = delete;
48
49 /* Safe callbacks determines whether callbacks are called with the lock
50 * held.
51 *
52 * safe_callbacks = true (default option) guarantees that a cancelled
53 * event's callback will never be called.
54 *
55 * Under some circumstances, holding the lock can cause lock cycles.
56 * If you are able to relax requirements on cancelled callbacks, then
57 * setting safe_callbacks = false eliminates the lock cycle issue.
58 * */
59 SafeTimer(CephContext *cct, Mutex &l, bool safe_callbacks=true);
60 ~SafeTimer();
61
62 /* Call with the event_lock UNLOCKED.
63 *
64 * Cancel all events and stop the timer thread.
65 *
66 * If there are any events that still have to run, they will need to take
67 * the event_lock first. */
68 void init();
69 void shutdown();
70
71 /* Schedule an event in the future
72 * Call with the event_lock LOCKED */
73 void add_event_after(double seconds, Context *callback);
74 void add_event_at(utime_t when, Context *callback);
75
76 /* Cancel an event.
77 * Call with the event_lock LOCKED
78 *
79 * Returns true if the callback was cancelled.
80 * Returns false if you never addded the callback in the first place.
81 */
82 bool cancel_event(Context *callback);
83
84 /* Cancel all events.
85 * Call with the event_lock LOCKED
86 *
87 * When this function returns, all events have been cancelled, and there are no
88 * more in progress.
89 */
90 void cancel_all_events();
91
92 };
93
94 #endif