Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/base/internal/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 "absl/base/internal/thread_identity.h"
16
17
#include <atomic>
18
#include <cassert>
19
#include <memory>
20
21
#include "absl/base/attributes.h"
22
#include "absl/base/call_once.h"
23
#include "absl/base/config.h"
24
#include "absl/base/internal/raw_logging.h"
25
#include "absl/base/internal/spinlock.h"
26
27
#if ABSL_THREAD_IDENTITY_MODE != ABSL_THREAD_IDENTITY_MODE_USE_CPP11
28
#include <pthread.h>
29
#ifndef __wasi__
30
// WASI does not provide this header, either way we disable use
31
// of signals with it below.
32
#include <signal.h>
33
#endif
34
#endif
35
36
namespace absl {
37
ABSL_NAMESPACE_BEGIN
38
namespace base_internal {
39
40
#if ABSL_THREAD_IDENTITY_MODE != ABSL_THREAD_IDENTITY_MODE_USE_CPP11
41
namespace {
42
// Used to co-ordinate one-time creation of our pthread_key
43
absl::once_flag init_thread_identity_key_once;
44
pthread_key_t thread_identity_pthread_key;
45
std::atomic<bool> pthread_key_initialized(false);
46
47
1
void AllocateThreadIdentityKey(ThreadIdentityReclaimerFunction reclaimer) {
48
1
  pthread_key_create(&thread_identity_pthread_key, reclaimer);
49
1
  pthread_key_initialized.store(true, std::memory_order_release);
50
1
}
51
}  // namespace
52
#endif
53
54
#if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
55
    ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
56
// The actual TLS storage for a thread's currently associated ThreadIdentity.
57
// This is referenced by inline accessors in the header.
58
// "protected" visibility ensures that if multiple instances of Abseil code
59
// exist within a process (via dlopen() or similar), references to
60
// thread_identity_ptr from each instance of the code will refer to
61
// *different* instances of this ptr.
62
// Apple platforms have the visibility attribute, but issue a compile warning
63
// that protected visibility is unsupported.
64
ABSL_CONST_INIT  // Must come before __attribute__((visibility("protected")))
65
#if ABSL_HAVE_ATTRIBUTE(visibility) && !defined(__APPLE__)
66
    __attribute__((visibility("protected")))
67
#endif  // ABSL_HAVE_ATTRIBUTE(visibility) && !defined(__APPLE__)
68
#if ABSL_PER_THREAD_TLS
69
    // Prefer __thread to thread_local as benchmarks indicate it is a bit
70
    // faster.
71
    ABSL_PER_THREAD_TLS_KEYWORD ThreadIdentity* thread_identity_ptr = nullptr;
72
#elif defined(ABSL_HAVE_THREAD_LOCAL)
73
    thread_local ThreadIdentity* thread_identity_ptr = nullptr;
74
#endif  // ABSL_PER_THREAD_TLS
75
#endif  // TLS or CPP11
76
77
void SetCurrentThreadIdentity(ThreadIdentity* identity,
78
1
                              ThreadIdentityReclaimerFunction reclaimer) {
79
1
  assert(CurrentThreadIdentityIfPresent() == nullptr);
80
  // Associate our destructor.
81
  // NOTE: This call to pthread_setspecific is currently the only immovable
82
  // barrier to CurrentThreadIdentity() always being async signal safe.
83
1
#if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
84
  // NOTE: Not async-safe.  But can be open-coded.
85
1
  absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
86
1
                  reclaimer);
87
88
#if defined(__wasi__) || defined(__EMSCRIPTEN__) || defined(__MINGW32__) || \
89
    defined(__hexagon__)
90
  // Emscripten, WASI and MinGW pthread implementations do not support
91
  // signals. See
92
  // https://kripken.github.io/emscripten-site/docs/porting/pthreads.html for
93
  // more information.
94
  pthread_setspecific(thread_identity_pthread_key,
95
                      reinterpret_cast<void*>(identity));
96
#else
97
  // We must mask signals around the call to setspecific as with current glibc,
98
  // a concurrent getspecific (needed for GetCurrentThreadIdentityIfPresent())
99
  // may zero our value.
100
  //
101
  // While not officially async-signal safe, getspecific within a signal handler
102
  // is otherwise OK.
103
1
  sigset_t all_signals;
104
1
  sigset_t curr_signals;
105
1
  sigfillset(&all_signals);
106
1
  pthread_sigmask(SIG_SETMASK, &all_signals, &curr_signals);
107
1
  pthread_setspecific(thread_identity_pthread_key,
108
1
                      reinterpret_cast<void*>(identity));
109
1
  pthread_sigmask(SIG_SETMASK, &curr_signals, nullptr);
110
1
#endif  // !__EMSCRIPTEN__ && !__MINGW32__
111
112
#elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS
113
  // NOTE: Not async-safe.  But can be open-coded.
114
  absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
115
                  reclaimer);
116
  pthread_setspecific(thread_identity_pthread_key,
117
                      reinterpret_cast<void*>(identity));
118
  thread_identity_ptr = identity;
119
#elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
120
  thread_local std::unique_ptr<ThreadIdentity, ThreadIdentityReclaimerFunction>
121
      holder(identity, reclaimer);
122
  thread_identity_ptr = identity;
123
#else
124
#error Unimplemented ABSL_THREAD_IDENTITY_MODE
125
#endif
126
1
}
127
128
#if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
129
    ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
130
131
// Please see the comment on `CurrentThreadIdentityIfPresent` in
132
// thread_identity.h. When we cannot expose thread_local variables in
133
// headers, we opt for the correct-but-slower option of not inlining this
134
// function.
135
#ifndef ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT
136
ThreadIdentity* CurrentThreadIdentityIfPresent() { return thread_identity_ptr; }
137
#endif
138
#endif
139
140
0
void ClearCurrentThreadIdentity() {
141
#if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
142
    ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
143
  thread_identity_ptr = nullptr;
144
#elif ABSL_THREAD_IDENTITY_MODE == \
145
    ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
146
  // pthread_setspecific expected to clear value on destruction
147
0
  assert(CurrentThreadIdentityIfPresent() == nullptr);
148
0
#endif
149
0
}
150
151
#if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
152
2.60M
ThreadIdentity* CurrentThreadIdentityIfPresent() {
153
2.60M
  bool initialized = pthread_key_initialized.load(std::memory_order_acquire);
154
2.60M
  if (!initialized) {
155
6
    return nullptr;
156
6
  }
157
2.60M
  return reinterpret_cast<ThreadIdentity*>(
158
2.60M
      pthread_getspecific(thread_identity_pthread_key));
159
2.60M
}
160
#endif
161
162
}  // namespace base_internal
163
ABSL_NAMESPACE_END
164
}  // namespace absl