summaryrefslogtreecommitdiffstats
path: root/src/crimson/thread/Throttle.cc
blob: 1d67e7231743ec11120a0353038fb37843a88816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Throttle.h"

namespace ceph::thread {

int64_t Throttle::take(int64_t c)
{
  if (!max) {
    return 0;
  }
  count += c;
  return count;
}

int64_t Throttle::put(int64_t c)
{
  if (!max) {
    return 0;
  }
  if (!c) {
    return count;
  }
  on_free_slots.signal();
  count -= c;
  return count;
}

seastar::future<> Throttle::get(size_t c)
{
  if (!max) {
    return seastar::now();
  }
  return on_free_slots.wait([this, c] {
    return !_should_wait(c);
  }).then([this, c] {
    count += c;
    return seastar::now();
  });
}

void Throttle::reset_max(size_t m) {
  if (max == m) {
    return;
  }

  if (m > max) {
    on_free_slots.signal();
  }
  max = m;
}

bool Throttle::_should_wait(size_t c) const {
  if (!max) {
    return false;
  }
  return ((c <= max && count + c > max) || // normally stay under max
          (c >= max && count > max));      // except for large c
}

} // namespace ceph::thread::seastar