Coverage Report

Created: 2026-08-13 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/synchronization/internal/per_thread_sem.h
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
16
// PerThreadSem is a low-level synchronization primitive controlling the
17
// runnability of a single thread, used internally by Mutex and CondVar.
18
//
19
// This is NOT a general-purpose synchronization mechanism, and should not be
20
// used directly by applications.  Applications should use Mutex and CondVar.
21
//
22
// The semantics of PerThreadSem are the same as that of a counting semaphore.
23
// Each thread maintains an abstract "count" value associated with its identity.
24
25
#ifndef ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_
26
#define ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_
27
28
#include <atomic>
29
30
#include "absl/base/internal/thread_identity.h"
31
#include "absl/synchronization/internal/create_thread_identity.h"
32
#include "absl/synchronization/internal/kernel_timeout.h"
33
#include "absl/time/time.h"
34
35
namespace gloop_do_not_use {
36
struct SynchronizationBenchmarkPeer;
37
}  // namespace gloop_do_not_use
38
39
namespace absl {
40
ABSL_NAMESPACE_BEGIN
41
42
class Mutex;
43
44
namespace synchronization_internal {
45
46
class PerThreadSem {
47
 public:
48
  PerThreadSem() = delete;
49
  PerThreadSem(const PerThreadSem&) = delete;
50
  PerThreadSem& operator=(const PerThreadSem&) = delete;
51
52
  // Routine invoked periodically (once a second) by a background thread.
53
  // Has no effect on user-visible state.
54
  static void Tick(base_internal::ThreadIdentity* identity);
55
56
  // ---------------------------------------------------------------------------
57
  // Routines used by autosizing threadpools to detect when threads are
58
  // blocked.  Each thread has a counter pointer, initially zero.  If non-zero,
59
  // the implementation atomically increments the counter when it blocks on a
60
  // semaphore, a decrements it again when it wakes.  This allows a threadpool
61
  // to keep track of how many of its threads are blocked.
62
  // SetThreadBlockedCounter() should be used only by threadpool
63
  // implementations.  GetThreadBlockedCounter() should be used by modules that
64
  // block threads; if the pointer returned is non-zero, the location should be
65
  // incremented before the thread blocks, and decremented after it wakes.
66
  static void SetThreadBlockedCounter(std::atomic<int> *counter);
67
  static std::atomic<int> *GetThreadBlockedCounter();
68
69
 private:
70
  // Create the PerThreadSem associated with "identity".  Initializes count=0.
71
  // REQUIRES: May only be called by ThreadIdentity.
72
  static inline void Init(base_internal::ThreadIdentity* identity);
73
74
  // Increments "identity"'s count.
75
  static inline void Post(base_internal::ThreadIdentity* identity);
76
77
  // Waits until either our count > 0 or t has expired.
78
  // If count > 0, decrements count and returns true.  Otherwise returns false.
79
  // !t.has_timeout() => Wait(t) will return true.
80
  static inline bool Wait(KernelTimeout t);
81
82
  // Waits until either our count > 0 or the absolute time t has passed.
83
  // If count > 0, decrements count and returns true.  Otherwise returns false.
84
  static inline bool WaitAbsolute(absl::Time t);
85
86
  // Permitted callers.
87
  friend class PerThreadSemTest;
88
  friend class absl::Mutex;
89
  friend struct ::gloop_do_not_use::SynchronizationBenchmarkPeer;
90
  friend void OneTimeInitThreadIdentity(absl::base_internal::ThreadIdentity*);
91
};
92
93
}  // namespace synchronization_internal
94
ABSL_NAMESPACE_END
95
}  // namespace absl
96
97
// In some build configurations we pass --detect-odr-violations to the
98
// gold linker.  This causes it to flag weak symbol overrides as ODR
99
// violations.  Because ODR only applies to C++ and not C,
100
// --detect-odr-violations ignores symbols not mangled with C++ names.
101
// By changing our extension points to be extern "C", we dodge this
102
// check.
103
extern "C" {
104
void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemInit)(
105
    absl::base_internal::ThreadIdentity* identity);
106
void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPost)(
107
    absl::base_internal::ThreadIdentity* identity);
108
bool ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemWait)(
109
    absl::synchronization_internal::KernelTimeout t);
110
void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPoke)(
111
    absl::base_internal::ThreadIdentity* identity);
112
}  // extern "C"
113
114
void absl::synchronization_internal::PerThreadSem::Init(
115
1
    absl::base_internal::ThreadIdentity* identity) {
116
1
  ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemInit)(identity);
117
1
}
118
119
void absl::synchronization_internal::PerThreadSem::Post(
120
0
    absl::base_internal::ThreadIdentity* identity) {
121
0
  ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPost)(identity);
122
0
}
123
124
bool absl::synchronization_internal::PerThreadSem::Wait(
125
0
    absl::synchronization_internal::KernelTimeout t) {
126
0
  return ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemWait)(t);
127
0
}
128
129
0
bool absl::synchronization_internal::PerThreadSem::WaitAbsolute(absl::Time t) {
130
0
  return Wait(KernelTimeout(t));
131
0
}
132
133
#endif  // ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_