/src/abseil-cpp/absl/synchronization/internal/create_thread_identity.cc
Line | Count | Source (jump to first uncovered line) |
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 | | absl::kConstInit, 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 | 2 | static intptr_t RoundUp(intptr_t addr, intptr_t align) { |
72 | 2 | return (addr + align - 1) & ~(align - 1); |
73 | 2 | } |
74 | | |
75 | 2 | void OneTimeInitThreadIdentity(base_internal::ThreadIdentity* identity) { |
76 | 2 | PerThreadSem::Init(identity); |
77 | 2 | identity->ticker.store(0, std::memory_order_relaxed); |
78 | 2 | identity->wait_start.store(0, std::memory_order_relaxed); |
79 | 2 | identity->is_idle.store(false, std::memory_order_relaxed); |
80 | 2 | } |
81 | | |
82 | | static void ResetThreadIdentityBetweenReuse( |
83 | 2 | base_internal::ThreadIdentity* identity) { |
84 | 2 | base_internal::PerThreadSynch* pts = &identity->per_thread_synch; |
85 | 2 | pts->next = nullptr; |
86 | 2 | pts->skip = nullptr; |
87 | 2 | pts->may_skip = false; |
88 | 2 | pts->waitp = nullptr; |
89 | 2 | pts->suppress_fatal_errors = false; |
90 | 2 | pts->readers = 0; |
91 | 2 | pts->priority = 0; |
92 | 2 | pts->next_priority_read_cycles = 0; |
93 | 2 | pts->state.store(base_internal::PerThreadSynch::State::kAvailable, |
94 | 2 | std::memory_order_relaxed); |
95 | 2 | pts->maybe_unlocking = false; |
96 | 2 | pts->wake = false; |
97 | 2 | pts->cond_waiter = false; |
98 | 2 | pts->all_locks = nullptr; |
99 | 2 | identity->blocked_count_ptr = nullptr; |
100 | 2 | identity->ticker.store(0, std::memory_order_relaxed); |
101 | 2 | identity->wait_start.store(0, std::memory_order_relaxed); |
102 | 2 | identity->is_idle.store(false, std::memory_order_relaxed); |
103 | 2 | identity->next = nullptr; |
104 | 2 | } |
105 | | |
106 | 2 | static base_internal::ThreadIdentity* NewThreadIdentity() { |
107 | 2 | base_internal::ThreadIdentity* identity = nullptr; |
108 | | |
109 | 2 | { |
110 | | // Re-use a previously released object if possible. |
111 | 2 | base_internal::SpinLockHolder l(&freelist_lock); |
112 | 2 | if (thread_identity_freelist) { |
113 | 0 | identity = thread_identity_freelist; // Take list-head. |
114 | 0 | thread_identity_freelist = thread_identity_freelist->next; |
115 | 0 | } |
116 | 2 | } |
117 | | |
118 | 2 | if (identity == nullptr) { |
119 | | // Allocate enough space to align ThreadIdentity to a multiple of |
120 | | // PerThreadSynch::kAlignment. This space is never released (it is |
121 | | // added to a freelist by ReclaimThreadIdentity instead). |
122 | 2 | void* allocation = base_internal::LowLevelAlloc::Alloc( |
123 | 2 | sizeof(*identity) + base_internal::PerThreadSynch::kAlignment - 1); |
124 | | // Round up the address to the required alignment. |
125 | 2 | identity = reinterpret_cast<base_internal::ThreadIdentity*>( |
126 | 2 | RoundUp(reinterpret_cast<intptr_t>(allocation), |
127 | 2 | base_internal::PerThreadSynch::kAlignment)); |
128 | | // Note that *identity is never constructed. |
129 | | // TODO(b/357097463): change this "one time init" to be a proper |
130 | | // constructor. |
131 | 2 | OneTimeInitThreadIdentity(identity); |
132 | 2 | } |
133 | 2 | ResetThreadIdentityBetweenReuse(identity); |
134 | | |
135 | 2 | return identity; |
136 | 2 | } |
137 | | |
138 | | // Allocates and attaches ThreadIdentity object for the calling thread. Returns |
139 | | // the new identity. |
140 | | // REQUIRES: CurrentThreadIdentity(false) == nullptr |
141 | 2 | base_internal::ThreadIdentity* CreateThreadIdentity() { |
142 | 2 | base_internal::ThreadIdentity* identity = NewThreadIdentity(); |
143 | | // Associate the value with the current thread, and attach our destructor. |
144 | 2 | base_internal::SetCurrentThreadIdentity(identity, ReclaimThreadIdentity); |
145 | 2 | return identity; |
146 | 2 | } |
147 | | |
148 | | } // namespace synchronization_internal |
149 | | ABSL_NAMESPACE_END |
150 | | } // namespace absl |
151 | | |
152 | | #endif // ABSL_LOW_LEVEL_ALLOC_MISSING |