NetQueryCounter: simplify

GitOrigin-RevId: f9d7cbf9c939bfc46b8a02b18a74264ad8827509
This commit is contained in:
Arseny Smirnov 2020-07-30 14:23:53 +03:00
parent e4ee1b7ce1
commit d7eadd77c1
3 changed files with 21 additions and 30 deletions

View File

@ -235,7 +235,7 @@ class NetQuery : public TsListNode<NetQueryDebug> {
*this = NetQuery();
}
bool empty() const {
return state_ == State::Empty || nq_counter_.empty() || may_be_lost_;
return state_ == State::Empty || !nq_counter_ || may_be_lost_;
}
void stop_track() {

View File

@ -8,6 +8,6 @@
namespace td {
std::atomic<uint64> NetQueryCounter::net_query_cnt_{0};
std::atomic<uint64> NetQueryCounter::counter_{0};
} // namespace td

View File

@ -13,44 +13,35 @@
namespace td {
class NetQueryCounter {
static std::atomic<uint64> net_query_cnt_;
public:
static uint64 get_count() {
return net_query_cnt_.load();
}
bool empty() const {
return !is_alive_;
}
explicit NetQueryCounter(bool is_alive = false) : is_alive_(is_alive) {
using Counter = std::atomic<uint64>;
// deprecated
NetQueryCounter(bool is_alive = false) {
if (is_alive) {
net_query_cnt_++;
*this = NetQueryCounter(&counter_);
}
}
NetQueryCounter(const NetQueryCounter &other) = delete;
NetQueryCounter &operator=(const NetQueryCounter &other) = delete;
NetQueryCounter(NetQueryCounter &&other) : is_alive_(other.is_alive_) {
other.is_alive_ = false;
static uint64 get_count() {
return counter_.load();
}
NetQueryCounter &operator=(NetQueryCounter &&other) {
if (is_alive_) {
net_query_cnt_--;
}
is_alive_ = other.is_alive_;
other.is_alive_ = false;
return *this;
NetQueryCounter(Counter *counter) : ptr_(counter) {
counter->fetch_add(1);
}
~NetQueryCounter() {
if (is_alive_) {
net_query_cnt_--;
}
explicit operator bool() const {
return (bool)ptr_;
}
private:
bool is_alive_;
struct Deleter {
void operator()(Counter *ptr) {
ptr->fetch_sub(1);
}
};
static Counter counter_;
std::unique_ptr<Counter, Deleter> ptr_;
};
} // namespace td