create_thread_identity.cc
Go to the documentation of this file.
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 #include <new>
17 
18 // This file is a no-op if the required LowLevelAlloc support is missing.
20 #ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
21 
22 #include <string.h>
23 
24 #include "absl/base/attributes.h"
28 
29 namespace absl {
30 namespace synchronization_internal {
31 
32 // ThreadIdentity storage is persistent, we maintain a free-list of previously
33 // released ThreadIdentity objects.
36 
37 // A per-thread destructor for reclaiming associated ThreadIdentity objects.
38 // Since we must preserve their storage we cache them for re-use.
39 static void ReclaimThreadIdentity(void* v) {
41  static_cast<base_internal::ThreadIdentity*>(v);
42 
43  // all_locks might have been allocated by the Mutex implementation.
44  // We free it here when we are notified that our thread is dying.
45  if (identity->per_thread_synch.all_locks != nullptr) {
47  }
48 
49  // We must explicitly clear the current thread's identity:
50  // (a) Subsequent (unrelated) per-thread destructors may require an identity.
51  // We must guarantee a new identity is used in this case (this instructor
52  // will be reinvoked up to PTHREAD_DESTRUCTOR_ITERATIONS in this case).
53  // (b) ThreadIdentity implementations may depend on memory that is not
54  // reinitialized before reuse. We must allow explicit clearing of the
55  // association state in this case.
57  {
58  base_internal::SpinLockHolder l(&freelist_lock);
59  identity->next = thread_identity_freelist;
60  thread_identity_freelist = identity;
61  }
62 }
63 
64 // Return value rounded up to next multiple of align.
65 // Align must be a power of two.
66 static intptr_t RoundUp(intptr_t addr, intptr_t align) {
67  return (addr + align - 1) & ~(align - 1);
68 }
69 
72  pts->next = nullptr;
73  pts->skip = nullptr;
74  pts->may_skip = false;
75  pts->waitp = nullptr;
76  pts->suppress_fatal_errors = false;
77  pts->readers = 0;
78  pts->priority = 0;
80  pts->state.store(base_internal::PerThreadSynch::State::kAvailable,
81  std::memory_order_relaxed);
82  pts->maybe_unlocking = false;
83  pts->wake = false;
84  pts->cond_waiter = false;
85  pts->all_locks = nullptr;
86  identity->waiter_state = {};
87  identity->blocked_count_ptr = nullptr;
88  identity->ticker.store(0, std::memory_order_relaxed);
89  identity->wait_start.store(0, std::memory_order_relaxed);
90  identity->is_idle.store(false, std::memory_order_relaxed);
91  identity->next = nullptr;
92 }
93 
95  base_internal::ThreadIdentity* identity = nullptr;
96 
97  {
98  // Re-use a previously released object if possible.
99  base_internal::SpinLockHolder l(&freelist_lock);
100  if (thread_identity_freelist) {
101  identity = thread_identity_freelist; // Take list-head.
102  thread_identity_freelist = thread_identity_freelist->next;
103  }
104  }
105 
106  if (identity == nullptr) {
107  // Allocate enough space to align ThreadIdentity to a multiple of
108  // PerThreadSynch::kAlignment. This space is never released (it is
109  // added to a freelist by ReclaimThreadIdentity instead).
110  void* allocation = base_internal::LowLevelAlloc::Alloc(
111  sizeof(*identity) + base_internal::PerThreadSynch::kAlignment - 1);
112  // Round up the address to the required alignment.
113  identity = reinterpret_cast<base_internal::ThreadIdentity*>(
114  RoundUp(reinterpret_cast<intptr_t>(allocation),
116  }
117  ResetThreadIdentity(identity);
118 
119  return identity;
120 }
121 
122 // Allocates and attaches ThreadIdentity object for the calling thread. Returns
123 // the new identity.
124 // REQUIRES: CurrentThreadIdentity(false) == nullptr
127  PerThreadSem::Init(identity);
128  // Associate the value with the current thread, and attach our destructor.
130  return identity;
131 }
132 
133 } // namespace synchronization_internal
134 } // namespace absl
135 
136 #endif // ABSL_LOW_LEVEL_ALLOC_MISSING
int v
Definition: variant_test.cc:81
static void ResetThreadIdentity(base_internal::ThreadIdentity *identity)
static base_internal::SpinLock freelist_lock(base_internal::kLinkerInitialized)
static void Init(base_internal::ThreadIdentity *identity)
static void * Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook)
std::atomic< int > * blocked_count_ptr
CONSTEXPR_F fields align(second_tag, fields f) noexcept
Definition: algorithm.h:29
static base_internal::ThreadIdentity * thread_identity_freelist
static intptr_t RoundUp(intptr_t addr, intptr_t align)
struct absl::base_internal::ThreadIdentity::WaiterState waiter_state
base_internal::ThreadIdentity * CreateThreadIdentity()
static base_internal::ThreadIdentity * NewThreadIdentity()
void SetCurrentThreadIdentity(ThreadIdentity *identity, ThreadIdentityReclaimerFunction reclaimer)
static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook)
static void ReclaimThreadIdentity(void *v)


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18