summaryrefslogtreecommitdiff
path: root/src/mongo
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
parent45ff40ff0f2a2d7664a38acd06bfb4f4fe235267 (diff)
downloadmongo-baaa7c25fadcbe4f544a885be4734a0d50ad20d2.tar.gz
SERVER-41358 Created condition_variable polyfill
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/SConscript1
-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
-rw-r--r--src/mongo/stdx/condition_variable.h27
-rw-r--r--src/mongo/util/diagnostic_info.h1
7 files changed, 224 insertions, 14 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript
index a67a51223af..4d657c05539 100644
--- a/src/mongo/SConscript
+++ b/src/mongo/SConscript
@@ -112,6 +112,7 @@ baseEnv.Library(
'logger/rotatable_file_manager.cpp',
'logger/rotatable_file_writer.cpp',
'platform/decimal128.cpp',
+ 'platform/condition_variable.cpp',
'platform/mutex.cpp',
'platform/posix_fadvise.cpp',
'platform/process_id.cpp',
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();
+}
+}
diff --git a/src/mongo/stdx/condition_variable.h b/src/mongo/stdx/condition_variable.h
index 1a6836b3a5f..a27567ee13b 100644
--- a/src/mongo/stdx/condition_variable.h
+++ b/src/mongo/stdx/condition_variable.h
@@ -88,18 +88,17 @@ class Waitable;
namespace stdx {
-using condition_variable_any = ::std::condition_variable_any; // NOLINT
-using cv_status = ::std::cv_status; // NOLINT
-using ::std::notify_all_at_thread_exit; // NOLINT
+using cv_status = ::std::cv_status; // NOLINT
+using ::std::notify_all_at_thread_exit; // NOLINT
/**
- * We wrap std::condition_variable to allow us to register Notifyables which can "wait" on the
- * condvar without actually waiting on the std::condition_variable. This allows us to possibly do
- * productive work in those types, rather than sleeping in the os.
+ * We wrap std::condition_variable_any to allow us to register Notifyables which can "wait" on the
+ * condvar without actually waiting on the std::condition_variable_any. This allows us to possibly
+ * do productive work in those types, rather than sleeping in the os.
*/
-class condition_variable : private std::condition_variable { // NOLINT
+class condition_variable : private std::condition_variable_any { // NOLINT
public:
- using std::condition_variable::condition_variable; // NOLINT
+ using std::condition_variable_any::condition_variable_any; // NOLINT
void notify_one() noexcept {
if (_notifyableCount.load()) {
@@ -110,7 +109,7 @@ public:
}
}
- std::condition_variable::notify_one(); // NOLINT
+ std::condition_variable_any::notify_one(); // NOLINT
}
void notify_all() noexcept {
@@ -121,13 +120,12 @@ public:
}
}
- std::condition_variable::notify_all(); // NOLINT
+ std::condition_variable_any::notify_all(); // NOLINT
}
- using std::condition_variable::wait; // NOLINT
- using std::condition_variable::wait_for; // NOLINT
- using std::condition_variable::wait_until; // NOLINT
- using std::condition_variable::native_handle; // NOLINT
+ using std::condition_variable_any::wait; // NOLINT
+ using std::condition_variable_any::wait_for; // NOLINT
+ using std::condition_variable_any::wait_until; // NOLINT
private:
friend class ::mongo::Waitable;
@@ -212,5 +210,6 @@ private:
std::list<Notifyable*> _notifyables;
};
+using condition_variable_any = stdx::condition_variable;
} // namespace stdx
} // namespace mongo
diff --git a/src/mongo/util/diagnostic_info.h b/src/mongo/util/diagnostic_info.h
index 8eba872bb20..57dadd75a24 100644
--- a/src/mongo/util/diagnostic_info.h
+++ b/src/mongo/util/diagnostic_info.h
@@ -69,6 +69,7 @@ private:
Date_t _timestamp;
StringData _captureName;
+
DiagnosticInfo(const Date_t& timestamp, const StringData& captureName)
: _timestamp(timestamp), _captureName(captureName) {}
};