]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/Timer.h
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / common / Timer.h
CommitLineData
7c673cae
FG
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
9f95a23c
TL
18#include <map>
19#include "include/common_fwd.h"
20#include "ceph_time.h"
21#include "ceph_mutex.h"
a4b75251
TL
22#include "fair_mutex.h"
23#include <condition_variable>
7c673cae 24
7c673cae 25class Context;
7c673cae 26
a4b75251
TL
27template <class Mutex> class CommonSafeTimerThread;
28
29template <class Mutex>
30class CommonSafeTimer
7c673cae
FG
31{
32 CephContext *cct;
a4b75251
TL
33 Mutex& lock;
34 std::condition_variable_any cond;
7c673cae
FG
35 bool safe_callbacks;
36
a4b75251
TL
37 friend class CommonSafeTimerThread<Mutex>;
38 class CommonSafeTimerThread<Mutex> *thread;
7c673cae
FG
39
40 void timer_thread();
41 void _shutdown();
42
20effc67 43 using clock_t = ceph::mono_clock;
9f95a23c
TL
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;
7c673cae
FG
48 bool stopping;
49
50 void dump(const char *caller = 0) const;
51
52public:
53 // This class isn't supposed to be copied
a4b75251
TL
54 CommonSafeTimer(const CommonSafeTimer&) = delete;
55 CommonSafeTimer& operator=(const CommonSafeTimer&) = delete;
7c673cae
FG
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 * */
a4b75251
TL
67 CommonSafeTimer(CephContext *cct, Mutex &l, bool safe_callbacks=true);
68 virtual ~CommonSafeTimer();
7c673cae
FG
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 */
f67539c2 81 Context* add_event_after(ceph::timespan duration, Context *callback);
3efd9988 82 Context* add_event_after(double seconds, Context *callback);
9f95a23c 83 Context* add_event_at(clock_t::time_point when, Context *callback);
20effc67 84 Context* add_event_at(ceph::real_clock::time_point when, Context *callback);
7c673cae
FG
85 /* Cancel an event.
86 * Call with the event_lock LOCKED
87 *
88 * Returns true if the callback was cancelled.
11fdf7f2 89 * Returns false if you never added the callback in the first place.
7c673cae
FG
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
a4b75251
TL
103extern template class CommonSafeTimer<ceph::mutex>;
104extern template class CommonSafeTimer<ceph::fair_mutex>;
105using SafeTimer = class CommonSafeTimer<ceph::mutex>;
106
7c673cae 107#endif