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