1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
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>
27 template <class Mutex
> class CommonSafeTimerThread
;
29 template <class Mutex
>
34 std::condition_variable_any cond
;
37 friend class CommonSafeTimerThread
<Mutex
>;
38 class CommonSafeTimerThread
<Mutex
> *thread
;
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
;
50 void dump(const char *caller
= 0) const;
53 // This class isn't supposed to be copied
54 CommonSafeTimer(const CommonSafeTimer
&) = delete;
55 CommonSafeTimer
& operator=(const CommonSafeTimer
&) = delete;
57 /* Safe callbacks determines whether callbacks are called with the lock
60 * safe_callbacks = true (default option) guarantees that a cancelled
61 * event's callback will never be called.
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.
67 CommonSafeTimer(CephContext
*cct
, Mutex
&l
, bool safe_callbacks
=true);
68 virtual ~CommonSafeTimer();
70 /* Call with the event_lock UNLOCKED.
72 * Cancel all events and stop the timer thread.
74 * If there are any events that still have to run, they will need to take
75 * the event_lock first. */
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
);
86 * Call with the event_lock LOCKED
88 * Returns true if the callback was cancelled.
89 * Returns false if you never added the callback in the first place.
91 bool cancel_event(Context
*callback
);
94 * Call with the event_lock LOCKED
96 * When this function returns, all events have been cancelled, and there are no
99 void cancel_all_events();
103 extern template class CommonSafeTimer
<ceph::mutex
>;
104 extern template class CommonSafeTimer
<ceph::fair_mutex
>;
105 using SafeTimer
= class CommonSafeTimer
<ceph::mutex
>;