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.
17 #ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
18 
20 
21 #include <atomic>
22 
23 #include "absl/base/attributes.h"
26 
27 namespace absl {
28 namespace synchronization_internal {
29 
33  identity->blocked_count_ptr = counter;
34 }
35 
39  return identity->blocked_count_ptr;
40 }
41 
43  Waiter::GetWaiter(identity)->Init();
44  identity->ticker.store(0, std::memory_order_relaxed);
45  identity->wait_start.store(0, std::memory_order_relaxed);
46  identity->is_idle.store(false, std::memory_order_relaxed);
47 }
48 
50  const int ticker =
51  identity->ticker.fetch_add(1, std::memory_order_relaxed) + 1;
52  const int wait_start = identity->wait_start.load(std::memory_order_relaxed);
53  const bool is_idle = identity->is_idle.load(std::memory_order_relaxed);
54  if (wait_start && (ticker - wait_start > Waiter::kIdlePeriods) && !is_idle) {
55  // Wakeup the waiting thread since it is time for it to become idle.
56  Waiter::GetWaiter(identity)->Poke();
57  }
58 }
59 
60 } // namespace synchronization_internal
61 } // namespace absl
62 
63 extern "C" {
64 
68 }
69 
72  bool timeout = false;
75 
76  // Ensure wait_start != 0.
77  int ticker = identity->ticker.load(std::memory_order_relaxed);
78  identity->wait_start.store(ticker ? ticker : 1, std::memory_order_relaxed);
79  identity->is_idle.store(false, std::memory_order_relaxed);
80 
81  if (identity->blocked_count_ptr != nullptr) {
82  // Increment count of threads blocked in a given thread pool.
83  identity->blocked_count_ptr->fetch_add(1, std::memory_order_relaxed);
84  }
85 
86  timeout =
88 
89  if (identity->blocked_count_ptr != nullptr) {
90  identity->blocked_count_ptr->fetch_sub(1, std::memory_order_relaxed);
91  }
92 
93  identity->is_idle.store(false, std::memory_order_relaxed);
94  identity->wait_start.store(0, std::memory_order_relaxed);
95  return !timeout;
96 }
97 
98 } // extern "C"
99 
100 #endif // ABSL_LOW_LEVEL_ALLOC_MISSING
bool Wait(KernelTimeout t)
Definition: waiter.cc:129
static void Init(base_internal::ThreadIdentity *identity)
base_internal::ThreadIdentity * GetOrCreateCurrentThreadIdentity()
ABSL_ATTRIBUTE_WEAK bool AbslInternalPerThreadSemWait(absl::synchronization_internal::KernelTimeout t)
static void SetThreadBlockedCounter(std::atomic< int > *counter)
int * counter
std::atomic< int > * blocked_count_ptr
Definition: algorithm.h:29
ABSL_ATTRIBUTE_WEAK void AbslInternalPerThreadSemPost(absl::base_internal::ThreadIdentity *identity)
static void Tick(base_internal::ThreadIdentity *identity)
#define ABSL_ATTRIBUTE_WEAK
Definition: attributes.h:168
static std::atomic< int > * GetThreadBlockedCounter()
static Waiter * GetWaiter(base_internal::ThreadIdentity *identity)
Definition: waiter.h:82


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:57