summaryrefslogtreecommitdiff
path: root/src/mongo/platform
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/platform')
-rw-r--r--src/mongo/platform/mutex.cpp26
-rw-r--r--src/mongo/platform/mutex.h61
2 files changed, 56 insertions, 31 deletions
diff --git a/src/mongo/platform/mutex.cpp b/src/mongo/platform/mutex.cpp
index 3f2a18fe58a..620afcd2b48 100644
--- a/src/mongo/platform/mutex.cpp
+++ b/src/mongo/platform/mutex.cpp
@@ -33,16 +33,16 @@ namespace mongo {
void Mutex::lock() {
if (_mutex.try_lock()) {
- _onQuickLock(_name);
+ _onQuickLock(_id);
return;
}
- _onContendedLock(_name);
+ _onContendedLock(_id);
_mutex.lock();
- _onSlowLock(_name);
+ _onSlowLock(_id);
}
void Mutex::unlock() {
- _onUnlock(_name);
+ _onUnlock(_id);
_mutex.unlock();
}
bool Mutex::try_lock() {
@@ -50,7 +50,7 @@ bool Mutex::try_lock() {
return false;
}
- _onQuickLock(_name);
+ _onQuickLock(_id);
return true;
}
@@ -60,31 +60,31 @@ void Mutex::addLockListener(LockListener* listener) {
state.list.push_back(listener);
}
-void Mutex::_onContendedLock(const StringData& name) noexcept {
+void Mutex::_onContendedLock(const Identity& id) noexcept {
auto& state = _getListenerState();
for (auto listener : state.list) {
- listener->onContendedLock(name);
+ listener->onContendedLock(id);
}
}
-void Mutex::_onQuickLock(const StringData& name) noexcept {
+void Mutex::_onQuickLock(const Identity& id) noexcept {
auto& state = _getListenerState();
for (auto listener : state.list) {
- listener->onQuickLock(name);
+ listener->onQuickLock(id);
}
}
-void Mutex::_onSlowLock(const StringData& name) noexcept {
+void Mutex::_onSlowLock(const Identity& id) noexcept {
auto& state = _getListenerState();
for (auto listener : state.list) {
- listener->onSlowLock(name);
+ listener->onSlowLock(id);
}
}
-void Mutex::_onUnlock(const StringData& name) noexcept {
+void Mutex::_onUnlock(const Identity& id) noexcept {
auto& state = _getListenerState();
for (auto listener : state.list) {
- listener->onUnlock(name);
+ listener->onUnlock(id);
}
}
diff --git a/src/mongo/platform/mutex.h b/src/mongo/platform/mutex.h
index dd6bd4996a0..ee86cfd8772 100644
--- a/src/mongo/platform/mutex.h
+++ b/src/mongo/platform/mutex.h
@@ -34,8 +34,10 @@
#include "mongo/base/error_codes.h"
#include "mongo/base/string_data.h"
#include "mongo/platform/atomic_word.h"
+#include "mongo/platform/source_location.h"
#include "mongo/stdx/mutex.h"
#include "mongo/util/duration.h"
+#include "mongo/util/hierarchical_acquisition.h"
namespace mongo {
@@ -60,18 +62,40 @@ public:
static constexpr auto kAnonymousMutexStr = "AnonymousMutex"_sd;
- Mutex() : Mutex(kAnonymousMutexStr) {}
- // Note that StringData is a view type, thus the underlying string for _name must outlive any
- // given Mutex
- explicit Mutex(const StringData& name) : _name(name) {}
-
void lock() override;
void unlock() override;
bool try_lock() override;
StringData getName() const override {
- return _name;
+ return StringData(_id.name);
}
+ struct Identity {
+ Identity(StringData name = kAnonymousMutexStr) : Identity(boost::none, boost::none, name) {}
+
+ Identity(SourceLocationHolder sourceLocation, StringData name = kAnonymousMutexStr)
+ : Identity(boost::none, sourceLocation, name) {}
+
+ Identity(hierarchical_acquisition_detail::Level level, StringData name = kAnonymousMutexStr)
+ : Identity(level, boost::none, name) {}
+
+ Identity(boost::optional<hierarchical_acquisition_detail::Level> level,
+ boost::optional<SourceLocationHolder> sourceLocation,
+ StringData name = kAnonymousMutexStr)
+ : level(level), sourceLocation(sourceLocation), name(name.toString()) {}
+
+ boost::optional<hierarchical_acquisition_detail::Level> level;
+ boost::optional<SourceLocationHolder> sourceLocation;
+ std::string name;
+ };
+
+ Mutex() : Mutex(Identity()) {}
+
+ Mutex(const Identity& id) : _id(id) {}
+
+ struct LatchSetState {
+ hierarchical_acquisition_detail::Set levelsHeld;
+ };
+
/**
* This function adds a LockListener subclass to the triggers for certain actions.
*
@@ -93,12 +117,13 @@ private:
return state;
}
- static void _onContendedLock(const StringData& name) noexcept;
- static void _onQuickLock(const StringData& name) noexcept;
- static void _onSlowLock(const StringData& name) noexcept;
- static void _onUnlock(const StringData& name) noexcept;
+ static void _onContendedLock(const Identity& id) noexcept;
+ static void _onQuickLock(const Identity& id) noexcept;
+ static void _onSlowLock(const Identity& id) noexcept;
+ static void _onUnlock(const Identity& id) noexcept;
+
+ const Identity _id;
- const StringData _name;
stdx::mutex _mutex; // NOLINT
};
@@ -114,22 +139,22 @@ public:
/**
* Action to do when a lock cannot be immediately acquired
*/
- virtual void onContendedLock(const StringData& name) = 0;
+ virtual void onContendedLock(const Identity& id) = 0;
/**
* Action to do when a lock was acquired without blocking
*/
- virtual void onQuickLock(const StringData& name) = 0;
+ virtual void onQuickLock(const Identity& id) = 0;
/**
* Action to do when a lock was acquired after blocking
*/
- virtual void onSlowLock(const StringData& name) = 0;
+ virtual void onSlowLock(const Identity& id) = 0;
/**
* Action to do when a lock is unlocked
*/
- virtual void onUnlock(const StringData& name) = 0;
+ virtual void onUnlock(const Identity& id) = 0;
};
} // namespace mongo
@@ -137,7 +162,7 @@ public:
/**
* Define a mongo::Mutex with all arguments passed through to the ctor
*/
-#define MONGO_MAKE_LATCH(...) \
- mongo::Mutex { \
- __VA_ARGS__ \
+#define MONGO_MAKE_LATCH(...) \
+ mongo::Mutex { \
+ mongo::Mutex::Identity(__VA_ARGS__) \
}