]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | #ifndef LIBRBD_TASK_FINISHER_H | |
4 | #define LIBRBD_TASK_FINISHER_H | |
5 | ||
9f95a23c | 6 | #include "include/common_fwd.h" |
7c673cae | 7 | #include "include/Context.h" |
11fdf7f2 | 8 | #include "common/ceph_context.h" |
7c673cae | 9 | #include "common/Finisher.h" |
9f95a23c | 10 | #include "common/ceph_mutex.h" |
7c673cae FG |
11 | #include "common/Timer.h" |
12 | #include <map> | |
13 | #include <utility> | |
14 | ||
7c673cae FG |
15 | |
16 | namespace librbd { | |
17 | ||
18 | struct TaskFinisherSingleton { | |
9f95a23c | 19 | ceph::mutex m_lock = ceph::make_mutex("librbd::TaskFinisher::m_lock"); |
7c673cae FG |
20 | SafeTimer *m_safe_timer; |
21 | Finisher *m_finisher; | |
22 | ||
9f95a23c | 23 | explicit TaskFinisherSingleton(CephContext *cct) { |
7c673cae FG |
24 | m_safe_timer = new SafeTimer(cct, m_lock, false); |
25 | m_safe_timer->init(); | |
26 | m_finisher = new Finisher(cct, "librbd::TaskFinisher::m_finisher", "taskfin_librbd"); | |
27 | m_finisher->start(); | |
28 | } | |
29 | virtual ~TaskFinisherSingleton() { | |
30 | { | |
9f95a23c | 31 | std::lock_guard l{m_lock}; |
7c673cae FG |
32 | m_safe_timer->shutdown(); |
33 | delete m_safe_timer; | |
34 | } | |
35 | m_finisher->wait_for_empty(); | |
36 | m_finisher->stop(); | |
37 | delete m_finisher; | |
38 | } | |
39 | }; | |
40 | ||
41 | ||
42 | template <typename Task> | |
43 | class TaskFinisher { | |
44 | public: | |
45 | TaskFinisher(CephContext &cct) : m_cct(cct) { | |
11fdf7f2 TL |
46 | auto& singleton = |
47 | cct.lookup_or_create_singleton_object<TaskFinisherSingleton>( | |
48 | "librbd::TaskFinisher::m_safe_timer", false, &cct); | |
49 | m_lock = &singleton.m_lock; | |
50 | m_safe_timer = singleton.m_safe_timer; | |
51 | m_finisher = singleton.m_finisher; | |
7c673cae FG |
52 | } |
53 | ||
54 | void cancel(const Task& task) { | |
9f95a23c | 55 | std::lock_guard l{*m_lock}; |
7c673cae FG |
56 | typename TaskContexts::iterator it = m_task_contexts.find(task); |
57 | if (it != m_task_contexts.end()) { | |
58 | delete it->second.first; | |
59 | m_safe_timer->cancel_event(it->second.second); | |
60 | m_task_contexts.erase(it); | |
61 | } | |
62 | } | |
63 | ||
64 | void cancel_all(Context *comp) { | |
65 | { | |
9f95a23c | 66 | std::lock_guard l{*m_lock}; |
7c673cae FG |
67 | for (typename TaskContexts::iterator it = m_task_contexts.begin(); |
68 | it != m_task_contexts.end(); ++it) { | |
69 | delete it->second.first; | |
70 | m_safe_timer->cancel_event(it->second.second); | |
71 | } | |
72 | m_task_contexts.clear(); | |
73 | } | |
74 | m_finisher->queue(comp); | |
75 | } | |
76 | ||
77 | bool add_event_after(const Task& task, double seconds, Context *ctx) { | |
9f95a23c | 78 | std::lock_guard l{*m_lock}; |
7c673cae FG |
79 | if (m_task_contexts.count(task) != 0) { |
80 | // task already scheduled on finisher or timer | |
81 | delete ctx; | |
82 | return false; | |
83 | } | |
84 | C_Task *timer_ctx = new C_Task(this, task); | |
85 | m_task_contexts[task] = std::make_pair(ctx, timer_ctx); | |
86 | ||
87 | m_safe_timer->add_event_after(seconds, timer_ctx); | |
88 | return true; | |
89 | } | |
90 | ||
91 | void queue(Context *ctx) { | |
92 | m_finisher->queue(ctx); | |
93 | } | |
94 | ||
95 | bool queue(const Task& task, Context *ctx) { | |
9f95a23c | 96 | std::lock_guard l{*m_lock}; |
7c673cae FG |
97 | typename TaskContexts::iterator it = m_task_contexts.find(task); |
98 | if (it != m_task_contexts.end()) { | |
99 | if (it->second.second != NULL) { | |
11fdf7f2 | 100 | ceph_assert(m_safe_timer->cancel_event(it->second.second)); |
7c673cae FG |
101 | delete it->second.first; |
102 | } else { | |
103 | // task already scheduled on the finisher | |
104 | delete ctx; | |
105 | return false; | |
106 | } | |
107 | } | |
108 | m_task_contexts[task] = std::make_pair(ctx, reinterpret_cast<Context *>(0)); | |
109 | ||
110 | m_finisher->queue(new C_Task(this, task)); | |
111 | return true; | |
112 | } | |
113 | ||
114 | private: | |
115 | class C_Task : public Context { | |
116 | public: | |
117 | C_Task(TaskFinisher *task_finisher, const Task& task) | |
118 | : m_task_finisher(task_finisher), m_task(task) | |
119 | { | |
120 | } | |
121 | protected: | |
122 | void finish(int r) override { | |
123 | m_task_finisher->complete(m_task); | |
124 | } | |
125 | private: | |
126 | TaskFinisher *m_task_finisher; | |
127 | Task m_task; | |
128 | }; | |
129 | ||
130 | CephContext &m_cct; | |
131 | ||
9f95a23c | 132 | ceph::mutex *m_lock; |
7c673cae FG |
133 | Finisher *m_finisher; |
134 | SafeTimer *m_safe_timer; | |
135 | ||
136 | typedef std::map<Task, std::pair<Context *, Context *> > TaskContexts; | |
137 | TaskContexts m_task_contexts; | |
138 | ||
139 | void complete(const Task& task) { | |
140 | Context *ctx = NULL; | |
141 | { | |
9f95a23c | 142 | std::lock_guard l{*m_lock}; |
7c673cae FG |
143 | typename TaskContexts::iterator it = m_task_contexts.find(task); |
144 | if (it != m_task_contexts.end()) { | |
145 | ctx = it->second.first; | |
146 | m_task_contexts.erase(it); | |
147 | } | |
148 | } | |
149 | ||
150 | if (ctx != NULL) { | |
151 | ctx->complete(0); | |
152 | } | |
153 | } | |
154 | }; | |
155 | ||
156 | } // namespace librbd | |
157 | ||
158 | #endif // LIBRBD_TASK_FINISHER |