8
8
// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
9
10
10
#include " util/thread_local.h"
11
-
12
- #include < mutex>
13
-
14
11
#include " util/mutexlock.h"
15
12
#include " port/likely.h"
16
13
14
+
17
15
namespace rocksdb {
18
16
19
17
std::unique_ptr<ThreadLocalPtr::StaticMeta> ThreadLocalPtr::StaticMeta::inst_;
20
- std::mutex ThreadLocalPtr::StaticMeta::mutex_;
18
+ port::Mutex ThreadLocalPtr::StaticMeta::mutex_;
21
19
#if !defined(OS_MACOSX)
22
20
__thread ThreadLocalPtr::ThreadData* ThreadLocalPtr::StaticMeta::tls_ = nullptr ;
23
21
#endif
24
22
25
23
ThreadLocalPtr::StaticMeta* ThreadLocalPtr::StaticMeta::Instance () {
26
24
if (UNLIKELY (inst_ == nullptr )) {
27
- std::lock_guard<std::mutex> l (mutex_);
25
+ MutexLock l (& mutex_);
28
26
if (inst_ == nullptr ) {
29
27
inst_.reset (new StaticMeta ());
30
28
}
@@ -39,7 +37,7 @@ void ThreadLocalPtr::StaticMeta::OnThreadExit(void* ptr) {
39
37
auto * inst = Instance ();
40
38
pthread_setspecific (inst->pthread_key_ , nullptr );
41
39
42
- std::lock_guard<std::mutex> l (mutex_);
40
+ MutexLock l (& mutex_);
43
41
inst->RemoveThreadData (tls);
44
42
// Unref stored pointers of current thread from all instances
45
43
uint32_t id = 0 ;
@@ -66,6 +64,7 @@ ThreadLocalPtr::StaticMeta::StaticMeta() : next_instance_id_(0) {
66
64
}
67
65
68
66
void ThreadLocalPtr::StaticMeta::AddThreadData (ThreadLocalPtr::ThreadData* d) {
67
+ mutex_.AssertHeld ();
69
68
d->next = &head_;
70
69
d->prev = head_.prev ;
71
70
head_.prev ->next = d;
@@ -74,6 +73,7 @@ void ThreadLocalPtr::StaticMeta::AddThreadData(ThreadLocalPtr::ThreadData* d) {
74
73
75
74
void ThreadLocalPtr::StaticMeta::RemoveThreadData (
76
75
ThreadLocalPtr::ThreadData* d) {
76
+ mutex_.AssertHeld ();
77
77
d->next ->prev = d->prev ;
78
78
d->prev ->next = d->next ;
79
79
d->next = d->prev = d;
@@ -93,14 +93,14 @@ ThreadLocalPtr::ThreadData* ThreadLocalPtr::StaticMeta::GetThreadLocal() {
93
93
{
94
94
// Register it in the global chain, needs to be done before thread exit
95
95
// handler registration
96
- std::lock_guard<std::mutex> l (mutex_);
96
+ MutexLock l (& mutex_);
97
97
inst->AddThreadData (tls_);
98
98
}
99
99
// Even it is not OS_MACOSX, need to register value for pthread_key_ so that
100
100
// its exit handler will be triggered.
101
101
if (pthread_setspecific (inst->pthread_key_ , tls_) != 0 ) {
102
102
{
103
- std::lock_guard<std::mutex> l (mutex_);
103
+ MutexLock l (& mutex_);
104
104
inst->RemoveThreadData (tls_);
105
105
}
106
106
delete tls_;
@@ -122,7 +122,7 @@ void ThreadLocalPtr::StaticMeta::Reset(uint32_t id, void* ptr) {
122
122
auto * tls = GetThreadLocal ();
123
123
if (UNLIKELY (id >= tls->entries .size ())) {
124
124
// Need mutex to protect entries access within ReclaimId
125
- std::lock_guard<std::mutex> l (mutex_);
125
+ MutexLock l (& mutex_);
126
126
tls->entries .resize (id + 1 );
127
127
}
128
128
tls->entries [id].ptr .store (ptr, std::memory_order_relaxed);
@@ -132,7 +132,7 @@ void* ThreadLocalPtr::StaticMeta::Swap(uint32_t id, void* ptr) {
132
132
auto * tls = GetThreadLocal ();
133
133
if (UNLIKELY (id >= tls->entries .size ())) {
134
134
// Need mutex to protect entries access within ReclaimId
135
- std::lock_guard<std::mutex> l (mutex_);
135
+ MutexLock l (& mutex_);
136
136
tls->entries .resize (id + 1 );
137
137
}
138
138
return tls->entries [id].ptr .exchange (ptr, std::memory_order_relaxed);
@@ -143,7 +143,7 @@ bool ThreadLocalPtr::StaticMeta::CompareAndSwap(uint32_t id, void* ptr,
143
143
auto * tls = GetThreadLocal ();
144
144
if (UNLIKELY (id >= tls->entries .size ())) {
145
145
// Need mutex to protect entries access within ReclaimId
146
- std::lock_guard<std::mutex> l (mutex_);
146
+ MutexLock l (& mutex_);
147
147
tls->entries .resize (id + 1 );
148
148
}
149
149
return tls->entries [id].ptr .compare_exchange_strong (expected, ptr,
@@ -152,7 +152,7 @@ bool ThreadLocalPtr::StaticMeta::CompareAndSwap(uint32_t id, void* ptr,
152
152
153
153
void ThreadLocalPtr::StaticMeta::Scrape (uint32_t id, autovector<void *>* ptrs,
154
154
void * const replacement) {
155
- std::lock_guard<std::mutex> l (mutex_);
155
+ MutexLock l (& mutex_);
156
156
for (ThreadData* t = head_.next ; t != &head_; t = t->next ) {
157
157
if (id < t->entries .size ()) {
158
158
void * ptr =
@@ -165,11 +165,12 @@ void ThreadLocalPtr::StaticMeta::Scrape(uint32_t id, autovector<void*>* ptrs,
165
165
}
166
166
167
167
void ThreadLocalPtr::StaticMeta::SetHandler (uint32_t id, UnrefHandler handler) {
168
- std::lock_guard<std::mutex> l (mutex_);
168
+ MutexLock l (& mutex_);
169
169
handler_map_[id] = handler;
170
170
}
171
171
172
172
UnrefHandler ThreadLocalPtr::StaticMeta::GetHandler (uint32_t id) {
173
+ mutex_.AssertHeld ();
173
174
auto iter = handler_map_.find (id);
174
175
if (iter == handler_map_.end ()) {
175
176
return nullptr ;
@@ -178,7 +179,7 @@ UnrefHandler ThreadLocalPtr::StaticMeta::GetHandler(uint32_t id) {
178
179
}
179
180
180
181
uint32_t ThreadLocalPtr::StaticMeta::GetId () {
181
- std::lock_guard<std::mutex> l (mutex_);
182
+ MutexLock l (& mutex_);
182
183
if (free_instance_ids_.empty ()) {
183
184
return next_instance_id_++;
184
185
}
@@ -189,7 +190,7 @@ uint32_t ThreadLocalPtr::StaticMeta::GetId() {
189
190
}
190
191
191
192
uint32_t ThreadLocalPtr::StaticMeta::PeekId () const {
192
- std::lock_guard<std::mutex> l (mutex_);
193
+ MutexLock l (& mutex_);
193
194
if (!free_instance_ids_.empty ()) {
194
195
return free_instance_ids_.back ();
195
196
}
@@ -199,7 +200,7 @@ uint32_t ThreadLocalPtr::StaticMeta::PeekId() const {
199
200
void ThreadLocalPtr::StaticMeta::ReclaimId (uint32_t id) {
200
201
// This id is not used, go through all thread local data and release
201
202
// corresponding value
202
- std::lock_guard<std::mutex> l (mutex_);
203
+ MutexLock l (& mutex_);
203
204
auto unref = GetHandler (id);
204
205
for (ThreadData* t = head_.next ; t != &head_; t = t->next ) {
205
206
if (id < t->entries .size ()) {
0 commit comments