aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/FloodControlGlobal.cpp
blob: ac57d0c6071ebae558fdbdf8f9cc58b67b6cfe84 (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
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/FloodControlGlobal.h"

namespace td {

FloodControlGlobal::FloodControlGlobal(uint64 limit) : limit_(limit) {
}

void FloodControlGlobal::finish() {
  auto old_value = active_count_.fetch_sub(1, std::memory_order_relaxed);
  CHECK(old_value > 0);
}

FloodControlGlobal::Guard FloodControlGlobal::try_start() {
  auto old_value = active_count_.fetch_add(1, std::memory_order_relaxed);
  if (old_value >= limit_) {
    finish();
    return nullptr;
  }
  return Guard(this);
}

void FloodControlGlobal::Finish::operator()(FloodControlGlobal *ctrl) const {
  ctrl->finish();
}

}  // namespace td