Skip to content

Commit 1068d2f

Browse files
committed
Revert "Better port::Mutex::AssertHeld() and AssertNotHeld()"
This reverts commit ddafceb.
1 parent ddafceb commit 1068d2f

File tree

5 files changed

+33
-47
lines changed

5 files changed

+33
-47
lines changed

db/db_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class DBTest {
415415
options.db_log_dir = test::TmpDir();
416416
break;
417417
case kWalDir:
418-
options.wal_dir = test::TmpDir() + "/wal_dir";
418+
options.wal_dir = "/tmp/wal";
419419
break;
420420
case kManifestFileSize:
421421
options.max_manifest_file_size = 50; // 50 bytes

port/port_posix.cc

+5-16
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
#include "port/port_posix.h"
1111

12-
#include <memory>
1312
#include <cstdlib>
1413
#include <stdio.h>
1514
#include <assert.h>
1615
#include <string.h>
1716
#include "util/logging.h"
18-
#include "util/thread_local.h"
1917

2018
namespace rocksdb {
2119
namespace port {
@@ -28,9 +26,6 @@ static void PthreadCall(const char* label, int result) {
2826
}
2927

3028
Mutex::Mutex(bool adaptive) {
31-
#ifndef NDEBUG
32-
locked_.reset(new ThreadLocalPtr());
33-
#endif
3429
#ifdef OS_LINUX
3530
if (!adaptive) {
3631
PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL));
@@ -54,26 +49,20 @@ Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
5449
void Mutex::Lock() {
5550
PthreadCall("lock", pthread_mutex_lock(&mu_));
5651
#ifndef NDEBUG
57-
locked_->Reset(this);
52+
locked_ = true;
5853
#endif
5954
}
6055

6156
void Mutex::Unlock() {
6257
#ifndef NDEBUG
63-
locked_->Reset(nullptr);
58+
locked_ = false;
6459
#endif
6560
PthreadCall("unlock", pthread_mutex_unlock(&mu_));
6661
}
6762

6863
void Mutex::AssertHeld() {
6964
#ifndef NDEBUG
70-
assert(locked_->Get() == this);
71-
#endif
72-
}
73-
74-
void Mutex::AssertNotHeld() {
75-
#ifndef NDEBUG
76-
assert(locked_->Get() == nullptr);
65+
assert(locked_);
7766
#endif
7867
}
7968

@@ -86,11 +75,11 @@ CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
8675

8776
void CondVar::Wait() {
8877
#ifndef NDEBUG
89-
mu_->locked_->Reset(nullptr);
78+
mu_->locked_ = false;
9079
#endif
9180
PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
9281
#ifndef NDEBUG
93-
mu_->locked_->Reset(mu_);
82+
mu_->locked_ = true;
9483
#endif
9584
}
9685

port/port_posix.h

+7-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
//
1010
// See port_example.h for documentation for the following types/functions.
1111

12-
#pragma once
12+
#ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_
13+
#define STORAGE_LEVELDB_PORT_PORT_POSIX_H_
1314

1415
#undef PLATFORM_IS_LITTLE_ENDIAN
1516
#if defined(OS_MACOSX)
@@ -50,7 +51,6 @@
5051
#include <lz4hc.h>
5152
#endif
5253

53-
#include <memory>
5454
#include <stdint.h>
5555
#include <string>
5656
#include <string.h>
@@ -83,36 +83,29 @@
8383
#endif
8484

8585
namespace rocksdb {
86-
87-
class ThreadLocalPtr;
88-
8986
namespace port {
9087

9188
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
9289
#undef PLATFORM_IS_LITTLE_ENDIAN
9390

9491
class CondVar;
9592

96-
// DO NOT declare this Mutex as static ever. Inside it depends on ThreadLocalPtr
97-
// and its Lock() and Unlock() function depend on ThreadLocalPtr::StaticMeta,
98-
// which is also declared static. We can't really control static
99-
// deinitialization order.
10093
class Mutex {
10194
public:
10295
/* implicit */ Mutex(bool adaptive = false);
10396
~Mutex();
10497

10598
void Lock();
10699
void Unlock();
107-
100+
// this will assert if the mutex is not locked
101+
// it does NOT verify that mutex is held by a calling thread
108102
void AssertHeld();
109-
void AssertNotHeld();
110103

111104
private:
112105
friend class CondVar;
113106
pthread_mutex_t mu_;
114107
#ifndef NDEBUG
115-
std::unique_ptr<ThreadLocalPtr> locked_;
108+
bool locked_;
116109
#endif
117110

118111
// No copying
@@ -487,3 +480,5 @@ inline bool LZ4HC_Compress(const CompressionOptions &opts, const char* input,
487480

488481
} // namespace port
489482
} // namespace rocksdb
483+
484+
#endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_

util/thread_local.cc

+17-16
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
// found in the LICENSE file. See the AUTHORS file for names of contributors.
99

1010
#include "util/thread_local.h"
11-
12-
#include <mutex>
13-
1411
#include "util/mutexlock.h"
1512
#include "port/likely.h"
1613

14+
1715
namespace rocksdb {
1816

1917
std::unique_ptr<ThreadLocalPtr::StaticMeta> ThreadLocalPtr::StaticMeta::inst_;
20-
std::mutex ThreadLocalPtr::StaticMeta::mutex_;
18+
port::Mutex ThreadLocalPtr::StaticMeta::mutex_;
2119
#if !defined(OS_MACOSX)
2220
__thread ThreadLocalPtr::ThreadData* ThreadLocalPtr::StaticMeta::tls_ = nullptr;
2321
#endif
2422

2523
ThreadLocalPtr::StaticMeta* ThreadLocalPtr::StaticMeta::Instance() {
2624
if (UNLIKELY(inst_ == nullptr)) {
27-
std::lock_guard<std::mutex> l(mutex_);
25+
MutexLock l(&mutex_);
2826
if (inst_ == nullptr) {
2927
inst_.reset(new StaticMeta());
3028
}
@@ -39,7 +37,7 @@ void ThreadLocalPtr::StaticMeta::OnThreadExit(void* ptr) {
3937
auto* inst = Instance();
4038
pthread_setspecific(inst->pthread_key_, nullptr);
4139

42-
std::lock_guard<std::mutex> l(mutex_);
40+
MutexLock l(&mutex_);
4341
inst->RemoveThreadData(tls);
4442
// Unref stored pointers of current thread from all instances
4543
uint32_t id = 0;
@@ -66,6 +64,7 @@ ThreadLocalPtr::StaticMeta::StaticMeta() : next_instance_id_(0) {
6664
}
6765

6866
void ThreadLocalPtr::StaticMeta::AddThreadData(ThreadLocalPtr::ThreadData* d) {
67+
mutex_.AssertHeld();
6968
d->next = &head_;
7069
d->prev = head_.prev;
7170
head_.prev->next = d;
@@ -74,6 +73,7 @@ void ThreadLocalPtr::StaticMeta::AddThreadData(ThreadLocalPtr::ThreadData* d) {
7473

7574
void ThreadLocalPtr::StaticMeta::RemoveThreadData(
7675
ThreadLocalPtr::ThreadData* d) {
76+
mutex_.AssertHeld();
7777
d->next->prev = d->prev;
7878
d->prev->next = d->next;
7979
d->next = d->prev = d;
@@ -93,14 +93,14 @@ ThreadLocalPtr::ThreadData* ThreadLocalPtr::StaticMeta::GetThreadLocal() {
9393
{
9494
// Register it in the global chain, needs to be done before thread exit
9595
// handler registration
96-
std::lock_guard<std::mutex> l(mutex_);
96+
MutexLock l(&mutex_);
9797
inst->AddThreadData(tls_);
9898
}
9999
// Even it is not OS_MACOSX, need to register value for pthread_key_ so that
100100
// its exit handler will be triggered.
101101
if (pthread_setspecific(inst->pthread_key_, tls_) != 0) {
102102
{
103-
std::lock_guard<std::mutex> l(mutex_);
103+
MutexLock l(&mutex_);
104104
inst->RemoveThreadData(tls_);
105105
}
106106
delete tls_;
@@ -122,7 +122,7 @@ void ThreadLocalPtr::StaticMeta::Reset(uint32_t id, void* ptr) {
122122
auto* tls = GetThreadLocal();
123123
if (UNLIKELY(id >= tls->entries.size())) {
124124
// Need mutex to protect entries access within ReclaimId
125-
std::lock_guard<std::mutex> l(mutex_);
125+
MutexLock l(&mutex_);
126126
tls->entries.resize(id + 1);
127127
}
128128
tls->entries[id].ptr.store(ptr, std::memory_order_relaxed);
@@ -132,7 +132,7 @@ void* ThreadLocalPtr::StaticMeta::Swap(uint32_t id, void* ptr) {
132132
auto* tls = GetThreadLocal();
133133
if (UNLIKELY(id >= tls->entries.size())) {
134134
// Need mutex to protect entries access within ReclaimId
135-
std::lock_guard<std::mutex> l(mutex_);
135+
MutexLock l(&mutex_);
136136
tls->entries.resize(id + 1);
137137
}
138138
return tls->entries[id].ptr.exchange(ptr, std::memory_order_relaxed);
@@ -143,7 +143,7 @@ bool ThreadLocalPtr::StaticMeta::CompareAndSwap(uint32_t id, void* ptr,
143143
auto* tls = GetThreadLocal();
144144
if (UNLIKELY(id >= tls->entries.size())) {
145145
// Need mutex to protect entries access within ReclaimId
146-
std::lock_guard<std::mutex> l(mutex_);
146+
MutexLock l(&mutex_);
147147
tls->entries.resize(id + 1);
148148
}
149149
return tls->entries[id].ptr.compare_exchange_strong(expected, ptr,
@@ -152,7 +152,7 @@ bool ThreadLocalPtr::StaticMeta::CompareAndSwap(uint32_t id, void* ptr,
152152

153153
void ThreadLocalPtr::StaticMeta::Scrape(uint32_t id, autovector<void*>* ptrs,
154154
void* const replacement) {
155-
std::lock_guard<std::mutex> l(mutex_);
155+
MutexLock l(&mutex_);
156156
for (ThreadData* t = head_.next; t != &head_; t = t->next) {
157157
if (id < t->entries.size()) {
158158
void* ptr =
@@ -165,11 +165,12 @@ void ThreadLocalPtr::StaticMeta::Scrape(uint32_t id, autovector<void*>* ptrs,
165165
}
166166

167167
void ThreadLocalPtr::StaticMeta::SetHandler(uint32_t id, UnrefHandler handler) {
168-
std::lock_guard<std::mutex> l(mutex_);
168+
MutexLock l(&mutex_);
169169
handler_map_[id] = handler;
170170
}
171171

172172
UnrefHandler ThreadLocalPtr::StaticMeta::GetHandler(uint32_t id) {
173+
mutex_.AssertHeld();
173174
auto iter = handler_map_.find(id);
174175
if (iter == handler_map_.end()) {
175176
return nullptr;
@@ -178,7 +179,7 @@ UnrefHandler ThreadLocalPtr::StaticMeta::GetHandler(uint32_t id) {
178179
}
179180

180181
uint32_t ThreadLocalPtr::StaticMeta::GetId() {
181-
std::lock_guard<std::mutex> l(mutex_);
182+
MutexLock l(&mutex_);
182183
if (free_instance_ids_.empty()) {
183184
return next_instance_id_++;
184185
}
@@ -189,7 +190,7 @@ uint32_t ThreadLocalPtr::StaticMeta::GetId() {
189190
}
190191

191192
uint32_t ThreadLocalPtr::StaticMeta::PeekId() const {
192-
std::lock_guard<std::mutex> l(mutex_);
193+
MutexLock l(&mutex_);
193194
if (!free_instance_ids_.empty()) {
194195
return free_instance_ids_.back();
195196
}
@@ -199,7 +200,7 @@ uint32_t ThreadLocalPtr::StaticMeta::PeekId() const {
199200
void ThreadLocalPtr::StaticMeta::ReclaimId(uint32_t id) {
200201
// This id is not used, go through all thread local data and release
201202
// corresponding value
202-
std::lock_guard<std::mutex> l(mutex_);
203+
MutexLock l(&mutex_);
203204
auto unref = GetHandler(id);
204205
for (ThreadData* t = head_.next; t != &head_; t = t->next) {
205206
if (id < t->entries.size()) {

util/thread_local.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
#pragma once
1111

1212
#include <atomic>
13-
#include <mutex>
1413
#include <memory>
1514
#include <unordered_map>
1615
#include <vector>
1716

1817
#include "util/autovector.h"
18+
#include "port/port_posix.h"
19+
#include "util/thread_local.h"
1920

2021
namespace rocksdb {
2122

@@ -152,7 +153,7 @@ class ThreadLocalPtr {
152153

153154
// protect inst, next_instance_id_, free_instance_ids_, head_,
154155
// ThreadData.entries
155-
static std::mutex mutex_;
156+
static port::Mutex mutex_;
156157
#if !defined(OS_MACOSX)
157158
// Thread local storage
158159
static __thread ThreadData* tls_;

0 commit comments

Comments
 (0)