Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00056 Waiter::GetWaiter(identity)->Poke();
00057 }
00058 }
00059
00060 }
00061 }
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
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
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 }
00099
00100 #endif // ABSL_LOW_LEVEL_ALLOC_MISSING