summaryrefslogtreecommitdiff
path: root/src/mongo/platform
diff options
context:
space:
mode:
authorRahul Sundararaman <rahul.sundararaman@10gen.com>2019-07-10 11:48:53 -0400
committerRahul Sundararaman <rahul.sundararaman@10gen.com>2019-07-10 11:48:53 -0400
commit724c7f44aa86bab796de7f4a1474785e971f995f (patch)
tree1dc43cbcd81e9977c2486040b53fe06d4e97a73a /src/mongo/platform
parent4f2383c9041598dbf19523c43c170ee01f840145 (diff)
downloadmongo-724c7f44aa86bab796de7f4a1474785e971f995f.tar.gz
SERVER-41357 Add mongo::mutex polyfill
Diffstat (limited to 'src/mongo/platform')
-rw-r--r--src/mongo/platform/SConscript8
-rw-r--r--src/mongo/platform/mutex.cpp46
-rw-r--r--src/mongo/platform/mutex.h57
-rw-r--r--src/mongo/platform/mutex_test.cpp48
4 files changed, 156 insertions, 3 deletions
diff --git a/src/mongo/platform/SConscript b/src/mongo/platform/SConscript
index e6ec2c4d004..171a183fa45 100644
--- a/src/mongo/platform/SConscript
+++ b/src/mongo/platform/SConscript
@@ -1,9 +1,7 @@
# -*- mode: python -*-
Import("env")
-
env = env.Clone()
-
env.CppUnitTest(
target='platform_test',
source=[
@@ -11,6 +9,7 @@ env.CppUnitTest(
'atomic_word_test.cpp',
'bits_test.cpp',
'endian_test.cpp',
+ 'mutex_test.cpp',
'process_id_test.cpp',
'random_test.cpp',
'stack_locator_test.cpp',
@@ -18,4 +17,7 @@ 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
new file mode 100644
index 00000000000..5caba1a02c0
--- /dev/null
+++ b/src/mongo/platform/mutex.cpp
@@ -0,0 +1,46 @@
+/**
+ * Copyright (C) 2018-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/platform/mutex.h"
+
+namespace mongo {
+
+void Mutex::lock() {
+ auto hasLock = _mutex.try_lock_for(_lockTimeout.toSystemDuration());
+ uassert(
+ ErrorCodes::InternalError, "Unable to take latch, wait time exceeds set timeout", hasLock);
+}
+void Mutex::unlock() {
+ _mutex.unlock();
+}
+bool Mutex::try_lock() {
+ return _mutex.try_lock();
+}
+
+} // namespace mongo \ No newline at end of file
diff --git a/src/mongo/platform/mutex.h b/src/mongo/platform/mutex.h
new file mode 100644
index 00000000000..46eb511e943
--- /dev/null
+++ b/src/mongo/platform/mutex.h
@@ -0,0 +1,57 @@
+/**
+ * Copyright (C) 2018-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include "mongo/base/error_codes.h"
+#include "mongo/base/string_data.h"
+#include "mongo/stdx/mutex.h"
+#include "mongo/util/clock_source_mock.h"
+
+namespace mongo {
+
+class Mutex {
+public:
+ Mutex() : Mutex("AnonymousMutex"_sd) {}
+ explicit Mutex(const StringData& name) : _name(name) {}
+
+ void lock();
+ void unlock();
+ bool try_lock();
+ const StringData& getName() {
+ return _name;
+ }
+
+private:
+ const StringData _name;
+ const Seconds _lockTimeout = Seconds(60);
+ stdx::timed_mutex _mutex;
+};
+
+} // namespace mongo \ No newline at end of file
diff --git a/src/mongo/platform/mutex_test.cpp b/src/mongo/platform/mutex_test.cpp
new file mode 100644
index 00000000000..594a4147659
--- /dev/null
+++ b/src/mongo/platform/mutex_test.cpp
@@ -0,0 +1,48 @@
+/**
+ * Copyright (C) 2018-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#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());
+ m.unlock();
+ ASSERT(m.try_lock());
+ m.unlock();
+}
+} \ No newline at end of file