summaryrefslogtreecommitdiff
path: root/src/mongo/db/query
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2014-11-05 15:09:07 -0500
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2014-11-05 15:51:00 -0500
commit8ee12731c9f31add89a08d8a391e9d88299aaa55 (patch)
tree987859121175270db731678b931a3c35319a58be /src/mongo/db/query
parentf95d813fba8b9a159ba10ffe5238a158caf29074 (diff)
downloadmongo-8ee12731c9f31add89a08d8a391e9d88299aaa55.tar.gz
SERVER-15541 Move yield.cpp out of the LockManager library and fix warning
Yielding should be should be part of the query library. Also rename the Yield class in order to avoid warning on Windows.
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r--src/mongo/db/query/SConscript1
-rw-r--r--src/mongo/db/query/plan_yield_policy.cpp4
-rw-r--r--src/mongo/db/query/query_yield.cpp110
-rw-r--r--src/mongo/db/query/query_yield.h53
4 files changed, 166 insertions, 2 deletions
diff --git a/src/mongo/db/query/SConscript b/src/mongo/db/query/SConscript
index e20af2e3b90..cd2f7209e9c 100644
--- a/src/mongo/db/query/SConscript
+++ b/src/mongo/db/query/SConscript
@@ -40,6 +40,7 @@ env.Library(
"plan_executor.cpp",
"plan_ranker.cpp",
"plan_yield_policy.cpp",
+ "query_yield.cpp",
"stage_builder.cpp",
],
LIBDEPS=[
diff --git a/src/mongo/db/query/plan_yield_policy.cpp b/src/mongo/db/query/plan_yield_policy.cpp
index 52d1488f48f..fd385ccca2a 100644
--- a/src/mongo/db/query/plan_yield_policy.cpp
+++ b/src/mongo/db/query/plan_yield_policy.cpp
@@ -30,8 +30,8 @@
#include "mongo/db/query/plan_yield_policy.h"
-#include "mongo/db/concurrency/yield.h"
#include "mongo/db/global_environment_experiment.h"
+#include "mongo/db/query/query_yield.h"
#include "mongo/db/storage/record_fetcher.h"
namespace mongo {
@@ -72,7 +72,7 @@ namespace mongo {
_planYielding->saveState();
// Release and reacquire locks.
- Yield::yieldAllLocks(opCtx, 1, fetcher);
+ QueryYield::yieldAllLocks(opCtx, 1, fetcher);
_elapsedTracker.resetLastTime();
diff --git a/src/mongo/db/query/query_yield.cpp b/src/mongo/db/query/query_yield.cpp
new file mode 100644
index 00000000000..9d0e6d0bbb1
--- /dev/null
+++ b/src/mongo/db/query/query_yield.cpp
@@ -0,0 +1,110 @@
+/**
+ * Copyright (C) 2014 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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/db/query/query_yield.h"
+
+#include "mongo/db/curop.h"
+#include "mongo/db/operation_context.h"
+#include "mongo/db/storage/record_fetcher.h"
+
+namespace mongo {
+namespace {
+
+ void yieldOrSleepFor1Microsecond() {
+#ifdef _WIN32
+ SwitchToThread();
+#elif defined(__linux__)
+ pthread_yield();
+#else
+ sleepmicros(1);
+#endif
+ }
+
+}
+
+ // static
+ void QueryYield::yieldAllLocks(OperationContext* txn, int micros, RecordFetcher* fetcher) {
+ // Things have to happen here in a specific order:
+ // 1) Release lock mgr locks
+ // 2) Go to sleep
+ // 3) Touch the record we're yielding on, if there is one (RecordFetcher::fetch)
+ // 4) Reacquire lock mgr locks
+
+ Locker* locker = txn->lockState();
+
+ // If we had the read lock, we yield extra hard so that we don't starve writers.
+ bool hadReadLock = locker->hasAnyReadLock();
+
+ Locker::LockSnapshot snapshot;
+
+ // Nothing was unlocked, just return, yielding is pointless.
+ if (!locker->saveLockStateAndUnlock(&snapshot)) {
+ return;
+ }
+
+ // Track the number of yields in CurOp.
+ txn->getCurOp()->yielded();
+
+ if (hadReadLock) {
+ // TODO(kal): Is this still relevant? Probably not?
+ //
+ // Quote: This sleep helps reader threads yield to writer threads. Without this, the underlying
+ // reader/writer lock implementations are not sufficiently writer-greedy.
+
+#ifdef _WIN32
+ SwitchToThread();
+#else
+ if (0 == micros) {
+ yieldOrSleepFor1Microsecond();
+ }
+ else {
+ sleepmicros(1);
+ }
+#endif
+ }
+ else {
+ if (-1 == micros) {
+ sleepmicros(1);
+ //sleepmicros(Client::recommendedYieldMicros());
+ }
+ else if (0 == micros) {
+ yieldOrSleepFor1Microsecond();
+ }
+ else if (micros > 0) {
+ sleepmicros(micros);
+ }
+ }
+
+ if (fetcher) {
+ fetcher->fetch();
+ }
+
+ locker->restoreLockState(snapshot);
+ }
+
+} // namespace mongo
diff --git a/src/mongo/db/query/query_yield.h b/src/mongo/db/query/query_yield.h
new file mode 100644
index 00000000000..f7e6fe7a91f
--- /dev/null
+++ b/src/mongo/db/query/query_yield.h
@@ -0,0 +1,53 @@
+/**
+ * Copyright (C) 2014 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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
+
+namespace mongo {
+
+ class OperationContext;
+ class RecordFetcher;
+
+ /**
+ * See the documentation for yieldAllLocks(...).
+ */
+ class QueryYield {
+ QueryYield();
+
+ public:
+
+ /**
+ * If not in a nested context, unlocks all locks, suggests to the operating system to
+ * switch to another thread, and then reacquires all locks.
+ *
+ * If in a nested context (eg DBDirectClient), does nothing.
+ */
+ static void yieldAllLocks(OperationContext* txn, int micros, RecordFetcher* fetcher);
+ };
+
+} // namespace mongo