Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit c20fc94

Browse files
replace tbb::reader_writer_lock with std::shared_mutex in SharedLatch implementation (#1441)
Co-authored-by: Lin Ma <[email protected]>
1 parent ce5f676 commit c20fc94

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/include/common/shared_latch.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
#pragma once
22

3-
#include <tbb/reader_writer_lock.h>
3+
#include <shared_mutex>
44

55
#include "common/macros.h"
66

77
namespace noisepage::common {
88

99
/**
10-
* A cheap and easy shared (reader-writer) latch, currently wraps tbb::reader_writer_lock. From Intel's docs:
11-
*
12-
* A reader_writer_lock is scalable and nonrecursive. The implementation handles lock requests on a first-come
13-
* first-serve basis except that writers have preference over readers. Waiting threads busy wait, which can degrade
14-
* system performance if the wait is long. However, if the wait is typically short, a reader_writer_lock can provide
15-
* performance competitive with other mutexes.
10+
* A cheap(?) and easy shared (reader-writer) latch, currently wraps std::shared_mutex.
1611
*/
1712
class SharedLatch {
1813
public:
@@ -24,7 +19,7 @@ class SharedLatch {
2419
/**
2520
* Acquire shared lock on mutex.
2621
*/
27-
void LockShared() { latch_.lock_read(); }
22+
void LockShared() { latch_.lock_shared(); }
2823

2924
/**
3025
* Try to acquire exclusive lock on mutex.
@@ -36,12 +31,17 @@ class SharedLatch {
3631
* Try to acquire shared lock on mutex.
3732
* @return true if lock acquired, false otherwise.
3833
*/
39-
bool TryLockShared() { return latch_.try_lock_read(); }
34+
bool TryLockShared() { return latch_.try_lock_shared(); }
4035

4136
/**
42-
* Release lock.
37+
* Release exclusive ownership of lock.
4338
*/
44-
void Unlock() { latch_.unlock(); }
39+
void UnlockExclusive() { latch_.unlock(); }
40+
41+
/**
42+
* Release shared ownership of lock.
43+
*/
44+
void UnlockShared() { latch_.unlock_shared(); }
4545

4646
/**
4747
* Scoped read latch that guarantees releasing the latch when destructed.
@@ -56,7 +56,7 @@ class SharedLatch {
5656
/**
5757
* Release write lock (if acquired).
5858
*/
59-
~ScopedSharedLatch() { rw_latch_->Unlock(); }
59+
~ScopedSharedLatch() { rw_latch_->UnlockShared(); }
6060
DISALLOW_COPY_AND_MOVE(ScopedSharedLatch)
6161

6262
private:
@@ -76,14 +76,14 @@ class SharedLatch {
7676
/**
7777
* Release read lock (if acquired).
7878
*/
79-
~ScopedExclusiveLatch() { rw_latch_->Unlock(); }
79+
~ScopedExclusiveLatch() { rw_latch_->UnlockExclusive(); }
8080
DISALLOW_COPY_AND_MOVE(ScopedExclusiveLatch)
8181
private:
8282
SharedLatch *const rw_latch_;
8383
};
8484

8585
private:
86-
tbb::reader_writer_lock latch_;
86+
std::shared_mutex latch_;
8787
};
8888

8989
} // namespace noisepage::common

0 commit comments

Comments
 (0)