summaryrefslogtreecommitdiff
path: root/src/mongo/platform
diff options
context:
space:
mode:
authorShaileja Jain <shaileja.jain@gmail.com>2019-06-28 13:51:23 -0400
committerShaileja Jain <shaileja.jain@gmail.com>2019-07-24 14:02:39 -0400
commitbaaa7c25fadcbe4f544a885be4734a0d50ad20d2 (patch)
tree842f7168119f973694710327a925070077b5eedb /src/mongo/platform
parent45ff40ff0f2a2d7664a38acd06bfb4f4fe235267 (diff)
downloadmongo-baaa7c25fadcbe4f544a885be4734a0d50ad20d2.tar.gz
SERVER-41358 Created condition_variable polyfill
Diffstat (limited to 'src/mongo/platform')
-rw-r--r--src/mongo/platform/SConscript1
-rw-r--r--src/mongo/platform/condition_variable.cpp44
-rw-r--r--src/mongo/platform/condition_variable.h103
-rw-r--r--src/mongo/platform/condition_variable_test.cpp61
4 files changed, 209 insertions, 0 deletions
diff --git a/src/mongo/platform/SConscript b/src/mongo/platform/SConscript
index 6ccbf7f77ce..2a6c8a4f0bc 100644
--- a/src/mongo/platform/SConscript
+++ b/src/mongo/platform/SConscript
@@ -8,6 +8,7 @@ env.CppUnitTest(
'atomic_proxy_test.cpp',
'atomic_word_test.cpp',
'bits_test.cpp',
+ 'condition_variable_test.cpp',
'endian_test.cpp',
'mutex_test.cpp',
'process_id_test.cpp',
diff --git a/src/mongo/platform/condition_variable.cpp b/src/mongo/platform/condition_variable.cpp
new file mode 100644
index 00000000000..76de1ca6d2b
--- /dev/null
+++ b/src/mongo/platform/condition_variable.cpp
@@ -0,0 +1,44 @@
+/**
+ * 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/condition_variable.h"
+
+namespace mongo {
+void ConditionVariable::wait(lock_t& lock) {
+ _condvar.wait(lock);
+}
+
+void ConditionVariable::notify_one() noexcept {
+ _condvar.notify_one();
+}
+
+void ConditionVariable::notify_all() noexcept {
+ _condvar.notify_all();
+}
+} // namespace mongo
diff --git a/src/mongo/platform/condition_variable.h b/src/mongo/platform/condition_variable.h
new file mode 100644
index 00000000000..5b5f2f3c79b
--- /dev/null
+++ b/src/mongo/platform/condition_variable.h
@@ -0,0 +1,103 @@
+/**
+ * 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/platform/basic.h"
+
+#include "mongo/platform/mutex.h"
+#include "mongo/stdx/condition_variable.h"
+#include "mongo/util/diagnostic_info.h"
+
+
+namespace mongo {
+class ConditionVariable : private stdx::condition_variable_any {
+ using lock_t = stdx::unique_lock<Mutex>;
+
+public:
+ void wait(lock_t& lock);
+
+ template <class Predicate>
+ void wait(lock_t& lock, Predicate pred);
+
+ template <class Rep, class Period>
+ stdx::cv_status wait_for(lock_t& lock, const stdx::chrono::duration<Rep, Period>& rel_time);
+
+ template <class Rep, class Period, class Predicate>
+ bool wait_for(lock_t& lock,
+ const stdx::chrono::duration<Rep, Period>& rel_time,
+ Predicate pred);
+
+ template <class Clock, class Duration>
+ stdx::cv_status wait_until(lock_t& lock,
+ const stdx::chrono::time_point<Clock, Duration>& timeout_time);
+
+ template <class Clock, class Duration, class Predicate>
+ bool wait_until(lock_t& lock,
+ const stdx::chrono::time_point<Clock, Duration>& timeout_time,
+ Predicate pred);
+
+ void notify_one() noexcept;
+ void notify_all() noexcept;
+
+private:
+ stdx::condition_variable_any _condvar;
+};
+
+template <class Predicate>
+void ConditionVariable::wait(lock_t& lock, Predicate pred) {
+ _condvar.wait(lock, pred);
+}
+
+template <class Rep, class Period>
+stdx::cv_status ConditionVariable::wait_for(lock_t& lock,
+ const stdx::chrono::duration<Rep, Period>& rel_time) {
+ return _condvar.wait_for(lock, rel_time);
+}
+
+template <class Rep, class Period, class Predicate>
+bool ConditionVariable::wait_for(lock_t& lock,
+ const stdx::chrono::duration<Rep, Period>& rel_time,
+ Predicate pred) {
+ return _condvar.wait_for(lock, rel_time, pred);
+}
+
+template <class Clock, class Duration>
+stdx::cv_status ConditionVariable::wait_until(
+ lock_t& lock, const stdx::chrono::time_point<Clock, Duration>& timeout_time) {
+ return _condvar.wait_until(lock, timeout_time);
+}
+
+template <class Clock, class Duration, class Predicate>
+bool ConditionVariable::wait_until(lock_t& lock,
+ const stdx::chrono::time_point<Clock, Duration>& timeout_time,
+ Predicate pred) {
+ return _condvar.wait_until(lock, timeout_time, pred);
+}
+
+} // namespace mongo
diff --git a/src/mongo/platform/condition_variable_test.cpp b/src/mongo/platform/condition_variable_test.cpp
new file mode 100644
index 00000000000..cd12e037537
--- /dev/null
+++ b/src/mongo/platform/condition_variable_test.cpp
@@ -0,0 +1,61 @@
+/**
+ * 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/platform/condition_variable.h"
+#include "mongo/platform/mutex.h"
+#include "mongo/stdx/thread.h"
+#include "mongo/unittest/barrier.h"
+
+namespace mongo {
+
+TEST(ConditionVariable, BasicSingleThread) {
+ unittest::Barrier barrier(2U);
+ ConditionVariable cv;
+ Mutex m;
+ bool done = false;
+
+ stdx::thread worker([&]() {
+ stdx::unique_lock<Mutex> lk(m);
+ barrier.countDownAndWait();
+ ASSERT(!done);
+ cv.wait(lk, [&] { return done; });
+ ASSERT(done);
+ });
+
+ barrier.countDownAndWait();
+ {
+ stdx::unique_lock<Mutex> lk(m);
+ done = true;
+ }
+ cv.notify_one();
+ worker.join();
+}
+}