abseil-cpp/absl/synchronization/internal/per_thread_sem.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // This file is a no-op if the required LowLevelAlloc support is missing.
16 #include "absl/base/internal/low_level_alloc.h"
17 #ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
18 
19 #include "absl/synchronization/internal/per_thread_sem.h"
20 
21 #include <atomic>
22 
23 #include "absl/base/attributes.h"
24 #include "absl/base/internal/thread_identity.h"
25 #include "absl/synchronization/internal/waiter.h"
26 
27 namespace absl {
29 namespace synchronization_internal {
30 
34  identity->blocked_count_ptr = counter;
35 }
36 
40  return identity->blocked_count_ptr;
41 }
42 
44  new (Waiter::GetWaiter(identity)) Waiter();
45  identity->ticker.store(0, std::memory_order_relaxed);
46  identity->wait_start.store(0, std::memory_order_relaxed);
47  identity->is_idle.store(false, std::memory_order_relaxed);
48 }
49 
51  const int ticker =
52  identity->ticker.fetch_add(1, std::memory_order_relaxed) + 1;
53  const int wait_start = identity->wait_start.load(std::memory_order_relaxed);
54  const bool is_idle = identity->is_idle.load(std::memory_order_relaxed);
55  if (wait_start && (ticker - wait_start > Waiter::kIdlePeriods) && !is_idle) {
56  // Wakeup the waiting thread since it is time for it to become idle.
57  Waiter::GetWaiter(identity)->Poke();
58  }
59 }
60 
61 } // namespace synchronization_internal
63 } // namespace absl
64 
65 extern "C" {
66 
70 }
71 
74  bool timeout = false;
77 
78  // Ensure wait_start != 0.
79  int ticker = identity->ticker.load(std::memory_order_relaxed);
80  identity->wait_start.store(ticker ? ticker : 1, std::memory_order_relaxed);
81  identity->is_idle.store(false, std::memory_order_relaxed);
82 
83  if (identity->blocked_count_ptr != nullptr) {
84  // Increment count of threads blocked in a given thread pool.
85  identity->blocked_count_ptr->fetch_add(1, std::memory_order_relaxed);
86  }
87 
88  timeout =
90 
91  if (identity->blocked_count_ptr != nullptr) {
92  identity->blocked_count_ptr->fetch_sub(1, std::memory_order_relaxed);
93  }
94 
95  identity->is_idle.store(false, std::memory_order_relaxed);
96  identity->wait_start.store(0, std::memory_order_relaxed);
97  return !timeout;
98 }
99 
100 } // extern "C"
101 
102 #endif // ABSL_LOW_LEVEL_ALLOC_MISSING
absl::synchronization_internal::GetOrCreateCurrentThreadIdentity
base_internal::ThreadIdentity * GetOrCreateCurrentThreadIdentity()
Definition: abseil-cpp/absl/synchronization/internal/create_thread_identity.h:43
absl::base_internal::ThreadIdentity
Definition: abseil-cpp/absl/base/internal/thread_identity.h:137
absl::base_internal::ThreadIdentity::ticker
std::atomic< int > ticker
Definition: abseil-cpp/absl/base/internal/thread_identity.h:155
absl::synchronization_internal::Waiter::Post
void Post()
Definition: abseil-cpp/absl/synchronization/internal/waiter.cc:107
absl::synchronization_internal::Waiter::GetWaiter
static Waiter * GetWaiter(base_internal::ThreadIdentity *identity)
Definition: abseil-cpp/absl/synchronization/internal/waiter.h:88
absl::base_internal::ThreadIdentity::wait_start
std::atomic< int > wait_start
Definition: abseil-cpp/absl/base/internal/thread_identity.h:156
absl::synchronization_internal::PerThreadSem::Tick
static void Tick(base_internal::ThreadIdentity *identity)
Definition: abseil-cpp/absl/synchronization/internal/per_thread_sem.cc:50
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::synchronization_internal::Waiter::kIdlePeriods
static constexpr int kIdlePeriods
Definition: abseil-cpp/absl/synchronization/internal/waiter.h:97
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::synchronization_internal::PerThreadSem::SetThreadBlockedCounter
static void SetThreadBlockedCounter(std::atomic< int > *counter)
Definition: abseil-cpp/absl/synchronization/internal/per_thread_sem.cc:31
counter
static int counter
Definition: abseil-cpp/absl/flags/reflection_test.cc:131
absl::synchronization_internal::KernelTimeout
Definition: abseil-cpp/absl/synchronization/internal/kernel_timeout.h:44
absl::base_internal::ThreadIdentity::blocked_count_ptr
std::atomic< int > * blocked_count_ptr
Definition: abseil-cpp/absl/base/internal/thread_identity.h:150
ABSL_INTERNAL_C_SYMBOL
#define ABSL_INTERNAL_C_SYMBOL(x)
Definition: third_party/abseil-cpp/absl/base/config.h:172
AbslInternalPerThreadSemWait
ABSL_ATTRIBUTE_WEAK bool ABSL_INTERNAL_C_SYMBOL() AbslInternalPerThreadSemWait(absl::synchronization_internal::KernelTimeout t)
Definition: abseil-cpp/absl/synchronization/internal/per_thread_sem.cc:72
absl::synchronization_internal::PerThreadSem::Init
static void Init(base_internal::ThreadIdentity *identity)
Definition: abseil-cpp/absl/synchronization/internal/per_thread_sem.cc:43
ABSL_ATTRIBUTE_WEAK
bool absl_internal_cordz_disabled ABSL_ATTRIBUTE_WEAK
Definition: bloaty/third_party/abseil-cpp/absl/strings/internal/cordz_functions.cc:31
absl::synchronization_internal::Waiter::Poke
void Poke()
Definition: abseil-cpp/absl/synchronization/internal/waiter.cc:114
absl::synchronization_internal::Waiter
Definition: abseil-cpp/absl/synchronization/internal/waiter.h:65
absl::synchronization_internal::Waiter::Wait
bool Wait(KernelTimeout t)
Definition: abseil-cpp/absl/synchronization/internal/waiter.cc:74
absl::synchronization_internal::PerThreadSem::GetThreadBlockedCounter
static std::atomic< int > * GetThreadBlockedCounter()
Definition: abseil-cpp/absl/synchronization/internal/per_thread_sem.cc:37
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
AbslInternalPerThreadSemPost
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL() AbslInternalPerThreadSemPost(absl::base_internal::ThreadIdentity *identity)
Definition: abseil-cpp/absl/synchronization/internal/per_thread_sem.cc:67
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
absl::base_internal::ThreadIdentity::is_idle
std::atomic< bool > is_idle
Definition: abseil-cpp/absl/base/internal/thread_identity.h:157


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:41