summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Caimano <ben.caimano@10gen.com>2020-07-15 22:15:46 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-07-16 20:47:06 +0000
commit64449198588abc90ea81eb4f4810bf4ae23d59b4 (patch)
tree98e21386a9868d8d4c9ba86ed9793cd591225cd2
parent80f11e6ae0708e8c8da49208ef2cf71cdd06877c (diff)
downloadmongo-64449198588abc90ea81eb4f4810bf4ae23d59b4.tar.gz
SERVER-49548 Convert ServiceExecutor to use unique_function
-rw-r--r--src/mongo/transport/service_entry_point_utils.cpp11
-rw-r--r--src/mongo/transport/service_entry_point_utils.h3
-rw-r--r--src/mongo/transport/service_executor.h3
-rw-r--r--src/mongo/transport/service_executor_fixed.cpp6
-rw-r--r--src/mongo/transport/service_executor_synchronous.cpp2
-rw-r--r--src/mongo/transport/service_executor_test.cpp8
6 files changed, 18 insertions, 15 deletions
diff --git a/src/mongo/transport/service_entry_point_utils.cpp b/src/mongo/transport/service_entry_point_utils.cpp
index f9489149ddf..f0c95a28dba 100644
--- a/src/mongo/transport/service_entry_point_utils.cpp
+++ b/src/mongo/transport/service_entry_point_utils.cpp
@@ -57,18 +57,19 @@ namespace mongo {
namespace {
void* runFunc(void* ctx) {
- std::unique_ptr<std::function<void()>> taskPtr(static_cast<std::function<void()>*>(ctx));
+ auto taskPtr =
+ std::unique_ptr<unique_function<void()>>(static_cast<unique_function<void()>*>(ctx));
(*taskPtr)();
return nullptr;
}
} // namespace
-Status launchServiceWorkerThread(std::function<void()> task) noexcept {
+Status launchServiceWorkerThread(unique_function<void()> task) noexcept {
try {
#if defined(_WIN32)
- stdx::thread(std::move(task)).detach();
+ stdx::thread([task = std::move(task)]() mutable { task(); }).detach();
#else
pthread_attr_t attrs;
pthread_attr_init(&attrs);
@@ -102,13 +103,13 @@ Status launchServiceWorkerThread(std::function<void()> task) noexcept {
// Wrap the user-specified `task` so it runs with an installed `sigaltstack`.
task = [sigAltStackController = std::make_shared<stdx::support::SigAltStackController>(),
- f = std::move(task)] {
+ f = std::move(task)]() mutable {
auto sigAltStackGuard = sigAltStackController->makeInstallGuard();
f();
};
pthread_t thread;
- auto ctx = std::make_unique<std::function<void()>>(std::move(task));
+ auto ctx = std::make_unique<unique_function<void()>>(std::move(task));
ThreadSafetyContext::getThreadSafetyContext()->onThreadCreate();
int failed = pthread_create(&thread, &attrs, runFunc, ctx.get());
diff --git a/src/mongo/transport/service_entry_point_utils.h b/src/mongo/transport/service_entry_point_utils.h
index c63afc5027b..f6f83d6d37e 100644
--- a/src/mongo/transport/service_entry_point_utils.h
+++ b/src/mongo/transport/service_entry_point_utils.h
@@ -32,9 +32,10 @@
#include <functional>
#include "mongo/transport/session.h"
+#include "mongo/util/functional.h"
namespace mongo {
-Status launchServiceWorkerThread(std::function<void()> task) noexcept;
+Status launchServiceWorkerThread(unique_function<void()> task) noexcept;
} // namespace mongo
diff --git a/src/mongo/transport/service_executor.h b/src/mongo/transport/service_executor.h
index 6a1410ae86b..d44bb704c66 100644
--- a/src/mongo/transport/service_executor.h
+++ b/src/mongo/transport/service_executor.h
@@ -36,6 +36,7 @@
#include "mongo/platform/bitwise_enum_operators.h"
#include "mongo/transport/transport_mode.h"
#include "mongo/util/duration.h"
+#include "mongo/util/functional.h"
namespace mongo {
// This needs to be forward declared here because the service_context.h is a circular dependency.
@@ -49,7 +50,7 @@ namespace transport {
class ServiceExecutor {
public:
virtual ~ServiceExecutor() = default;
- using Task = std::function<void()>;
+ using Task = unique_function<void()>;
enum ScheduleFlags {
// No flags (kEmptyFlags) specifies that this is a normal task and that the executor should
// launch new threads as needed to run the task.
diff --git a/src/mongo/transport/service_executor_fixed.cpp b/src/mongo/transport/service_executor_fixed.cpp
index 48a94cd57cd..ba8eca2bfaf 100644
--- a/src/mongo/transport/service_executor_fixed.cpp
+++ b/src/mongo/transport/service_executor_fixed.cpp
@@ -128,7 +128,7 @@ Status ServiceExecutorFixed::schedule(Task task, ScheduleFlags flags) {
if (_executorContext->getRecursionDepth() <
fixedServiceExecutorRecursionLimit.loadRelaxed()) {
// Recursively executing the task on the executor thread.
- _executorContext->run(task);
+ _executorContext->run(std::move(task));
return Status::OK();
}
}
@@ -137,10 +137,10 @@ Status ServiceExecutorFixed::schedule(Task task, ScheduleFlags flags) {
// May throw if an attempt is made to schedule after the thread pool is shutdown.
try {
- _threadPool->schedule([task = std::move(task)](Status status) {
+ _threadPool->schedule([task = std::move(task)](Status status) mutable {
internalAssert(status);
invariant(_executorContext);
- _executorContext->run(task);
+ _executorContext->run(std::move(task));
});
} catch (DBException& e) {
return e.toStatus();
diff --git a/src/mongo/transport/service_executor_synchronous.cpp b/src/mongo/transport/service_executor_synchronous.cpp
index 15724d3470c..10cc0ad3cce 100644
--- a/src/mongo/transport/service_executor_synchronous.cpp
+++ b/src/mongo/transport/service_executor_synchronous.cpp
@@ -114,7 +114,7 @@ Status ServiceExecutorSynchronous::schedule(Task task, ScheduleFlags flags) {
LOGV2_DEBUG(22983, 3, "Starting new executor thread in passthrough mode");
Status status = launchServiceWorkerThread(
- [this, condVarAnchor = _shutdownCondition, task = std::move(task)] {
+ [this, condVarAnchor = _shutdownCondition, task = std::move(task)]() mutable {
_numRunningWorkerThreads.addAndFetch(1);
_localWorkQueue.emplace_back(std::move(task));
diff --git a/src/mongo/transport/service_executor_test.cpp b/src/mongo/transport/service_executor_test.cpp
index 99ee7f3ddb5..f79597a5521 100644
--- a/src/mongo/transport/service_executor_test.cpp
+++ b/src/mongo/transport/service_executor_test.cpp
@@ -233,8 +233,8 @@ TEST_F(ServiceExecutorFixedFixture, RecursiveTask) {
auto executor = startAndGetServiceExecutor();
auto barrier = std::make_shared<unittest::Barrier>(2);
- ServiceExecutor::Task recursiveTask;
- recursiveTask = [this, executor, barrier, &recursiveTask] {
+ std::function<void()> recursiveTask;
+ recursiveTask = [&, barrier] {
auto recursionGuard = makeRecursionGuard();
if (getRecursionDepth() < fixedServiceExecutorRecursionLimit.load()) {
ASSERT_OK(executor->schedule(recursiveTask, ServiceExecutor::kMayRecurse));
@@ -257,8 +257,8 @@ TEST_F(ServiceExecutorFixedFixture, FlattenRecursiveScheduledTasks) {
// A recursive task that expects to be executed non-recursively. The task recursively schedules
// "tasksToSchedule" tasks to the service executor, and each scheduled task verifies that the
// recursion depth remains zero during its execution.
- ServiceExecutor::Task recursiveTask;
- recursiveTask = [this, executor, barrier, &recursiveTask, &tasksToSchedule] {
+ std::function<void()> recursiveTask;
+ recursiveTask = [&, barrier] {
auto recursionGuard = makeRecursionGuard();
ASSERT_EQ(getRecursionDepth(), 1);
if (tasksToSchedule.fetchAndSubtract(1) > 0) {