Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/synchronization/internal/per_thread_sem.cc
Line
Count
Source
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
#include "absl/base/internal/low_level_alloc.h"  // IWYU pragma: keep
16
17
// This file is a no-op if the required LowLevelAlloc support is missing.
18
#ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
19
20
#include <atomic>
21
22
#include "absl/base/attributes.h"
23
#include "absl/base/config.h"
24
#include "absl/base/internal/thread_identity.h"
25
#include "absl/synchronization/internal/create_thread_identity.h"
26
#include "absl/synchronization/internal/kernel_timeout.h"
27
#include "absl/synchronization/internal/per_thread_sem.h"
28
#include "absl/synchronization/internal/waiter.h"
29
30
namespace absl {
31
ABSL_NAMESPACE_BEGIN
32
namespace synchronization_internal {
33
34
0
void PerThreadSem::SetThreadBlockedCounter(std::atomic<int> *counter) {
35
0
  base_internal::ThreadIdentity *identity;
36
0
  identity = GetOrCreateCurrentThreadIdentity();
37
0
  identity->blocked_count_ptr = counter;
38
0
}
39
40
0
std::atomic<int> *PerThreadSem::GetThreadBlockedCounter() {
41
0
  base_internal::ThreadIdentity *identity;
42
0
  identity = GetOrCreateCurrentThreadIdentity();
43
0
  return identity->blocked_count_ptr;
44
0
}
45
46
0
void PerThreadSem::Tick(base_internal::ThreadIdentity *identity) {
47
0
  const int ticker =
48
0
      identity->ticker.fetch_add(1, std::memory_order_relaxed) + 1;
49
0
  const int wait_start = identity->wait_start.load(std::memory_order_relaxed);
50
0
  const bool is_idle = identity->is_idle.load(std::memory_order_relaxed);
51
0
  if (wait_start && (ticker - wait_start > Waiter::kIdlePeriods) && !is_idle) {
52
    // Wakeup the waiting thread since it is time for it to become idle.
53
0
    ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPoke)(identity);
54
0
  }
55
0
}
56
57
}  // namespace synchronization_internal
58
ABSL_NAMESPACE_END
59
}  // namespace absl
60
61
extern "C" {
62
63
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemInit)(
64
1
    absl::base_internal::ThreadIdentity *identity) {
65
1
  new (absl::synchronization_internal::Waiter::GetWaiter(identity))
66
1
      absl::synchronization_internal::Waiter();
67
1
}
68
69
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPost)(
70
0
    absl::base_internal::ThreadIdentity *identity) {
71
0
  absl::synchronization_internal::Waiter::GetWaiter(identity)->Post();
72
0
}
73
74
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPoke)(
75
0
    absl::base_internal::ThreadIdentity *identity) {
76
0
  absl::synchronization_internal::Waiter::GetWaiter(identity)->Poke();
77
0
}
78
79
ABSL_ATTRIBUTE_WEAK bool ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemWait)(
80
0
    absl::synchronization_internal::KernelTimeout t) {
81
0
  bool timeout = false;
82
0
  absl::base_internal::ThreadIdentity *identity;
83
0
  identity = absl::synchronization_internal::GetOrCreateCurrentThreadIdentity();
84
85
  // Ensure wait_start != 0.
86
0
  int ticker = identity->ticker.load(std::memory_order_relaxed);
87
0
  identity->wait_start.store(ticker ? ticker : 1, std::memory_order_relaxed);
88
0
  identity->is_idle.store(false, std::memory_order_relaxed);
89
90
0
  if (identity->blocked_count_ptr != nullptr) {
91
    // Increment count of threads blocked in a given thread pool.
92
0
    identity->blocked_count_ptr->fetch_add(1, std::memory_order_relaxed);
93
0
  }
94
95
0
  timeout =
96
0
      !absl::synchronization_internal::Waiter::GetWaiter(identity)->Wait(t);
97
98
0
  if (identity->blocked_count_ptr != nullptr) {
99
0
    identity->blocked_count_ptr->fetch_sub(1, std::memory_order_relaxed);
100
0
  }
101
102
0
  identity->is_idle.store(false, std::memory_order_relaxed);
103
0
  identity->wait_start.store(0, std::memory_order_relaxed);
104
0
  return !timeout;
105
0
}
106
107
}  // extern "C"
108
109
#endif  // ABSL_LOW_LEVEL_ALLOC_MISSING