Coverage Report

Created: 2026-04-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/synchronization/internal/create_thread_identity.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 <stdint.h>
16
17
#include <new>
18
19
// This file is a no-op if the required LowLevelAlloc support is missing.
20
#include "absl/base/internal/low_level_alloc.h"
21
#include "absl/synchronization/internal/waiter.h"
22
#ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
23
24
#include <string.h>
25
26
#include "absl/base/attributes.h"
27
#include "absl/base/internal/spinlock.h"
28
#include "absl/base/internal/thread_identity.h"
29
#include "absl/synchronization/internal/per_thread_sem.h"
30
31
namespace absl {
32
ABSL_NAMESPACE_BEGIN
33
namespace synchronization_internal {
34
35
// ThreadIdentity storage is persistent, we maintain a free-list of previously
36
// released ThreadIdentity objects.
37
ABSL_CONST_INIT static base_internal::SpinLock freelist_lock(
38
    base_internal::SCHEDULE_KERNEL_ONLY);
39
ABSL_CONST_INIT static base_internal::ThreadIdentity* thread_identity_freelist;
40
41
// A per-thread destructor for reclaiming associated ThreadIdentity objects.
42
// Since we must preserve their storage, we cache them for re-use instead of
43
// truly destructing the object.
44
0
static void ReclaimThreadIdentity(void* v) {
45
0
  base_internal::ThreadIdentity* identity =
46
0
      static_cast<base_internal::ThreadIdentity*>(v);
47
48
  // all_locks might have been allocated by the Mutex implementation.
49
  // We free it here when we are notified that our thread is dying.
50
0
  if (identity->per_thread_synch.all_locks != nullptr) {
51
0
    base_internal::LowLevelAlloc::Free(identity->per_thread_synch.all_locks);
52
0
  }
53
54
  // We must explicitly clear the current thread's identity:
55
  // (a) Subsequent (unrelated) per-thread destructors may require an identity.
56
  //     We must guarantee a new identity is used in this case (this instructor
57
  //     will be reinvoked up to PTHREAD_DESTRUCTOR_ITERATIONS in this case).
58
  // (b) ThreadIdentity implementations may depend on memory that is not
59
  //     reinitialized before reuse.  We must allow explicit clearing of the
60
  //     association state in this case.
61
0
  base_internal::ClearCurrentThreadIdentity();
62
0
  {
63
0
    base_internal::SpinLockHolder l(freelist_lock);
64
0
    identity->next = thread_identity_freelist;
65
0
    thread_identity_freelist = identity;
66
0
  }
67
0
}
68
69
// Return value rounded up to next multiple of align.
70
// Align must be a power of two.
71
1
static intptr_t RoundUp(intptr_t addr, intptr_t align) {
72
1
  return (addr + align - 1) & ~(align - 1);
73
1
}
74
75
1
void OneTimeInitThreadIdentity(base_internal::ThreadIdentity* identity) {
76
1
  PerThreadSem::Init(identity);
77
1
  identity->ticker.store(0, std::memory_order_relaxed);
78
1
  identity->wait_start.store(0, std::memory_order_relaxed);
79
1
  identity->is_idle.store(false, std::memory_order_relaxed);
80
  // To avoid a circular dependency we declare only the storage in the header
81
  // and use placement new to construct the SpinLock.
82
1
  static_assert(
83
1
      sizeof(base_internal::ThreadIdentity::SchedulerState::
84
1
                 association_lock_word) == sizeof(base_internal::SpinLock),
85
1
      "Wrong size for SpinLock");
86
  // Protects the association between this identity and its schedulable.  Should
87
  // never be cooperative.
88
1
  new (&identity->scheduler_state.association_lock_word)
89
1
      base_internal::SpinLock(base_internal::SCHEDULE_KERNEL_ONLY);
90
1
}
91
92
static void ResetThreadIdentityBetweenReuse(
93
1
    base_internal::ThreadIdentity* identity) {
94
1
  base_internal::PerThreadSynch* pts = &identity->per_thread_synch;
95
1
  pts->next = nullptr;
96
1
  pts->skip = nullptr;
97
1
  pts->may_skip = false;
98
1
  pts->waitp = nullptr;
99
1
  pts->suppress_fatal_errors = false;
100
1
  pts->readers = 0;
101
1
  pts->priority = 0;
102
1
  pts->next_priority_read_cycles = 0;
103
1
  pts->state.store(base_internal::PerThreadSynch::State::kAvailable,
104
1
                   std::memory_order_relaxed);
105
1
  pts->maybe_unlocking = false;
106
1
  pts->wake = false;
107
1
  pts->cond_waiter = false;
108
1
  pts->all_locks = nullptr;
109
1
  base_internal::ThreadIdentity::SchedulerState* ss =
110
1
      &identity->scheduler_state;
111
1
  ss->bound_schedulable.store(nullptr, std::memory_order_relaxed);
112
1
  ss->association_lock_word = 0;
113
1
  ss->scheduling_disabled_depth.store(0, std::memory_order_relaxed);
114
1
  ss->potentially_blocking_depth = 0;
115
1
  ss->schedule_next_state = 0;
116
1
  ss->waking_designated_waker = false;
117
1
  identity->static_initialization_depth = 0;
118
1
  identity->wait_state.store(base_internal::ThreadIdentity::WaitState::kActive,
119
1
                             std::memory_order_relaxed);
120
1
  identity->blocked_count_ptr = nullptr;
121
1
  identity->ticker.store(0, std::memory_order_relaxed);
122
1
  identity->wait_start.store(0, std::memory_order_relaxed);
123
1
  identity->is_idle.store(false, std::memory_order_relaxed);
124
1
  identity->next = nullptr;
125
1
}
126
127
1
static base_internal::ThreadIdentity* NewThreadIdentity() {
128
1
  base_internal::ThreadIdentity* identity = nullptr;
129
130
1
  {
131
    // Re-use a previously released object if possible.
132
1
    base_internal::SpinLockHolder l(freelist_lock);
133
1
    if (thread_identity_freelist) {
134
0
      identity = thread_identity_freelist;  // Take list-head.
135
0
      thread_identity_freelist = thread_identity_freelist->next;
136
0
    }
137
1
  }
138
139
1
  if (identity == nullptr) {
140
    // Allocate enough space to align ThreadIdentity to a multiple of
141
    // PerThreadSynch::kAlignment. This space is never released (it is
142
    // added to a freelist by ReclaimThreadIdentity instead).
143
1
    void* allocation = base_internal::LowLevelAlloc::Alloc(
144
1
        sizeof(*identity) + base_internal::PerThreadSynch::kAlignment - 1);
145
    // Round up the address to the required alignment.
146
1
    identity = reinterpret_cast<base_internal::ThreadIdentity*>(
147
1
        RoundUp(reinterpret_cast<intptr_t>(allocation),
148
1
                base_internal::PerThreadSynch::kAlignment));
149
    // Note that *identity is never constructed.
150
    // TODO(b/357097463): change this "one time init" to be a proper
151
    // constructor.
152
1
    OneTimeInitThreadIdentity(identity);
153
1
  }
154
1
  ResetThreadIdentityBetweenReuse(identity);
155
156
1
  return identity;
157
1
}
158
159
// Allocates and attaches ThreadIdentity object for the calling thread.  Returns
160
// the new identity.
161
// REQUIRES: CurrentThreadIdentity(false) == nullptr
162
1
base_internal::ThreadIdentity* CreateThreadIdentity() {
163
1
  base_internal::ThreadIdentity* identity = NewThreadIdentity();
164
  // Associate the value with the current thread, and attach our destructor.
165
1
  base_internal::SetCurrentThreadIdentity(identity, ReclaimThreadIdentity);
166
1
  return identity;
167
1
}
168
169
}  // namespace synchronization_internal
170
ABSL_NAMESPACE_END
171
}  // namespace absl
172
173
#endif  // ABSL_LOW_LEVEL_ALLOC_MISSING