diff options
author | Derek Mauro <dmauro@google.com> | 2023-03-20 14:10:11 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-20 14:11:09 -0700 |
commit | 396e9764cded739f595ba880b95b061051c49128 (patch) | |
tree | 02b550c2a2b9a4fcacb4cf2555d9d8672d53248b /absl/synchronization/internal/waiter_base.cc | |
parent | e5067964adcb792b7c9dbddbdcfe4d9b94079597 (diff) | |
download | abseil-396e9764cded739f595ba880b95b061051c49128.tar.gz abseil-396e9764cded739f595ba880b95b061051c49128.tar.bz2 abseil-396e9764cded739f595ba880b95b061051c49128.zip |
Synchronization: Refactor Waiter to allow us to write tests
Instead of being only able to test the platform Waiter implementation,
this allows us to be able to test all Waiter implementations that
build on a specific platform.
A unittest is added that tests all implementations that work for the
platform, and allows us to check that the expected one is being used
by printing the name of the selected implementation.
PiperOrigin-RevId: 518072415
Change-Id: Ie9e6fcd9d8283b4038e6f4e68a304d2adcc04b19
Diffstat (limited to 'absl/synchronization/internal/waiter_base.cc')
-rw-r--r-- | absl/synchronization/internal/waiter_base.cc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/absl/synchronization/internal/waiter_base.cc b/absl/synchronization/internal/waiter_base.cc new file mode 100644 index 00000000..46928b40 --- /dev/null +++ b/absl/synchronization/internal/waiter_base.cc @@ -0,0 +1,42 @@ +// Copyright 2023 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/synchronization/internal/waiter_base.h" + +#include "absl/base/config.h" +#include "absl/base/internal/thread_identity.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN +namespace synchronization_internal { + +#ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL +constexpr int WaiterBase::kIdlePeriods; +#endif + +void WaiterBase::MaybeBecomeIdle() { + base_internal::ThreadIdentity *identity = + base_internal::CurrentThreadIdentityIfPresent(); + assert(identity != nullptr); + const bool is_idle = identity->is_idle.load(std::memory_order_relaxed); + const int ticker = identity->ticker.load(std::memory_order_relaxed); + const int wait_start = identity->wait_start.load(std::memory_order_relaxed); + if (!is_idle && ticker - wait_start > kIdlePeriods) { + identity->is_idle.store(true, std::memory_order_relaxed); + } +} + +} // namespace synchronization_internal +ABSL_NAMESPACE_END +} // namespace absl |