]> git.proxmox.com Git - ceph.git/blob - ceph/src/crimson/thread/Throttle.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / crimson / thread / Throttle.cc
1 #include "Throttle.h"
2
3 namespace ceph::thread {
4
5 int64_t Throttle::take(int64_t c)
6 {
7 if (!max) {
8 return 0;
9 }
10 count += c;
11 return count;
12 }
13
14 int64_t Throttle::put(int64_t c)
15 {
16 if (!max) {
17 return 0;
18 }
19 if (!c) {
20 return count;
21 }
22 on_free_slots.signal();
23 count -= c;
24 return count;
25 }
26
27 seastar::future<> Throttle::get(size_t c)
28 {
29 if (!max) {
30 return seastar::now();
31 }
32 return on_free_slots.wait([this, c] {
33 return !_should_wait(c);
34 }).then([this, c] {
35 count += c;
36 return seastar::now();
37 });
38 }
39
40 void Throttle::reset_max(size_t m) {
41 if (max == m) {
42 return;
43 }
44
45 if (m > max) {
46 on_free_slots.signal();
47 }
48 max = m;
49 }
50
51 bool Throttle::_should_wait(size_t c) const {
52 if (!max) {
53 return false;
54 }
55 return ((c <= max && count + c > max) || // normally stay under max
56 (c >= max && count > max)); // except for large c
57 }
58
59 } // namespace ceph::thread::seastar