]> git.proxmox.com Git - ceph.git/blob - ceph/src/msg/QueueStrategy.cc
update sources to v12.1.1
[ceph.git] / ceph / src / msg / QueueStrategy.cc
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) 2014 CohortFS, LLC
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 #include <string>
15 #include "QueueStrategy.h"
16 #define dout_subsys ceph_subsys_ms
17 #include "common/debug.h"
18 #include "common/backport14.h"
19
20 QueueStrategy::QueueStrategy(int _n_threads)
21 : lock("QueueStrategy::lock"),
22 n_threads(_n_threads),
23 stop(false),
24 mqueue(),
25 disp_threads()
26 {
27 }
28
29 void QueueStrategy::ds_dispatch(Message *m) {
30 msgr->ms_fast_preprocess(m);
31 if (msgr->ms_can_fast_dispatch(m)) {
32 msgr->ms_fast_dispatch(m);
33 return;
34 }
35 lock.Lock();
36 mqueue.push_back(*m);
37 if (disp_threads.size()) {
38 if (! disp_threads.empty()) {
39 QSThread *thrd = &disp_threads.front();
40 disp_threads.pop_front();
41 thrd->cond.Signal();
42 }
43 }
44 lock.Unlock();
45 }
46
47 void QueueStrategy::entry(QSThread *thrd)
48 {
49 Message *m = NULL;
50 for (;;) {
51 lock.Lock();
52 for (;;) {
53 if (! mqueue.empty()) {
54 m = &(mqueue.front());
55 mqueue.pop_front();
56 break;
57 }
58 m = NULL;
59 if (stop)
60 break;
61 disp_threads.push_front(*thrd);
62 thrd->cond.Wait(lock);
63 }
64 lock.Unlock();
65 if (stop) {
66 if (!m) break;
67 m->put();
68 continue;
69 }
70 get_messenger()->ms_deliver_dispatch(m);
71 }
72 }
73
74 void QueueStrategy::shutdown()
75 {
76 QSThread *thrd;
77 lock.Lock();
78 stop = true;
79 while (disp_threads.size()) {
80 thrd = &(disp_threads.front());
81 disp_threads.pop_front();
82 thrd->cond.Signal();
83 }
84 lock.Unlock();
85 }
86
87 void QueueStrategy::wait()
88 {
89 lock.Lock();
90 assert(stop);
91 for (auto& thread : threads) {
92 lock.Unlock();
93
94 // join outside of lock
95 thread->join();
96
97 lock.Lock();
98 }
99 lock.Unlock();
100 }
101
102 void QueueStrategy::start()
103 {
104 assert(!stop);
105 lock.Lock();
106 threads.reserve(n_threads);
107 for (int ix = 0; ix < n_threads; ++ix) {
108 string thread_name = "ms_xio_qs_";
109 thread_name.append(std::to_string(ix));
110 auto thrd = ceph::make_unique<QSThread>(this);
111 thrd->create(thread_name.c_str());
112 threads.emplace_back(std::move(thrd));
113 }
114 lock.Unlock();
115 }