2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
2020-07-30 21:59:23 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class NetQueryCounter {
|
|
|
|
public:
|
2020-07-30 13:23:53 +02:00
|
|
|
using Counter = std::atomic<uint64>;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-07-30 21:59:23 +02:00
|
|
|
NetQueryCounter() = default;
|
2020-07-30 13:23:53 +02:00
|
|
|
|
2020-07-30 21:59:23 +02:00
|
|
|
explicit NetQueryCounter(Counter *counter) : ptr_(counter) {
|
|
|
|
CHECK(counter != nullptr);
|
|
|
|
counter->fetch_add(1, std::memory_order_relaxed);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-30 13:23:53 +02:00
|
|
|
|
2022-10-09 18:30:18 +02:00
|
|
|
explicit operator bool() const noexcept {
|
2020-07-30 21:59:23 +02:00
|
|
|
return static_cast<bool>(ptr_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-30 13:23:53 +02:00
|
|
|
struct Deleter {
|
|
|
|
void operator()(Counter *ptr) {
|
2020-07-30 21:59:23 +02:00
|
|
|
ptr->fetch_sub(1, std::memory_order_relaxed);
|
2020-07-30 13:23:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unique_ptr<Counter, Deleter> ptr_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|