]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/Timer.h
import quincy beta 17.1.0
[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 <map>
19 #include "include/common_fwd.h"
20 #include "ceph_time.h"
21 #include "ceph_mutex.h"
22 #include "fair_mutex.h"
23 #include <condition_variable>
24
25 class Context;
26
27 template <class Mutex> class CommonSafeTimerThread;
28
29 template <class Mutex>
30 class CommonSafeTimer
31 {
32 CephContext *cct;
33 Mutex& lock;
34 std::condition_variable_any cond;
35 bool safe_callbacks;
36
37 friend class CommonSafeTimerThread<Mutex>;
38 class CommonSafeTimerThread<Mutex> *thread;
39
40 void timer_thread();
41 void _shutdown();
42
43 using clock_t = ceph::mono_clock;
44 using scheduled_map_t = std::multimap<clock_t::time_point, Context*>;
45 scheduled_map_t schedule;
46 using event_lookup_map_t = std::map<Context*, scheduled_map_t::iterator>;
47 event_lookup_map_t events;
48 bool stopping;
49
50 void dump(const char *caller = 0) const;
51
52 public:
53 // This class isn't supposed to be copied
54 CommonSafeTimer(const CommonSafeTimer&) = delete;
55 CommonSafeTimer& operator=(const CommonSafeTimer&) = delete;
56
57 /* Safe callbacks determines whether callbacks are called with the lock
58 * held.
59 *
60 * safe_callbacks = true (default option) guarantees that a cancelled
61 * event's callback will never be called.
62 *
63 * Under some circumstances, holding the lock can cause lock cycles.
64 * If you are able to relax requirements on cancelled callbacks, then
65 * setting safe_callbacks = false eliminates the lock cycle issue.
66 * */
67 CommonSafeTimer(CephContext *cct, Mutex &l, bool safe_callbacks=true);
68 virtual ~CommonSafeTimer();
69
70 /* Call with the event_lock UNLOCKED.
71 *
72 * Cancel all events and stop the timer thread.
73 *
74 * If there are any events that still have to run, they will need to take
75 * the event_lock first. */
76 void init();
77 void shutdown();
78
79 /* Schedule an event in the future
80 * Call with the event_lock LOCKED */
81 Context* add_event_after(ceph::timespan duration, Context *callback);
82 Context* add_event_after(double seconds, Context *callback);
83 Context* add_event_at(clock_t::time_point when, Context *callback);
84 Context* add_event_at(ceph::real_clock::time_point when, Context *callback);
85 /* Cancel an event.
86 * Call with the event_lock LOCKED
87 *
88 * Returns true if the callback was cancelled.
89 * Returns false if you never added the callback in the first place.
90 */
91 bool cancel_event(Context *callback);
92
93 /* Cancel all events.
94 * Call with the event_lock LOCKED
95 *
96 * When this function returns, all events have been cancelled, and there are no
97 * more in progress.
98 */
99 void cancel_all_events();
100
101 };
102
103 extern template class CommonSafeTimer<ceph::mutex>;
104 extern template class CommonSafeTimer<ceph::fair_mutex>;
105 using SafeTimer = class CommonSafeTimer<ceph::mutex>;
106
107 #endif