summaryrefslogtreecommitdiff
path: root/src/mongo/platform
diff options
context:
space:
mode:
authorRahul Sundararaman <rahul.sundararaman@10gen.com>2019-07-23 13:57:25 -0400
committerRahul Sundararaman <rahul.sundararaman@10gen.com>2019-07-23 15:58:10 -0400
commit5020e611bbffd51bb8cebd3790f8537b7eb4c03c (patch)
tree6b92c60cf3b00eabea37b49cf0de63a48896663d /src/mongo/platform
parentf79c017612cba9db58ca96c74d787678e0660c43 (diff)
downloadmongo-5020e611bbffd51bb8cebd3790f8537b7eb4c03c.tar.gz
SERVER-41362 Attach diagnostic captures to OperationContexts
Diffstat (limited to 'src/mongo/platform')
-rw-r--r--src/mongo/platform/SConscript3
-rw-r--r--src/mongo/platform/mutex.cpp21
-rw-r--r--src/mongo/platform/mutex.h10
-rw-r--r--src/mongo/platform/mutex_test.cpp5
4 files changed, 30 insertions, 9 deletions
diff --git a/src/mongo/platform/SConscript b/src/mongo/platform/SConscript
index 171a183fa45..6ccbf7f77ce 100644
--- a/src/mongo/platform/SConscript
+++ b/src/mongo/platform/SConscript
@@ -17,7 +17,4 @@ env.CppUnitTest(
'decimal128_bson_test.cpp',
'overflow_arithmetic_test.cpp'
],
- LIBDEPS=[
- '$BUILD_DIR/mongo/db/service_context'
- ]
) \ No newline at end of file
diff --git a/src/mongo/platform/mutex.cpp b/src/mongo/platform/mutex.cpp
index c81783f4bd7..b5661cf6f53 100644
--- a/src/mongo/platform/mutex.cpp
+++ b/src/mongo/platform/mutex.cpp
@@ -31,16 +31,35 @@
namespace mongo {
+namespace {
+std::unique_ptr<LockActions> gLockActions;
+}
+
void Mutex::lock() {
- auto hasLock = _mutex.try_lock_for(_lockTimeout.toSystemDuration());
+ auto hasLock = _mutex.try_lock_for(kContendedLockTimeout.toSystemDuration());
+ if (hasLock) {
+ return;
+ }
+ if (gLockActions) {
+ gLockActions->onContendedLock(_name);
+ }
+ hasLock = _mutex.try_lock_for(_lockTimeout.toSystemDuration() -
+ kContendedLockTimeout.toSystemDuration());
uassert(
ErrorCodes::InternalError, "Unable to take latch, wait time exceeds set timeout", hasLock);
}
void Mutex::unlock() {
+ if (gLockActions) {
+ gLockActions->onUnlock();
+ }
_mutex.unlock();
}
bool Mutex::try_lock() {
return _mutex.try_lock();
}
+void Mutex::setLockActions(std::unique_ptr<LockActions> actions) {
+ gLockActions = std::move(actions);
+}
+
} // namespace mongo
diff --git a/src/mongo/platform/mutex.h b/src/mongo/platform/mutex.h
index a0be7ae971d..bb885d0cbe1 100644
--- a/src/mongo/platform/mutex.h
+++ b/src/mongo/platform/mutex.h
@@ -36,6 +36,13 @@
namespace mongo {
+class LockActions {
+public:
+ virtual ~LockActions() = default;
+ virtual void onContendedLock(const StringData& name) = 0;
+ virtual void onUnlock() = 0;
+};
+
class Mutex {
public:
Mutex() : Mutex("AnonymousMutex"_sd) {}
@@ -48,9 +55,12 @@ public:
return _name;
}
+ static void setLockActions(std::unique_ptr<LockActions> actions);
+
private:
const StringData _name;
const Seconds _lockTimeout = Seconds(60);
+ static constexpr Milliseconds kContendedLockTimeout = Milliseconds(100);
stdx::timed_mutex _mutex;
};
diff --git a/src/mongo/platform/mutex_test.cpp b/src/mongo/platform/mutex_test.cpp
index 895914c529b..6c6674aa197 100644
--- a/src/mongo/platform/mutex_test.cpp
+++ b/src/mongo/platform/mutex_test.cpp
@@ -29,15 +29,10 @@
#include "mongo/unittest/unittest.h"
-#include "mongo/db/service_context.h"
#include "mongo/platform/mutex.h"
-#include "mongo/stdx/thread.h"
namespace mongo {
TEST(MongoMutexTest, BasicSingleThread) {
- auto serviceContext = ServiceContext::make();
- setGlobalServiceContext(std::move(serviceContext));
-
Mutex m;
m.lock();
ASSERT(!m.try_lock());