]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/Finisher.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #define dout_subsys ceph_subsys_finisher
8 #define dout_prefix *_dout << "finisher(" << this << ") "
10 void Finisher::start()
12 ldout(cct
, 10) << __func__
<< dendl
;
13 finisher_thread
.create(thread_name
.c_str());
18 ldout(cct
, 10) << __func__
<< dendl
;
21 // we don't have any new work to do, but we want the worker to wake up anyway
22 // to process the stop condition.
23 finisher_cond
.notify_all();
24 finisher_lock
.unlock();
25 finisher_thread
.join(); // wait until the worker exits completely
26 ldout(cct
, 10) << __func__
<< " finish" << dendl
;
29 void Finisher::wait_for_empty()
31 std::unique_lock
ul(finisher_lock
);
32 while (!finisher_queue
.empty() || finisher_running
) {
33 ldout(cct
, 10) << "wait_for_empty waiting" << dendl
;
34 finisher_empty_wait
= true;
35 finisher_empty_cond
.wait(ul
);
37 ldout(cct
, 10) << "wait_for_empty empty" << dendl
;
38 finisher_empty_wait
= false;
41 void *Finisher::finisher_thread_entry()
43 std::unique_lock
ul(finisher_lock
);
44 ldout(cct
, 10) << "finisher_thread start" << dendl
;
48 while (!finisher_stop
) {
49 /// Every time we are woken up, we process the queue until it is empty.
50 while (!finisher_queue
.empty()) {
51 // To reduce lock contention, we swap out the queue to process.
52 // This way other threads can submit new contexts to complete
53 // while we are working.
54 in_progress_queue
.swap(finisher_queue
);
55 finisher_running
= true;
57 ldout(cct
, 10) << "finisher_thread doing " << in_progress_queue
<< dendl
;
60 start
= ceph_clock_now();
61 count
= in_progress_queue
.size();
64 // Now actually process the contexts.
65 for (auto p
: in_progress_queue
) {
66 p
.first
->complete(p
.second
);
68 ldout(cct
, 10) << "finisher_thread done with " << in_progress_queue
70 in_progress_queue
.clear();
72 logger
->dec(l_finisher_queue_len
, count
);
73 logger
->tinc(l_finisher_complete_lat
, ceph_clock_now() - start
);
77 finisher_running
= false;
79 ldout(cct
, 10) << "finisher_thread empty" << dendl
;
80 if (unlikely(finisher_empty_wait
))
81 finisher_empty_cond
.notify_all();
85 ldout(cct
, 10) << "finisher_thread sleeping" << dendl
;
86 finisher_cond
.wait(ul
);
88 // If we are exiting, we signal the thread waiting in stop(),
89 // otherwise it would never unblock
90 finisher_empty_cond
.notify_all();
92 ldout(cct
, 10) << "finisher_thread stop" << dendl
;
93 finisher_stop
= false;