diff options
author | Andrew Morrow <acm@mongodb.com> | 2017-04-26 13:05:48 -0400 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2017-05-01 11:41:38 -0400 |
commit | b779cb0bf72267b8d6cefbb4739c118a720026da (patch) | |
tree | 6f9c9e6f5f8bb40f6c0446df61272307eb032112 /src/mongo/util/background.cpp | |
parent | 3d5c9f8da43874e8d058f323d6f4938efc2a3299 (diff) | |
download | mongo-b779cb0bf72267b8d6cefbb4739c118a720026da.tar.gz |
SERVER-29012 Enable ASAN strict init order checking and fix revealed issues
Diffstat (limited to 'src/mongo/util/background.cpp')
-rw-r--r-- | src/mongo/util/background.cpp | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/mongo/util/background.cpp b/src/mongo/util/background.cpp index 4fcd7b4ae1c..47a01afaf86 100644 --- a/src/mongo/util/background.cpp +++ b/src/mongo/util/background.cpp @@ -96,12 +96,10 @@ private: std::vector<PeriodicTask*> _tasks; }; -// We rely here on zero-initialization of 'runnerMutex' to distinguish whether we are -// running before or after static initialization for this translation unit has -// completed. In the former case, we assume no threads are present, so we do not need -// to use the mutex. When present, the mutex protects 'runner' and 'runnerDestroyed' -// below. -SimpleMutex* const runnerMutex = new SimpleMutex; +SimpleMutex* runnerMutex() { + static SimpleMutex mutex; + return &mutex; +} // A scoped lock like object that only locks/unlocks the mutex if it exists. class ConditionalScopedLock { @@ -120,10 +118,10 @@ private: }; // The unique PeriodicTaskRunner, also zero-initialized. -PeriodicTaskRunner* runner; +PeriodicTaskRunner* runner = nullptr; // The runner is never re-created once it has been destroyed. -bool runnerDestroyed; +bool runnerDestroyed = false; } // namespace @@ -228,7 +226,7 @@ bool BackgroundJob::running() const { // ------------------------- PeriodicTask::PeriodicTask() { - ConditionalScopedLock lock(runnerMutex); + ConditionalScopedLock lock(runnerMutex()); if (runnerDestroyed) return; @@ -239,7 +237,7 @@ PeriodicTask::PeriodicTask() { } PeriodicTask::~PeriodicTask() { - ConditionalScopedLock lock(runnerMutex); + ConditionalScopedLock lock(runnerMutex()); if (runnerDestroyed || !runner) return; @@ -247,7 +245,7 @@ PeriodicTask::~PeriodicTask() { } void PeriodicTask::startRunningPeriodicTasks() { - ConditionalScopedLock lock(runnerMutex); + ConditionalScopedLock lock(runnerMutex()); if (runnerDestroyed) return; @@ -258,7 +256,7 @@ void PeriodicTask::startRunningPeriodicTasks() { } Status PeriodicTask::stopRunningPeriodicTasks(int gracePeriodMillis) { - ConditionalScopedLock lock(runnerMutex); + ConditionalScopedLock lock(runnerMutex()); Status status = Status::OK(); if (runnerDestroyed || !runner) |