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

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: leakiness Created 5 years, 11 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 8cf592aa48dddc0a56d81a011c78966ae674a14a..f11c26666b6d17376aba2d592cec7945d2fde5a5 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,11 +58,9 @@ 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;
-
-void MaybeCreateSequencedWorkerPool() {
- if (!g_sequenced_worker_pool) {
+class LeakySequencedWorkerPool {
+ public:
+ LeakySequencedWorkerPool() {
int max_worker_threads = kDefaultMaxWorkerThreads;
const std::string thread_count_field_trial =
@@ -71,11 +70,27 @@ void MaybeCreateSequencedWorkerPool() {
std::max(1, std::atoi(thread_count_field_trial.c_str()));
}
- g_sequenced_worker_pool = new SequencedWorkerPool(max_worker_threads,
- kThreadNamePrefix);
- g_sequenced_worker_pool->AddRef(); // Leak it.
+ sequenced_worker_pool_ = new SequencedWorkerPool(max_worker_threads,
+ kThreadNamePrefix);
+ }
+
+ void FlushForTesting() {
+ sequenced_worker_pool_->FlushForTesting();
+ }
+
+ 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;
@@ -245,10 +260,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(),
@@ -733,9 +745,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