Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(874)

Unified Diff: net/disk_cache/simple/simple_backend_impl.cc

Issue 851013003: Make SimpleCache creation threadsafe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge and git cl format Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/simple/simple_backend_impl.cc
diff --git a/net/disk_cache/simple/simple_backend_impl.cc b/net/disk_cache/simple/simple_backend_impl.cc
index 6c43eeb9d6e0b439d577d1d0aff79b0dd39d5654..38086441ebda528f93034956d4e85eb94945cbd3 100644
--- a/net/disk_cache/simple/simple_backend_impl.cc
+++ b/net/disk_cache/simple/simple_backend_impl.cc
@@ -15,6 +15,7 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_util.h"
+#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
@@ -57,16 +58,27 @@ const char kThreadNamePrefix[] = "SimpleCache";
// Maximum fraction of the cache that one entry can consume.
const int kMaxFileRatio = 8;
-// A global sequenced worker pool to use for launching all tasks.
-SequencedWorkerPool* g_sequenced_worker_pool = NULL;
+class LeakySequencedWorkerPool {
+ public:
+ LeakySequencedWorkerPool()
+ : sequenced_worker_pool_(
+ new SequencedWorkerPool(kMaxWorkerThreads, kThreadNamePrefix)) {}
+
+ void FlushForTesting() { sequenced_worker_pool_->FlushForTesting(); }
-void MaybeCreateSequencedWorkerPool() {
- if (!g_sequenced_worker_pool) {
- g_sequenced_worker_pool =
- new SequencedWorkerPool(kMaxWorkerThreads, kThreadNamePrefix);
- g_sequenced_worker_pool->AddRef(); // Leak it.
+ scoped_refptr<base::TaskRunner> GetTaskRunner() {
+ return sequenced_worker_pool_->GetTaskRunnerWithShutdownBehavior(
+ SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
}
-}
+
+ private:
+ scoped_refptr<SequencedWorkerPool> sequenced_worker_pool_;
+
+ DISALLOW_COPY_AND_ASSIGN(LeakySequencedWorkerPool);
+};
+
+base::LazyInstance<LeakySequencedWorkerPool>::Leaky g_sequenced_worker_pool =
+ LAZY_INSTANCE_INITIALIZER;
bool g_fd_limit_histogram_has_been_populated = false;
@@ -236,10 +248,7 @@ SimpleBackendImpl::~SimpleBackendImpl() {
}
int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) {
- MaybeCreateSequencedWorkerPool();
-
- worker_pool_ = g_sequenced_worker_pool->GetTaskRunnerWithShutdownBehavior(
- SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
+ worker_pool_ = g_sequenced_worker_pool.Get().GetTaskRunner();
index_.reset(new SimpleIndex(
base::ThreadTaskRunnerHandle::Get(),
@@ -724,9 +733,9 @@ void SimpleBackendImpl::DoomEntriesComplete(
callback.Run(result);
}
+// static
void SimpleBackendImpl::FlushWorkerPoolForTesting() {
- if (g_sequenced_worker_pool)
- g_sequenced_worker_pool->FlushForTesting();
+ g_sequenced_worker_pool.Get().FlushForTesting();
}
} // namespace disk_cache
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698