diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/messagebus/misc/tokenquota.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/messagebus/misc/tokenquota.h')
-rw-r--r-- | library/cpp/messagebus/misc/tokenquota.h | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/library/cpp/messagebus/misc/tokenquota.h b/library/cpp/messagebus/misc/tokenquota.h index d5bdd79625..190547fa54 100644 --- a/library/cpp/messagebus/misc/tokenquota.h +++ b/library/cpp/messagebus/misc/tokenquota.h @@ -25,26 +25,26 @@ namespace NBus { Y_UNUSED(padd_); } - bool Acquire(TAtomic level = 1, bool force = false) { - level = Max(TAtomicBase(level), TAtomicBase(1)); + bool Acquire(TAtomic level = 1, bool force = false) { + level = Max(TAtomicBase(level), TAtomicBase(1)); - if (Enabled && (Acquired < level || force)) { + if (Enabled && (Acquired < level || force)) { Acquired += AtomicSwap(&Tokens_, 0); } return !Enabled || Acquired >= level; } - void Consume(size_t items) { - if (Enabled) { + void Consume(size_t items) { + if (Enabled) { Y_ASSERT(Acquired >= TAtomicBase(items)); Acquired -= items; } } - bool Return(size_t items_) noexcept { - if (!Enabled || items_ == 0) + bool Return(size_t items_) noexcept { + if (!Enabled || items_ == 0) return false; const TAtomic items = items_; @@ -53,31 +53,31 @@ namespace NBus { return (value - items < WakeLev && value >= WakeLev); } - bool IsEnabled() const noexcept { + bool IsEnabled() const noexcept { return Enabled; } - bool IsAboveWake() const noexcept { + bool IsAboveWake() const noexcept { return !Enabled || (WakeLev <= AtomicGet(Tokens_)); } - size_t Tokens() const noexcept { + size_t Tokens() const noexcept { return Acquired + AtomicGet(Tokens_); } - size_t Check(const TAtomic level) const noexcept { + size_t Check(const TAtomic level) const noexcept { return !Enabled || level <= Acquired; } private: - bool Enabled; - TAtomicBase Acquired; + bool Enabled; + TAtomicBase Acquired; const TAtomicBase WakeLev; - TAtomic Tokens_; + TAtomic Tokens_; /* This padd requires for align Tokens_ member on its own CPU cacheline. */ - ui64 padd_; + ui64 padd_; }; } |