per_thread_sem.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // This file is a no-op if the required LowLevelAlloc support is missing.
00016 #include "absl/base/internal/low_level_alloc.h"
00017 #ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
00018 
00019 #include "absl/synchronization/internal/per_thread_sem.h"
00020 
00021 #include <atomic>
00022 
00023 #include "absl/base/attributes.h"
00024 #include "absl/base/internal/thread_identity.h"
00025 #include "absl/synchronization/internal/waiter.h"
00026 
00027 namespace absl {
00028 namespace synchronization_internal {
00029 
00030 void PerThreadSem::SetThreadBlockedCounter(std::atomic<int> *counter) {
00031   base_internal::ThreadIdentity *identity;
00032   identity = GetOrCreateCurrentThreadIdentity();
00033   identity->blocked_count_ptr = counter;
00034 }
00035 
00036 std::atomic<int> *PerThreadSem::GetThreadBlockedCounter() {
00037   base_internal::ThreadIdentity *identity;
00038   identity = GetOrCreateCurrentThreadIdentity();
00039   return identity->blocked_count_ptr;
00040 }
00041 
00042 void PerThreadSem::Init(base_internal::ThreadIdentity *identity) {
00043   Waiter::GetWaiter(identity)->Init();
00044   identity->ticker.store(0, std::memory_order_relaxed);
00045   identity->wait_start.store(0, std::memory_order_relaxed);
00046   identity->is_idle.store(false, std::memory_order_relaxed);
00047 }
00048 
00049 void PerThreadSem::Tick(base_internal::ThreadIdentity *identity) {
00050   const int ticker =
00051       identity->ticker.fetch_add(1, std::memory_order_relaxed) + 1;
00052   const int wait_start = identity->wait_start.load(std::memory_order_relaxed);
00053   const bool is_idle = identity->is_idle.load(std::memory_order_relaxed);
00054   if (wait_start && (ticker - wait_start > Waiter::kIdlePeriods) && !is_idle) {
00055     // Wakeup the waiting thread since it is time for it to become idle.
00056     Waiter::GetWaiter(identity)->Poke();
00057   }
00058 }
00059 
00060 }  // namespace synchronization_internal
00061 }  // namespace absl
00062 
00063 extern "C" {
00064 
00065 ABSL_ATTRIBUTE_WEAK void AbslInternalPerThreadSemPost(
00066     absl::base_internal::ThreadIdentity *identity) {
00067   absl::synchronization_internal::Waiter::GetWaiter(identity)->Post();
00068 }
00069 
00070 ABSL_ATTRIBUTE_WEAK bool AbslInternalPerThreadSemWait(
00071     absl::synchronization_internal::KernelTimeout t) {
00072   bool timeout = false;
00073   absl::base_internal::ThreadIdentity *identity;
00074   identity = absl::synchronization_internal::GetOrCreateCurrentThreadIdentity();
00075 
00076   // Ensure wait_start != 0.
00077   int ticker = identity->ticker.load(std::memory_order_relaxed);
00078   identity->wait_start.store(ticker ? ticker : 1, std::memory_order_relaxed);
00079   identity->is_idle.store(false, std::memory_order_relaxed);
00080 
00081   if (identity->blocked_count_ptr != nullptr) {
00082     // Increment count of threads blocked in a given thread pool.
00083     identity->blocked_count_ptr->fetch_add(1, std::memory_order_relaxed);
00084   }
00085 
00086   timeout =
00087       !absl::synchronization_internal::Waiter::GetWaiter(identity)->Wait(t);
00088 
00089   if (identity->blocked_count_ptr != nullptr) {
00090     identity->blocked_count_ptr->fetch_sub(1, std::memory_order_relaxed);
00091   }
00092 
00093   identity->is_idle.store(false, std::memory_order_relaxed);
00094   identity->wait_start.store(0, std::memory_order_relaxed);
00095   return !timeout;
00096 }
00097 
00098 }  // extern "C"
00099 
00100 #endif  // ABSL_LOW_LEVEL_ALLOC_MISSING


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