summaryrefslogtreecommitdiff
path: root/src/mongo/util
diff options
context:
space:
mode:
authorAmirsaman Memaripour <amirsaman.memaripour@mongodb.com>2020-05-26 14:24:08 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-06-02 13:22:38 +0000
commit3b7bc03a8f8b964769ff708674163b67ddd1a450 (patch)
tree77248252074ae22667ad87cdd9dd6d6f15dd4a88 /src/mongo/util
parentb348cccce22b4b3a8bc26d3d840c64fe15915a04 (diff)
downloadmongo-3b7bc03a8f8b964769ff708674163b67ddd1a450.tar.gz
SERVER-48346 Fix lifetime issues for barriers captured by reference
Extends the lifetime of barriers that are referenced by background threads and their reference could outlive the barrier's owner.
Diffstat (limited to 'src/mongo/util')
-rw-r--r--src/mongo/util/concurrency/thread_pool_test.cpp22
-rw-r--r--src/mongo/util/thread_safety_context_test.cpp12
2 files changed, 12 insertions, 22 deletions
diff --git a/src/mongo/util/concurrency/thread_pool_test.cpp b/src/mongo/util/concurrency/thread_pool_test.cpp
index 30e5dbd0ddc..0b456c6abc1 100644
--- a/src/mongo/util/concurrency/thread_pool_test.cpp
+++ b/src/mongo/util/concurrency/thread_pool_test.cpp
@@ -32,8 +32,10 @@
#include "mongo/platform/basic.h"
#include <boost/optional.hpp>
+#include <fmt/format.h>
#include "mongo/base/init.h"
+#include "mongo/platform/atomic_word.h"
#include "mongo/platform/mutex.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/stdx/thread.h"
@@ -48,6 +50,7 @@
namespace {
using namespace mongo;
+using namespace fmt::literals;
MONGO_INITIALIZER(ThreadPoolCommonTests)(InitializerContext*) {
addTestsForThreadPool("ThreadPoolCommon",
@@ -256,29 +259,22 @@ DEATH_TEST_REGEX(ThreadPoolTest,
TEST_F(ThreadPoolTest, ThreadPoolRunsOnCreateThreadFunctionBeforeConsumingTasks) {
unittest::Barrier barrier(2U);
-
- bool onCreateThreadCalled = false;
- std::string taskThreadName;
+ std::string journal;
ThreadPool::Options options;
options.threadNamePrefix = "mythread";
options.maxThreads = 1U;
- options.onCreateThread = [&onCreateThreadCalled,
- &taskThreadName](const std::string& threadName) {
- onCreateThreadCalled = true;
- taskThreadName = threadName;
+ options.onCreateThread = [&](const std::string& threadName) {
+ journal.append("[onCreate({})]"_format(threadName));
};
ThreadPool pool(options);
pool.startup();
-
- pool.schedule([&barrier](auto status) {
- ASSERT_OK(status);
+ pool.schedule([&](auto status) {
+ journal.append("[Call({})]"_format(status.toString()));
barrier.countDownAndWait();
});
barrier.countDownAndWait();
-
- ASSERT_TRUE(onCreateThreadCalled);
- ASSERT_EQUALS(options.threadNamePrefix + "0", taskThreadName);
+ ASSERT_EQUALS(journal, "[onCreate(mythread0)][Call(OK)]");
}
TEST(ThreadPoolTest, JoinAllRetiredThreads) {
diff --git a/src/mongo/util/thread_safety_context_test.cpp b/src/mongo/util/thread_safety_context_test.cpp
index 94d3c45d5b5..189bdc86ee0 100644
--- a/src/mongo/util/thread_safety_context_test.cpp
+++ b/src/mongo/util/thread_safety_context_test.cpp
@@ -29,7 +29,6 @@
#include <vector>
#include "mongo/stdx/thread.h"
-#include "mongo/unittest/barrier.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/thread_safety_context.h"
@@ -78,15 +77,10 @@ DEATH_TEST_F(ThreadSafetyContextTest, CreateThreadsAfterForbidingMultiThreading,
}
DEATH_TEST_F(ThreadSafetyContextTest, ForbidMultiThreadingAfterCreatingThreads, "invariant") {
- unittest::Barrier barrier(2);
+ auto thread = stdx::thread([]() {});
- auto thread = stdx::thread([&]() mutable {
- barrier.countDownAndWait();
- sleepFor(Milliseconds(50));
- });
-
- // Wait for the thread to start before proceeding with the test
- barrier.countDownAndWait();
+ // Wait for the thread to return before proceeding with the test
+ thread.join();
ThreadSafetyContext::getThreadSafetyContext()->forbidMultiThreading();
// Must never reach here or the test fails