summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBilly Donahue <billy.donahue@mongodb.com>2021-06-08 18:03:55 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-06-10 22:31:37 +0000
commit2f464f96322d22a89c284ee6c0b213072f1a0345 (patch)
tree33f9a03e62b093d2e0bd1fe17dd0f144597bb954 /src
parent6e6bfdaa1bfa5653d4f389d0a32e84ebe27c2376 (diff)
downloadmongo-2f464f96322d22a89c284ee6c0b213072f1a0345.tar.gz
SERVER-57424 remove ENABLE_BITMASK_OPERATORS
Diffstat (limited to 'src')
-rw-r--r--src/mongo/db/mongod_main.cpp3
-rw-r--r--src/mongo/db/storage/storage_engine_init.cpp7
-rw-r--r--src/mongo/db/storage/storage_engine_init.h17
-rw-r--r--src/mongo/dbtests/framework.cpp2
-rw-r--r--src/mongo/platform/bitwise_enum_operators.h100
-rw-r--r--src/mongo/stdx/utility.h7
-rw-r--r--src/mongo/transport/service_executor.h31
-rw-r--r--src/mongo/transport/service_executor_fixed.cpp2
-rw-r--r--src/mongo/transport/service_executor_reserved.cpp2
-rw-r--r--src/mongo/transport/service_executor_synchronous.cpp4
-rw-r--r--src/mongo/transport/service_executor_test.cpp31
11 files changed, 58 insertions, 148 deletions
diff --git a/src/mongo/db/mongod_main.cpp b/src/mongo/db/mongod_main.cpp
index e8306ee6e16..e42e56c5f34 100644
--- a/src/mongo/db/mongod_main.cpp
+++ b/src/mongo/db/mongod_main.cpp
@@ -406,8 +406,7 @@ ExitCode _initAndListen(ServiceContext* serviceContext, int listenPort) {
// initialized, a noop recovery unit is used until the initialization is complete.
auto startupOpCtx = serviceContext->makeOperationContext(&cc());
- auto lastShutdownState =
- initializeStorageEngine(startupOpCtx.get(), StorageEngineInitFlags::kNone);
+ auto lastShutdownState = initializeStorageEngine(startupOpCtx.get(), StorageEngineInitFlags{});
StorageControl::startStorageControls(serviceContext);
#ifdef MONGO_CONFIG_WIREDTIGER_ENABLED
diff --git a/src/mongo/db/storage/storage_engine_init.cpp b/src/mongo/db/storage/storage_engine_init.cpp
index 04140384c2a..b53db891190 100644
--- a/src/mongo/db/storage/storage_engine_init.cpp
+++ b/src/mongo/db/storage/storage_engine_init.cpp
@@ -66,7 +66,7 @@ StorageEngine::LastShutdownState initializeStorageEngine(OperationContext* opCtx
// This should be set once.
invariant(!service->getStorageEngine());
- if (0 == (initFlags & StorageEngineInitFlags::kAllowNoLockFile)) {
+ if ((initFlags & StorageEngineInitFlags::kAllowNoLockFile) == StorageEngineInitFlags{}) {
createLockFile(service);
}
@@ -130,7 +130,7 @@ StorageEngine::LastShutdownState initializeStorageEngine(OperationContext* opCtx
}
std::unique_ptr<StorageEngineMetadata> metadata;
- if ((initFlags & StorageEngineInitFlags::kSkipMetadataFile) == 0) {
+ if ((initFlags & StorageEngineInitFlags::kSkipMetadataFile) == StorageEngineInitFlags{}) {
metadata = StorageEngineMetadata::forPath(dbpath);
}
@@ -163,7 +163,8 @@ StorageEngine::LastShutdownState initializeStorageEngine(OperationContext* opCtx
}
// Write a new metadata file if it is not present.
- if (!metadata.get() && (initFlags & StorageEngineInitFlags::kSkipMetadataFile) == 0) {
+ if (!metadata.get() &&
+ (initFlags & StorageEngineInitFlags::kSkipMetadataFile) == StorageEngineInitFlags{}) {
invariant(!storageGlobalParams.readOnly);
metadata.reset(new StorageEngineMetadata(storageGlobalParams.dbpath));
metadata->setStorageEngine(factory->getCanonicalName().toString());
diff --git a/src/mongo/db/storage/storage_engine_init.h b/src/mongo/db/storage/storage_engine_init.h
index e131c0148dc..a59a47be8ed 100644
--- a/src/mongo/db/storage/storage_engine_init.h
+++ b/src/mongo/db/storage/storage_engine_init.h
@@ -34,19 +34,28 @@
#include "mongo/base/string_data.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/storage_engine.h"
-#include "mongo/platform/bitwise_enum_operators.h"
+#include "mongo/stdx/utility.h"
namespace mongo {
/**
* Valid flags to pass to initializeStorageEngine. Used as a bitfield.
*/
-enum StorageEngineInitFlags {
- kNone = 0,
+enum class StorageEngineInitFlags {
kAllowNoLockFile = 1 << 0,
kSkipMetadataFile = 1 << 1,
};
+constexpr StorageEngineInitFlags operator&(StorageEngineInitFlags a,
+ StorageEngineInitFlags b) noexcept {
+ return StorageEngineInitFlags{stdx::to_underlying(a) & stdx::to_underlying(b)};
+}
+
+constexpr StorageEngineInitFlags operator|(StorageEngineInitFlags a,
+ StorageEngineInitFlags b) noexcept {
+ return StorageEngineInitFlags{stdx::to_underlying(a) | stdx::to_underlying(b)};
+}
+
/**
* Initializes the storage engine on "service".
*/
@@ -96,6 +105,4 @@ Status validateStorageOptions(
*/
void appendStorageEngineList(ServiceContext* service, BSONObjBuilder* result);
-ENABLE_BITMASK_OPERATORS(StorageEngineInitFlags)
-
} // namespace mongo
diff --git a/src/mongo/dbtests/framework.cpp b/src/mongo/dbtests/framework.cpp
index 899905090af..330d796e9f3 100644
--- a/src/mongo/dbtests/framework.cpp
+++ b/src/mongo/dbtests/framework.cpp
@@ -113,7 +113,7 @@ int runDbTests(int argc, char** argv) {
{
auto opCtx = globalServiceContext->makeOperationContext(&cc());
- initializeStorageEngine(opCtx.get(), StorageEngineInitFlags::kNone);
+ initializeStorageEngine(opCtx.get(), StorageEngineInitFlags{});
}
StorageControl::startStorageControls(globalServiceContext, true /*forTestOnly*/);
diff --git a/src/mongo/platform/bitwise_enum_operators.h b/src/mongo/platform/bitwise_enum_operators.h
deleted file mode 100644
index 8950b93ce1c..00000000000
--- a/src/mongo/platform/bitwise_enum_operators.h
+++ /dev/null
@@ -1,100 +0,0 @@
-/**
- * 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 <type_traits>
-
-namespace mongo {
-
-template <typename Enum>
-struct EnableBitMaskOperators {
- static const bool enable = false;
-};
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type operator&(Enum lhs,
- Enum rhs) {
- using underlying = typename std::underlying_type<Enum>::type;
- return static_cast<Enum>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
-}
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type operator|(Enum lhs,
- Enum rhs) {
- using underlying = typename std::underlying_type<Enum>::type;
- return static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
-}
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type operator^(Enum lhs,
- Enum rhs) {
- using underlying = typename std::underlying_type<Enum>::type;
- return static_cast<Enum>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs));
-}
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type operator~(Enum rhs) {
- return static_cast<Enum>(~static_cast<typename std::underlying_type<Enum>::type>(rhs));
-}
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum&>::type operator|=(Enum& lhs,
- Enum rhs) {
- using underlying = typename std::underlying_type<Enum>::type;
- lhs = static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
- return lhs;
-}
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum&>::type operator&=(Enum& lhs,
- Enum rhs) {
- using underlying = typename std::underlying_type<Enum>::type;
- lhs = static_cast<Enum>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
- return lhs;
-}
-
-template <typename Enum>
-typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum&>::type operator^=(Enum& lhs,
- Enum rhs) {
- using underlying = typename std::underlying_type<Enum>::type;
- lhs = static_cast<Enum>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs));
- return lhs;
-}
-
-} // namespace mongo
-
-#define ENABLE_BITMASK_OPERATORS(x) \
- \
- template <> \
- \
- struct EnableBitMaskOperators<x> { \
- static_assert(std::is_enum<x>::value, "template parameter is not an enum type"); \
- static const bool enable = true; \
- };
diff --git a/src/mongo/stdx/utility.h b/src/mongo/stdx/utility.h
index c71134dd1cb..327028da24c 100644
--- a/src/mongo/stdx/utility.h
+++ b/src/mongo/stdx/utility.h
@@ -51,5 +51,12 @@ using std::as_const;
#endif
+/** https://en.cppreference.com/w/cpp/utility/to_underlying */
+template <typename E>
+constexpr auto to_underlying(E e) noexcept {
+ static_assert(std::is_enum_v<E>, "E is not an enumeration");
+ return static_cast<std::underlying_type_t<E>>(e);
+}
+
} // namespace stdx
} // namespace mongo
diff --git a/src/mongo/transport/service_executor.h b/src/mongo/transport/service_executor.h
index 478120f52bc..b107f5d7d80 100644
--- a/src/mongo/transport/service_executor.h
+++ b/src/mongo/transport/service_executor.h
@@ -35,7 +35,7 @@
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/client.h"
#include "mongo/db/service_context.h"
-#include "mongo/platform/bitwise_enum_operators.h"
+#include "mongo/stdx/utility.h"
#include "mongo/transport/service_entry_point.h"
#include "mongo/transport/session.h"
#include "mongo/transport/transport_mode.h"
@@ -70,21 +70,19 @@ public:
virtual ~ServiceExecutor() = default;
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.
- kEmptyFlags = 1 << 0,
- // Deferred tasks will never get a new thread launched to run them.
- kDeferredTask = 1 << 1,
-
- // MayRecurse indicates that a task may be run recursively.
- kMayRecurse = 1 << 2,
-
- // MayYieldBeforeSchedule indicates that the executor may yield on the current thread before
- // scheduling the task.
- kMayYieldBeforeSchedule = 1 << 3,
+ /** With no flags set, `scheduleTask` will launch new threads as needed. */
+ enum class ScheduleFlags {
+ kDeferredTask = 1 << 0, /**< Never given a newly launched thread. */
+ kMayRecurse = 1 << 1, /**< May be run recursively. */
+ kMayYieldBeforeSchedule = 1 << 2, /**< May yield before scheduling. */
};
+ friend constexpr ScheduleFlags operator&(ScheduleFlags a, ScheduleFlags b) noexcept {
+ return ScheduleFlags{stdx::to_underlying(a) & stdx::to_underlying(b)};
+ }
+ friend constexpr ScheduleFlags operator|(ScheduleFlags a, ScheduleFlags b) noexcept {
+ return ScheduleFlags{stdx::to_underlying(a) | stdx::to_underlying(b)};
+ }
/*
* Starts the ServiceExecutor. This may create threads even if no tasks are scheduled.
@@ -108,8 +106,7 @@ public:
* for execution on the service executor. May throw if "scheduleTask" returns a non-okay status.
*/
void schedule(OutOfLineExecutor::Task func) override {
- iassert(scheduleTask([task = std::move(func)]() mutable { task(Status::OK()); },
- ScheduleFlags::kEmptyFlags));
+ iassert(scheduleTask([task = std::move(func)]() mutable { task(Status::OK()); }, {}));
}
/*
@@ -256,6 +253,4 @@ public:
} // namespace transport
-ENABLE_BITMASK_OPERATORS(transport::ServiceExecutor::ScheduleFlags)
-
} // namespace mongo
diff --git a/src/mongo/transport/service_executor_fixed.cpp b/src/mongo/transport/service_executor_fixed.cpp
index 08579f53f36..d0de6702a2d 100644
--- a/src/mongo/transport/service_executor_fixed.cpp
+++ b/src/mongo/transport/service_executor_fixed.cpp
@@ -354,7 +354,7 @@ Status ServiceExecutorFixed::scheduleTask(Task task, ScheduleFlags flags) try {
}
auto mayExecuteTaskInline = [&] {
- if (!(flags & ScheduleFlags::kMayRecurse))
+ if ((flags & ScheduleFlags::kMayRecurse) == ScheduleFlags{})
return false;
if (!_executorContext)
return false;
diff --git a/src/mongo/transport/service_executor_reserved.cpp b/src/mongo/transport/service_executor_reserved.cpp
index c613c9c7ba2..4a086efb01d 100644
--- a/src/mongo/transport/service_executor_reserved.cpp
+++ b/src/mongo/transport/service_executor_reserved.cpp
@@ -197,7 +197,7 @@ Status ServiceExecutorReserved::scheduleTask(Task task, ScheduleFlags flags) {
// performance in testing. Try to limit the amount of recursion so we don't blow up the
// stack, even though this shouldn't happen with this executor that uses blocking
// network I/O.
- if ((flags & ScheduleFlags::kMayRecurse) &&
+ if (((flags & ScheduleFlags::kMayRecurse) != ScheduleFlags{}) &&
(_localRecursionDepth < reservedServiceExecutorRecursionLimit.loadRelaxed())) {
++_localRecursionDepth;
task();
diff --git a/src/mongo/transport/service_executor_synchronous.cpp b/src/mongo/transport/service_executor_synchronous.cpp
index 0ab01d817b6..96f32fbe175 100644
--- a/src/mongo/transport/service_executor_synchronous.cpp
+++ b/src/mongo/transport/service_executor_synchronous.cpp
@@ -99,7 +99,7 @@ Status ServiceExecutorSynchronous::scheduleTask(Task task, ScheduleFlags flags)
}
if (!_localWorkQueue.empty()) {
- if (flags & ScheduleFlags::kMayYieldBeforeSchedule) {
+ if ((flags & ScheduleFlags::kMayYieldBeforeSchedule) != ScheduleFlags{}) {
yieldIfAppropriate();
}
@@ -107,7 +107,7 @@ Status ServiceExecutorSynchronous::scheduleTask(Task task, ScheduleFlags flags)
// performance in testing. Try to limit the amount of recursion so we don't blow up the
// stack, even though this shouldn't happen with this executor that uses blocking network
// I/O.
- if ((flags & ScheduleFlags::kMayRecurse) &&
+ if ((flags & ScheduleFlags::kMayRecurse) != ScheduleFlags{} &&
(_localRecursionDepth < synchronousServiceExecutorRecursionLimit.loadRelaxed())) {
++_localRecursionDepth;
task();
diff --git a/src/mongo/transport/service_executor_test.cpp b/src/mongo/transport/service_executor_test.cpp
index 6193a97d01d..f90948472a0 100644
--- a/src/mongo/transport/service_executor_test.cpp
+++ b/src/mongo/transport/service_executor_test.cpp
@@ -149,7 +149,7 @@ void scheduleBasicTask(ServiceExecutor* exec, bool expectSuccess) {
auto barrier = std::make_shared<unittest::Barrier>(2);
auto task = [barrier] { barrier->countDownAndWait(); };
- auto status = exec->scheduleTask(std::move(task), ServiceExecutor::kEmptyFlags);
+ auto status = exec->scheduleTask(std::move(task), {});
if (expectSuccess) {
ASSERT_OK(status);
barrier->countDownAndWait();
@@ -217,7 +217,7 @@ public:
TEST_F(ServiceExecutorFixedFixture, ScheduleFailsBeforeStartup) {
auto executorHandle = ServiceExecutorHandle();
- ASSERT_NOT_OK(executorHandle->scheduleTask([] {}, ServiceExecutor::kEmptyFlags));
+ ASSERT_NOT_OK(executorHandle->scheduleTask([] {}, {}));
}
TEST_F(ServiceExecutorFixedFixture, JoinWorksBeforeShutdown) {
@@ -236,7 +236,7 @@ TEST_F(ServiceExecutorFixedFixture, JoinWorksBeforeShutdown) {
executorHandle.joinWithoutShutdown();
// Poke the executor to make sure it is shutdown.
- ASSERT_NOT_OK(executorHandle->scheduleTask([] {}, ServiceExecutor::kEmptyFlags));
+ ASSERT_NOT_OK(executorHandle->scheduleTask([] {}, {}));
}
TEST_F(ServiceExecutorFixedFixture, BasicTaskRuns) {
@@ -244,8 +244,8 @@ TEST_F(ServiceExecutorFixedFixture, BasicTaskRuns) {
executorHandle.start();
auto barrier = std::make_shared<unittest::Barrier>(2);
- ASSERT_OK(executorHandle->scheduleTask([barrier]() mutable { barrier->countDownAndWait(); },
- ServiceExecutor::kEmptyFlags));
+ ASSERT_OK(
+ executorHandle->scheduleTask([barrier]() mutable { barrier->countDownAndWait(); }, {}));
barrier->countDownAndWait();
}
@@ -259,7 +259,8 @@ TEST_F(ServiceExecutorFixedFixture, RecursiveTask) {
recursiveTask = [&, barrier, executor = *executorHandle] {
if (executor->getRecursionDepthForExecutorThread() <
fixedServiceExecutorRecursionLimit.load()) {
- ASSERT_OK(executor->scheduleTask(recursiveTask, ServiceExecutor::kMayRecurse));
+ ASSERT_OK(
+ executor->scheduleTask(recursiveTask, ServiceExecutor::ScheduleFlags::kMayRecurse));
} else {
// This test never returns unless the service executor can satisfy the recursion depth.
barrier->countDownAndWait();
@@ -267,7 +268,8 @@ TEST_F(ServiceExecutorFixedFixture, RecursiveTask) {
};
// Schedule recursive task and wait for the recursion to stop
- ASSERT_OK(executorHandle->scheduleTask(recursiveTask, ServiceExecutor::kMayRecurse));
+ ASSERT_OK(
+ executorHandle->scheduleTask(recursiveTask, ServiceExecutor::ScheduleFlags::kMayRecurse));
barrier->countDownAndWait();
}
@@ -285,7 +287,7 @@ TEST_F(ServiceExecutorFixedFixture, FlattenRecursiveScheduledTasks) {
recursiveTask = [&, barrier, executor = *executorHandle] {
ASSERT_EQ(executor->getRecursionDepthForExecutorThread(), 1);
if (tasksToSchedule.fetchAndSubtract(1) > 0) {
- ASSERT_OK(executor->scheduleTask(recursiveTask, ServiceExecutor::kEmptyFlags));
+ ASSERT_OK(executor->scheduleTask(recursiveTask, {}));
} else {
// Once there are no more tasks to schedule, notify the main thread to proceed.
barrier->countDownAndWait();
@@ -293,8 +295,8 @@ TEST_F(ServiceExecutorFixedFixture, FlattenRecursiveScheduledTasks) {
};
// Schedule the recursive task and wait for the execution to finish.
- ASSERT_OK(
- executorHandle->scheduleTask(recursiveTask, ServiceExecutor::kMayYieldBeforeSchedule));
+ ASSERT_OK(executorHandle->scheduleTask(
+ recursiveTask, ServiceExecutor::ScheduleFlags::kMayYieldBeforeSchedule));
barrier->countDownAndWait();
}
@@ -310,7 +312,7 @@ TEST_F(ServiceExecutorFixedFixture, ShutdownTimeLimit) {
invoked->emplaceValue();
mayReturn->getFuture().get();
},
- ServiceExecutor::kEmptyFlags));
+ {}));
invoked->getFuture().get();
ASSERT_NOT_OK(executorHandle->shutdown(kShutdownTime));
@@ -330,8 +332,8 @@ TEST_F(ServiceExecutorFixedFixture, ScheduleSucceedsBeforeShutdown) {
// The executor accepts the work, but hasn't used the underlying pool yet.
thread = stdx::thread([&] {
- ASSERT_OK(executorHandle->scheduleTask([&, barrier] { barrier->countDownAndWait(); },
- ServiceExecutor::kEmptyFlags));
+ ASSERT_OK(
+ executorHandle->scheduleTask([&, barrier] { barrier->countDownAndWait(); }, {}));
});
failpoint->waitForTimesEntered(1);
@@ -353,8 +355,7 @@ TEST_F(ServiceExecutorFixedFixture, ScheduleFailsAfterShutdown) {
executorHandle.start();
ASSERT_OK(executorHandle->shutdown(kShutdownTime));
- ASSERT_NOT_OK(
- executorHandle->scheduleTask([] { MONGO_UNREACHABLE; }, ServiceExecutor::kEmptyFlags));
+ ASSERT_NOT_OK(executorHandle->scheduleTask([] { MONGO_UNREACHABLE; }, {}));
}
TEST_F(ServiceExecutorFixedFixture, RunTaskAfterWaitingForData) {