]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
f67539c2 | 4 | #include "throttle.h" |
11fdf7f2 | 5 | |
f67539c2 | 6 | namespace crimson::common { |
11fdf7f2 TL |
7 | |
8 | int64_t Throttle::take(int64_t c) | |
9 | { | |
20effc67 | 10 | if (max == 0u) { |
11fdf7f2 TL |
11 | return 0; |
12 | } | |
13 | count += c; | |
14 | return count; | |
15 | } | |
16 | ||
17 | int64_t Throttle::put(int64_t c) | |
18 | { | |
20effc67 | 19 | if (max == 0u) { |
11fdf7f2 TL |
20 | return 0; |
21 | } | |
22 | if (!c) { | |
23 | return count; | |
24 | } | |
25 | on_free_slots.signal(); | |
26 | count -= c; | |
27 | return count; | |
28 | } | |
29 | ||
30 | seastar::future<> Throttle::get(size_t c) | |
31 | { | |
20effc67 | 32 | if (max == 0u) { |
f67539c2 | 33 | return seastar::make_ready_future<>(); |
11fdf7f2 | 34 | } |
1e59de90 | 35 | pending++; |
11fdf7f2 TL |
36 | return on_free_slots.wait([this, c] { |
37 | return !_should_wait(c); | |
38 | }).then([this, c] { | |
1e59de90 | 39 | pending--; |
11fdf7f2 | 40 | count += c; |
f67539c2 | 41 | return seastar::make_ready_future<>(); |
11fdf7f2 TL |
42 | }); |
43 | } | |
44 | ||
45 | void Throttle::reset_max(size_t m) { | |
46 | if (max == m) { | |
47 | return; | |
48 | } | |
49 | ||
50 | if (m > max) { | |
51 | on_free_slots.signal(); | |
52 | } | |
53 | max = m; | |
54 | } | |
55 | ||
56 | bool Throttle::_should_wait(size_t c) const { | |
57 | if (!max) { | |
58 | return false; | |
59 | } | |
60 | return ((c <= max && count + c > max) || // normally stay under max | |
61 | (c >= max && count > max)); // except for large c | |
62 | } | |
63 | ||
f67539c2 | 64 | } // namespace crimson::common |