tvl-depot/absl/base/internal/periodic_sampler.cc
Abseil Team 2103fd9acd Export of internal Abseil changes
--
d447fdcb801036cf08197eece193a5a706661120 by Gennadiy Rozental <rogeeff@google.com>:

Eliminate the need for static function holding help message. This decreases the cost of ABSL_FLAG abstraction by 120 bytes under clang.

PiperOrigin-RevId: 281107806

--
0aa6b91189f0e8b2381438c33465673a7ae02487 by Derek Mauro <dmauro@google.com>:

Disable the weak symbol CCTZ extension in the time test_util
on MinGW, which does not support it.

PiperOrigin-RevId: 280719769

--
67322c41c3e776eb541de90fa4526bdb49422eb6 by Abseil Team <absl-team@google.com>:

Tune PeriodicSampler implementation (for internal-use only)

PiperOrigin-RevId: 280708943

--
3a48c346340c7ed03816645cd327e1ff07729aa4 by Abseil Team <absl-team@google.com>:

Clean up public headers not to have warnings for "-Wcomma"

PiperOrigin-RevId: 280695373

--
981acd1ef3b13a83a84f04f11c8931f4ed4451c9 by Matthew Brown <matthewbr@google.com>:

Release absl::int128.

PiperOrigin-RevId: 280690817

--
d30fae9d2ec30b81322d2eb5afe7e13e45b4b422 by Derek Mauro <dmauro@google.com>:

Fix -Wundef warnings in random platform detection

PiperOrigin-RevId: 280669598
GitOrigin-RevId: d447fdcb801036cf08197eece193a5a706661120
Change-Id: Ie5e10e567c54b7de211833607689f233d4ddf734
2019-11-19 10:27:57 -05:00

50 lines
1.5 KiB
C++

// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/base/internal/periodic_sampler.h"
#include <atomic>
#include "absl/base/internal/exponential_biased.h"
namespace absl {
namespace base_internal {
int64_t PeriodicSamplerBase::GetExponentialBiased(int period) noexcept {
return rng_.Get(period);
}
bool PeriodicSamplerBase::SubtleConfirmSample() noexcept {
int current_period = period();
// Deal with period case 0 (always off) and 1 (always on)
if (ABSL_PREDICT_FALSE(current_period < 2)) {
stride_ = 0;
return current_period == 1;
}
// Check if this is the first call to Sample()
if (ABSL_PREDICT_FALSE(stride_ == 1)) {
stride_ = static_cast<uint64_t>(-1 - GetExponentialBiased(current_period));
if (static_cast<int64_t>(stride_) < -1) {
++stride_;
return false;
}
}
stride_ = static_cast<uint64_t>(-1 - GetExponentialBiased(current_period));
return true;
}
} // namespace base_internal
} // namespace absl