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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/disk_cache/simple/simple_backend_impl.h" 5 #include "net/disk_cache/simple/simple_backend_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstdlib> 8 #include <cstdlib>
9 #include <functional> 9 #include <functional>
10 10
11 #if defined(OS_POSIX) 11 #if defined(OS_POSIX)
12 #include <sys/resource.h> 12 #include <sys/resource.h>
13 #endif 13 #endif
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
18 #include "base/lazy_instance.h"
18 #include "base/location.h" 19 #include "base/location.h"
19 #include "base/metrics/field_trial.h" 20 #include "base/metrics/field_trial.h"
20 #include "base/metrics/histogram.h" 21 #include "base/metrics/histogram.h"
21 #include "base/metrics/sparse_histogram.h" 22 #include "base/metrics/sparse_histogram.h"
22 #include "base/single_thread_task_runner.h" 23 #include "base/single_thread_task_runner.h"
23 #include "base/sys_info.h" 24 #include "base/sys_info.h"
24 #include "base/task_runner_util.h" 25 #include "base/task_runner_util.h"
25 #include "base/thread_task_runner_handle.h" 26 #include "base/thread_task_runner_handle.h"
26 #include "base/threading/sequenced_worker_pool.h" 27 #include "base/threading/sequenced_worker_pool.h"
27 #include "base/time/time.h" 28 #include "base/time/time.h"
(...skipping 22 matching lines...) Expand all
50 51
51 // Maximum number of concurrent worker pool threads, which also is the limit 52 // Maximum number of concurrent worker pool threads, which also is the limit
52 // on concurrent IO (as we use one thread per IO request). 53 // on concurrent IO (as we use one thread per IO request).
53 const size_t kMaxWorkerThreads = 5U; 54 const size_t kMaxWorkerThreads = 5U;
54 55
55 const char kThreadNamePrefix[] = "SimpleCache"; 56 const char kThreadNamePrefix[] = "SimpleCache";
56 57
57 // Maximum fraction of the cache that one entry can consume. 58 // Maximum fraction of the cache that one entry can consume.
58 const int kMaxFileRatio = 8; 59 const int kMaxFileRatio = 8;
59 60
60 // A global sequenced worker pool to use for launching all tasks. 61 class LeakySequencedWorkerPool {
61 SequencedWorkerPool* g_sequenced_worker_pool = NULL; 62 public:
63 LeakySequencedWorkerPool()
64 : sequenced_worker_pool_(
65 new SequencedWorkerPool(kMaxWorkerThreads, kThreadNamePrefix)) {}
62 66
63 void MaybeCreateSequencedWorkerPool() { 67 void FlushForTesting() { sequenced_worker_pool_->FlushForTesting(); }
64 if (!g_sequenced_worker_pool) { 68
65 g_sequenced_worker_pool = 69 scoped_refptr<base::TaskRunner> GetTaskRunner() {
66 new SequencedWorkerPool(kMaxWorkerThreads, kThreadNamePrefix); 70 return sequenced_worker_pool_->GetTaskRunnerWithShutdownBehavior(
67 g_sequenced_worker_pool->AddRef(); // Leak it. 71 SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
68 } 72 }
69 } 73
74 private:
75 scoped_refptr<SequencedWorkerPool> sequenced_worker_pool_;
76
77 DISALLOW_COPY_AND_ASSIGN(LeakySequencedWorkerPool);
78 };
79
80 base::LazyInstance<LeakySequencedWorkerPool>::Leaky g_sequenced_worker_pool =
81 LAZY_INSTANCE_INITIALIZER;
70 82
71 bool g_fd_limit_histogram_has_been_populated = false; 83 bool g_fd_limit_histogram_has_been_populated = false;
72 84
73 void MaybeHistogramFdLimit(net::CacheType cache_type) { 85 void MaybeHistogramFdLimit(net::CacheType cache_type) {
74 if (g_fd_limit_histogram_has_been_populated) 86 if (g_fd_limit_histogram_has_been_populated)
75 return; 87 return;
76 88
77 // Used in histograms; add new entries at end. 89 // Used in histograms; add new entries at end.
78 enum FdLimitStatus { 90 enum FdLimitStatus {
79 FD_LIMIT_STATUS_UNSUPPORTED = 0, 91 FD_LIMIT_STATUS_UNSUPPORTED = 0,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS), 241 SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS),
230 net_log_(net_log) { 242 net_log_(net_log) {
231 MaybeHistogramFdLimit(cache_type_); 243 MaybeHistogramFdLimit(cache_type_);
232 } 244 }
233 245
234 SimpleBackendImpl::~SimpleBackendImpl() { 246 SimpleBackendImpl::~SimpleBackendImpl() {
235 index_->WriteToDisk(); 247 index_->WriteToDisk();
236 } 248 }
237 249
238 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) { 250 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) {
239 MaybeCreateSequencedWorkerPool(); 251 worker_pool_ = g_sequenced_worker_pool.Get().GetTaskRunner();
240
241 worker_pool_ = g_sequenced_worker_pool->GetTaskRunnerWithShutdownBehavior(
242 SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
243 252
244 index_.reset(new SimpleIndex( 253 index_.reset(new SimpleIndex(
245 base::ThreadTaskRunnerHandle::Get(), 254 base::ThreadTaskRunnerHandle::Get(),
246 this, 255 this,
247 cache_type_, 256 cache_type_,
248 make_scoped_ptr(new SimpleIndexFile( 257 make_scoped_ptr(new SimpleIndexFile(
249 cache_thread_, worker_pool_.get(), cache_type_, path_)))); 258 cache_thread_, worker_pool_.get(), cache_type_, path_))));
250 index_->ExecuteWhenReady( 259 index_->ExecuteWhenReady(
251 base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now())); 260 base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now()));
252 261
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 scoped_ptr<std::vector<uint64> > entry_hashes, 726 scoped_ptr<std::vector<uint64> > entry_hashes,
718 const net::CompletionCallback& callback, 727 const net::CompletionCallback& callback,
719 int result) { 728 int result) {
720 std::for_each( 729 std::for_each(
721 entry_hashes->begin(), entry_hashes->end(), 730 entry_hashes->begin(), entry_hashes->end(),
722 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), 731 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete),
723 this)); 732 this));
724 callback.Run(result); 733 callback.Run(result);
725 } 734 }
726 735
736 // static
727 void SimpleBackendImpl::FlushWorkerPoolForTesting() { 737 void SimpleBackendImpl::FlushWorkerPoolForTesting() {
728 if (g_sequenced_worker_pool) 738 g_sequenced_worker_pool.Get().FlushForTesting();
729 g_sequenced_worker_pool->FlushForTesting();
730 } 739 }
731 740
732 } // namespace disk_cache 741 } // namespace disk_cache
OLDNEW
« 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