-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: Introduce MutexProtected and SharedMutexProtected (#9115)
ref #6233 Signed-off-by: Lloyd-Pottiger <[email protected]> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bf69caf
commit 1ee7da7
Showing
6 changed files
with
266 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <Common/nocopyable.h> | ||
#include <common/defines.h> | ||
#include <common/types.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
template <typename T> | ||
class MutexProtected | ||
{ | ||
DISALLOW_COPY_AND_MOVE(MutexProtected); | ||
|
||
private: | ||
template <typename U> | ||
class Locked | ||
{ | ||
DISALLOW_COPY_AND_MOVE(Locked); | ||
|
||
public: | ||
Locked(U & value_, std::mutex & mutex_) | ||
: value(value_) | ||
, locker(mutex_) | ||
{} | ||
|
||
ALWAYS_INLINE inline U const * operator->() const { return &value; } | ||
ALWAYS_INLINE inline U const & operator*() const { return value; } | ||
|
||
ALWAYS_INLINE inline U * operator->() { return &value; } | ||
ALWAYS_INLINE inline U & operator*() { return value; } | ||
|
||
ALWAYS_INLINE inline U const & get() const { return value; } | ||
ALWAYS_INLINE inline U & get() { return value; } | ||
|
||
private: | ||
U & value; | ||
std::scoped_lock<std::mutex> locker; | ||
}; | ||
|
||
auto lockConst() const { return Locked<T const>(value, mutex); } | ||
auto lockMutable() { return Locked<T>(value, mutex); } | ||
|
||
public: | ||
template <typename... Args> | ||
explicit MutexProtected(Args &&... args) | ||
: value(forward<Args>(args)...) | ||
{} | ||
|
||
template <typename Callback> | ||
decltype(auto) with(Callback callback) const | ||
{ | ||
auto lock = lockConst(); | ||
return callback(*lock); | ||
} | ||
|
||
template <typename Callback> | ||
decltype(auto) with(Callback callback) | ||
{ | ||
auto lock = lockMutable(); | ||
return callback(*lock); | ||
} | ||
|
||
template <typename Callback> | ||
void forEachConst(Callback callback) const | ||
{ | ||
with([&](auto const & value) { | ||
for (auto & item : value) | ||
callback(item); | ||
}); | ||
} | ||
|
||
template <typename Callback> | ||
void forEach(Callback callback) | ||
{ | ||
with([&](auto & value) { | ||
for (auto & item : value) | ||
callback(item); | ||
}); | ||
} | ||
|
||
private: | ||
T value; | ||
mutable std::mutex mutex; | ||
}; | ||
|
||
} // namespace DB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <Common/nocopyable.h> | ||
#include <common/defines.h> | ||
#include <common/types.h> | ||
|
||
#include <shared_mutex> | ||
|
||
namespace DB | ||
{ | ||
|
||
template <typename T> | ||
class SharedMutexProtected | ||
{ | ||
DISALLOW_COPY_AND_MOVE(SharedMutexProtected); | ||
|
||
private: | ||
template <typename U, typename Lock> | ||
class Locked | ||
{ | ||
DISALLOW_COPY_AND_MOVE(Locked); | ||
|
||
public: | ||
Locked(U & value_, std::shared_mutex & mutex_) | ||
: value(value_) | ||
, locker(mutex_) | ||
{} | ||
|
||
ALWAYS_INLINE inline U const * operator->() const { return &value; } | ||
ALWAYS_INLINE inline U const & operator*() const { return value; } | ||
|
||
ALWAYS_INLINE inline U * operator->() | ||
requires(!std::is_const_v<U>) | ||
{ | ||
return &value; | ||
} | ||
ALWAYS_INLINE inline U & operator*() | ||
requires(!std::is_const_v<U>) | ||
{ | ||
return value; | ||
} | ||
|
||
ALWAYS_INLINE inline U const & get() const { return &value; } | ||
ALWAYS_INLINE inline U & get() | ||
requires(!std::is_const_v<U>) | ||
{ | ||
return &value; | ||
} | ||
|
||
private: | ||
U & value; | ||
Lock locker; | ||
}; | ||
|
||
public: | ||
// Return a locked object that can be used to shared access the protected value. | ||
// Please destroy the object ASAP to release the lock. | ||
auto lockShared() const { return Locked<T const, std::shared_lock<std::shared_mutex>>(value, mutex); } | ||
// Return a locked object that can be used to exclusive access the protected value. | ||
// Please destroy the object ASAP to release the lock. | ||
auto lockExclusive() { return Locked<T, std::unique_lock<std::shared_mutex>>(value, mutex); } | ||
|
||
SharedMutexProtected() = default; | ||
|
||
template <typename Callback> | ||
decltype(auto) withShared(Callback callback) const | ||
{ | ||
auto lock = lockShared(); | ||
return callback(*lock); | ||
} | ||
|
||
template <typename Callback> | ||
decltype(auto) withExclusive(Callback callback) | ||
{ | ||
auto lock = lockExclusive(); | ||
return callback(*lock); | ||
} | ||
|
||
template <typename Callback> | ||
void forEachShared(Callback callback) const | ||
{ | ||
withShared([&](auto const & value) { | ||
for (auto & item : value) | ||
callback(item); | ||
}); | ||
} | ||
|
||
template <typename Callback> | ||
void forEachExclusive(Callback callback) | ||
{ | ||
withExclusive([&](auto & value) { | ||
for (auto & item : value) | ||
callback(item); | ||
}); | ||
} | ||
|
||
private: | ||
T value; | ||
mutable std::shared_mutex mutex; | ||
}; | ||
|
||
} // namespace DB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters