summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2016-12-29 14:55:51 -0500
committerMark Benvenuto <mark.benvenuto@mongodb.com>2016-12-29 14:55:51 -0500
commit9ba33ad1fb3a27f24de309da25e84b3175aa4c82 (patch)
treed8f1deec113a099d3950c7b3ca12ab147a429bc7 /src
parent241b64c7bffb9c8cadc9b1a01844833218b0c8f0 (diff)
downloadmongo-9ba33ad1fb3a27f24de309da25e84b3175aa4c82.tar.gz
SERVER-16363 Remove all volatile storage types from server source code
Diffstat (limited to 'src')
-rw-r--r--src/mongo/crypto/mechanism_scram.cpp4
-rw-r--r--src/mongo/db/commands/generic.cpp2
-rw-r--r--src/mongo/db/geo/shapes.cpp4
-rw-r--r--src/mongo/db/prefetch.cpp3
-rw-r--r--src/mongo/db/repl/master_slave.cpp15
-rw-r--r--src/mongo/db/repl/master_slave.h6
-rw-r--r--src/mongo/db/repl/resync.cpp6
-rw-r--r--src/mongo/db/storage/mmap_v1/mmap_v1_extent_manager.cpp3
-rw-r--r--src/mongo/dbtests/threadedtests.cpp22
-rw-r--r--src/mongo/shell/dbshell.cpp9
-rw-r--r--src/mongo/util/secure_zero_memory.cpp3
11 files changed, 41 insertions, 36 deletions
diff --git a/src/mongo/crypto/mechanism_scram.cpp b/src/mongo/crypto/mechanism_scram.cpp
index 329da56c9f8..5bc797ef704 100644
--- a/src/mongo/crypto/mechanism_scram.cpp
+++ b/src/mongo/crypto/mechanism_scram.cpp
@@ -209,7 +209,9 @@ std::string generateClientProof(const unsigned char saltedPassword[hashSize],
* TODO: evaluate if LTO inlines or changes the code flow of this function.
*/
NOINLINE_DECL
-bool memequal(volatile const unsigned char* s1, volatile const unsigned char* s2, size_t length) {
+bool memequal(volatile const unsigned char* s1, // NOLINT - using volatile to
+ volatile const unsigned char* s2, // NOLINT - disable compiler optimizations
+ size_t length) {
unsigned char ret = 0;
for (size_t i = 0; i < length; ++i) {
diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp
index 57e96ac75c7..f554498bfbf 100644
--- a/src/mongo/db/commands/generic.cpp
+++ b/src/mongo/db/commands/generic.cpp
@@ -310,7 +310,7 @@ public:
namespace {
MONGO_FP_DECLARE(crashOnShutdown);
-int* volatile illegalAddress;
+int* volatile illegalAddress; // NOLINT - used for fail point only
} // namespace
void CmdShutdown::addRequiredPrivileges(const std::string& dbname,
diff --git a/src/mongo/db/geo/shapes.cpp b/src/mongo/db/geo/shapes.cpp
index 5ccc0fb85d4..94b52f94948 100644
--- a/src/mongo/db/geo/shapes.cpp
+++ b/src/mongo/db/geo/shapes.cpp
@@ -501,7 +501,7 @@ double distanceCompare(const Point& p1, const Point& p2, double radius) {
// inline will make certain geo tests fail. Of course this check will force volatile
// for all 32-bit systems, not just affected systems.
if (sizeof(void*) <= 4) {
- volatile double sum = p2.y > p1.y ? p1.y + radius : p2.y + radius;
+ volatile double sum = p2.y > p1.y ? p1.y + radius : p2.y + radius; // NOLINT
return p2.y > p1.y ? p2.y - sum : p1.y - sum;
} else {
// Original math, correct for most systems
@@ -511,7 +511,7 @@ double distanceCompare(const Point& p1, const Point& p2, double radius) {
if (b == 0) {
if (sizeof(void*) <= 4) {
- volatile double sum = p2.x > p1.x ? p1.x + radius : p2.x + radius;
+ volatile double sum = p2.x > p1.x ? p1.x + radius : p2.x + radius; // NOLINT
return p2.x > p1.x ? p2.x - sum : p1.x - sum;
} else {
return p2.x > p1.x ? p2.x - (p1.x + radius) : p1.x - (p2.x + radius);
diff --git a/src/mongo/db/prefetch.cpp b/src/mongo/db/prefetch.cpp
index a1d6315bfe1..d698def8a3a 100644
--- a/src/mongo/db/prefetch.cpp
+++ b/src/mongo/db/prefetch.cpp
@@ -126,7 +126,8 @@ void prefetchRecordPages(OperationContext* txn, Database* db, const char* ns, co
try {
if (Helpers::findById(txn, db, ns, builder.done(), result)) {
// do we want to use Record::touch() here? it's pretty similar.
- volatile char _dummy_char = '\0';
+ // volatile - avoid compiler optimizations for touching a mmap page
+ volatile char _dummy_char = '\0'; // NOLINT
// Touch the first word on every page in order to fault it into memory
for (int i = 0; i < result.objsize(); i += g_minOSPageSizeBytes) {
diff --git a/src/mongo/db/repl/master_slave.cpp b/src/mongo/db/repl/master_slave.cpp
index 5a05b023088..6f25242273c 100644
--- a/src/mongo/db/repl/master_slave.cpp
+++ b/src/mongo/db/repl/master_slave.cpp
@@ -69,6 +69,7 @@
#include "mongo/db/server_parameters.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/storage_options.h"
+#include "mongo/platform/atomic_word.h"
#include "mongo/stdx/thread.h"
#include "mongo/util/exit.h"
#include "mongo/util/log.h"
@@ -98,8 +99,8 @@ void pretouchOperation(OperationContext* txn, const BSONObj& op);
void pretouchN(vector<BSONObj>&, unsigned a, unsigned b);
/* if 1 sync() is running */
-volatile int syncing = 0;
-volatile int relinquishSyncingSome = 0;
+AtomicInt32 syncing(0);
+AtomicInt32 relinquishSyncingSome(0);
/* output by the web console */
const char* replInfo = "";
@@ -1257,8 +1258,7 @@ static void replMain(OperationContext* txn) {
}
// i.e., there is only one sync thread running. we will want to change/fix this.
- verify(syncing == 0);
- syncing++;
+ invariant(syncing.swap(1) == 0);
}
try {
@@ -1281,12 +1281,11 @@ static void replMain(OperationContext* txn) {
{
ScopedTransaction transaction(txn, MODE_X);
Lock::GlobalWrite lk(txn->lockState());
- verify(syncing == 1);
- syncing--;
+ invariant(syncing.swap(0) == 1);
}
- if (relinquishSyncingSome) {
- relinquishSyncingSome = 0;
+ if (relinquishSyncingSome.load()) {
+ relinquishSyncingSome.store(0);
s = restartSyncAfterSleep; // sleep before going back in to syncing=1
}
diff --git a/src/mongo/db/repl/master_slave.h b/src/mongo/db/repl/master_slave.h
index d6234df9bc3..3a869a750ba 100644
--- a/src/mongo/db/repl/master_slave.h
+++ b/src/mongo/db/repl/master_slave.h
@@ -28,8 +28,8 @@
#pragma once
-
#include "mongo/db/repl/oplogreader.h"
+#include "mongo/platform/atomic_word.h"
/* replication data overview
@@ -53,8 +53,8 @@ namespace repl {
void startMasterSlave(OperationContext* txn);
// externed for use with resync.cpp
-extern volatile int relinquishSyncingSome;
-extern volatile int syncing;
+extern AtomicInt32 relinquishSyncingSome;
+extern AtomicInt32 syncing;
extern const char* replInfo;
diff --git a/src/mongo/db/repl/resync.cpp b/src/mongo/db/repl/resync.cpp
index 9fcaff08bb5..e45022440a3 100644
--- a/src/mongo/db/repl/resync.cpp
+++ b/src/mongo/db/repl/resync.cpp
@@ -132,15 +132,15 @@ public:
// reloaded with new saved state on next pass.
Timer t;
while (1) {
- if (syncing == 0 || t.millis() > 30000)
+ if (syncing.load() == 0 || t.millis() > 30000)
break;
{
Lock::TempRelease t(txn->lockState());
- relinquishSyncingSome = 1;
+ relinquishSyncingSome.store(1);
sleepmillis(1);
}
}
- if (syncing) {
+ if (syncing.load()) {
errmsg = "timeout waiting for sync() to finish";
return false;
}
diff --git a/src/mongo/db/storage/mmap_v1/mmap_v1_extent_manager.cpp b/src/mongo/db/storage/mmap_v1/mmap_v1_extent_manager.cpp
index a8141798e3c..761fecfb075 100644
--- a/src/mongo/db/storage/mmap_v1/mmap_v1_extent_manager.cpp
+++ b/src/mongo/db/storage/mmap_v1/mmap_v1_extent_manager.cpp
@@ -70,7 +70,8 @@ MONGO_FP_DECLARE(recordNeedsFetchFail);
// Used to make sure the compiler doesn't get too smart on us when we're
// trying to touch records.
-volatile int __record_touch_dummy = 1;
+// volatile - avoid compiler optimizations for touching a mmap page
+volatile int __record_touch_dummy = 1; // NOLINT
class MmapV1RecordFetcher : public RecordFetcher {
MONGO_DISALLOW_COPYING(MmapV1RecordFetcher);
diff --git a/src/mongo/dbtests/threadedtests.cpp b/src/mongo/dbtests/threadedtests.cpp
index 956a872447a..498e39f71e7 100644
--- a/src/mongo/dbtests/threadedtests.cpp
+++ b/src/mongo/dbtests/threadedtests.cpp
@@ -390,8 +390,8 @@ template <class whichmutex, class scoped>
class Slack : public ThreadedTest<17> {
public:
Slack() {
- k = 0;
- done = false;
+ k.store(0);
+ done.store(false);
a = b = 0;
locks = 0;
}
@@ -403,7 +403,7 @@ private:
char pad2[128];
unsigned locks;
char pad3[128];
- volatile int k;
+ AtomicInt32 k;
virtual void validate() {
if (once++ == 0) {
@@ -417,15 +417,15 @@ private:
while (1) {
b++;
//__sync_synchronize();
- if (k) {
+ if (k.load()) {
a++;
}
sleepmillis(0);
- if (done)
+ if (done.load())
break;
}
}
- volatile bool done;
+ AtomicBool done;
virtual void subthread(int x) {
if (x == 1) {
watch();
@@ -435,19 +435,19 @@ private:
unsigned lks = 0;
while (1) {
scoped lk(m);
- k = 1;
+ k.store(1);
// not very long, we'd like to simulate about 100K locks per second
sleepalittle();
lks++;
- if (done || t.millis() > 1500) {
+ if (done.load() || t.millis() > 1500) {
locks += lks;
- k = 0;
+ k.store(0);
break;
}
- k = 0;
+ k.store(0);
//__sync_synchronize();
}
- done = true;
+ done.store(true);
}
};
diff --git a/src/mongo/shell/dbshell.cpp b/src/mongo/shell/dbshell.cpp
index 5c33596b5ce..530b4b32f8b 100644
--- a/src/mongo/shell/dbshell.cpp
+++ b/src/mongo/shell/dbshell.cpp
@@ -50,6 +50,7 @@
#include "mongo/logger/console_appender.h"
#include "mongo/logger/logger.h"
#include "mongo/logger/message_event_utf8_encoder.h"
+#include "mongo/platform/atomic_word.h"
#include "mongo/scripting/engine.h"
#include "mongo/shell/linenoise.h"
#include "mongo/shell/shell_options.h"
@@ -85,7 +86,7 @@ using namespace mongo;
string historyFile;
bool gotInterrupted = false;
bool inMultiLine = false;
-static volatile bool atPrompt = false; // can eval before getting to prompt
+static AtomicBool atPrompt(false); // can eval before getting to prompt
namespace {
const auto kDefaultMongoURL = "mongodb://127.0.0.1:27017"_sd;
@@ -180,7 +181,7 @@ void killOps() {
if (mongo::shell_utils::_nokillop)
return;
- if (atPrompt)
+ if (atPrompt.load())
return;
sleepmillis(10); // give current op a chance to finish
@@ -195,14 +196,14 @@ void quitNicely(int sig) {
// the returned string is allocated with strdup() or malloc() and must be freed by calling free()
char* shellReadline(const char* prompt, int handlesigint = 0) {
- atPrompt = true;
+ atPrompt.store(true);
char* ret = linenoise(prompt);
if (!ret) {
gotInterrupted = true; // got ^C, break out of multiline
}
- atPrompt = false;
+ atPrompt.store(false);
return ret;
}
diff --git a/src/mongo/util/secure_zero_memory.cpp b/src/mongo/util/secure_zero_memory.cpp
index ee7bb48218e..6021238fec8 100644
--- a/src/mongo/util/secure_zero_memory.cpp
+++ b/src/mongo/util/secure_zero_memory.cpp
@@ -53,7 +53,8 @@ void secureZeroMemory(void* mem, size_t size) {
fassert(28752, memset_s(mem, size, 0, size) == 0);
#else
// fall back to using volatile pointer
- volatile char* p = reinterpret_cast<volatile char*>(mem);
+ // using volatile to disable compiler optimizations
+ volatile char* p = reinterpret_cast<volatile char*>(mem); // NOLINT
while (size--) {
*p++ = 0;
}