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

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: leakiness 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 int kDefaultMaxWorkerThreads = 50; 54 const int kDefaultMaxWorkerThreads = 50;
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:
62 63 LeakySequencedWorkerPool() {
63 void MaybeCreateSequencedWorkerPool() {
64 if (!g_sequenced_worker_pool) {
65 int max_worker_threads = kDefaultMaxWorkerThreads; 64 int max_worker_threads = kDefaultMaxWorkerThreads;
66 65
67 const std::string thread_count_field_trial = 66 const std::string thread_count_field_trial =
68 base::FieldTrialList::FindFullName("SimpleCacheMaxThreads"); 67 base::FieldTrialList::FindFullName("SimpleCacheMaxThreads");
pasko 2015/02/19 12:09:13 I removed this code for the experiment recently, p
mmenke 2015/02/19 16:53:07 Done.
69 if (!thread_count_field_trial.empty()) { 68 if (!thread_count_field_trial.empty()) {
70 max_worker_threads = 69 max_worker_threads =
71 std::max(1, std::atoi(thread_count_field_trial.c_str())); 70 std::max(1, std::atoi(thread_count_field_trial.c_str()));
72 } 71 }
73 72
74 g_sequenced_worker_pool = new SequencedWorkerPool(max_worker_threads, 73 sequenced_worker_pool_ = new SequencedWorkerPool(max_worker_threads,
75 kThreadNamePrefix); 74 kThreadNamePrefix);
76 g_sequenced_worker_pool->AddRef(); // Leak it.
77 } 75 }
78 } 76
77 void FlushForTesting() {
78 sequenced_worker_pool_->FlushForTesting();
79 }
80
81 scoped_refptr<base::TaskRunner> GetTaskRunner() {
82 return sequenced_worker_pool_->GetTaskRunnerWithShutdownBehavior(
83 SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
84 }
85
86 private:
87 scoped_refptr<SequencedWorkerPool> sequenced_worker_pool_;
88
89 DISALLOW_COPY_AND_ASSIGN(LeakySequencedWorkerPool);
90 };
91
92 base::LazyInstance<LeakySequencedWorkerPool>::Leaky g_sequenced_worker_pool =
93 LAZY_INSTANCE_INITIALIZER;
79 94
80 bool g_fd_limit_histogram_has_been_populated = false; 95 bool g_fd_limit_histogram_has_been_populated = false;
81 96
82 void MaybeHistogramFdLimit(net::CacheType cache_type) { 97 void MaybeHistogramFdLimit(net::CacheType cache_type) {
83 if (g_fd_limit_histogram_has_been_populated) 98 if (g_fd_limit_histogram_has_been_populated)
84 return; 99 return;
85 100
86 // Used in histograms; add new entries at end. 101 // Used in histograms; add new entries at end.
87 enum FdLimitStatus { 102 enum FdLimitStatus {
88 FD_LIMIT_STATUS_UNSUPPORTED = 0, 103 FD_LIMIT_STATUS_UNSUPPORTED = 0,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS), 253 SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS),
239 net_log_(net_log) { 254 net_log_(net_log) {
240 MaybeHistogramFdLimit(cache_type_); 255 MaybeHistogramFdLimit(cache_type_);
241 } 256 }
242 257
243 SimpleBackendImpl::~SimpleBackendImpl() { 258 SimpleBackendImpl::~SimpleBackendImpl() {
244 index_->WriteToDisk(); 259 index_->WriteToDisk();
245 } 260 }
246 261
247 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) { 262 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) {
248 MaybeCreateSequencedWorkerPool(); 263 worker_pool_ = g_sequenced_worker_pool.Get().GetTaskRunner();
249
250 worker_pool_ = g_sequenced_worker_pool->GetTaskRunnerWithShutdownBehavior(
251 SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
252 264
253 index_.reset(new SimpleIndex( 265 index_.reset(new SimpleIndex(
254 base::ThreadTaskRunnerHandle::Get(), 266 base::ThreadTaskRunnerHandle::Get(),
255 this, 267 this,
256 cache_type_, 268 cache_type_,
257 make_scoped_ptr(new SimpleIndexFile( 269 make_scoped_ptr(new SimpleIndexFile(
258 cache_thread_, worker_pool_.get(), cache_type_, path_)))); 270 cache_thread_, worker_pool_.get(), cache_type_, path_))));
259 index_->ExecuteWhenReady( 271 index_->ExecuteWhenReady(
260 base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now())); 272 base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now()));
261 273
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 scoped_ptr<std::vector<uint64> > entry_hashes, 738 scoped_ptr<std::vector<uint64> > entry_hashes,
727 const net::CompletionCallback& callback, 739 const net::CompletionCallback& callback,
728 int result) { 740 int result) {
729 std::for_each( 741 std::for_each(
730 entry_hashes->begin(), entry_hashes->end(), 742 entry_hashes->begin(), entry_hashes->end(),
731 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), 743 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete),
732 this)); 744 this));
733 callback.Run(result); 745 callback.Run(result);
734 } 746 }
735 747
748 // static
736 void SimpleBackendImpl::FlushWorkerPoolForTesting() { 749 void SimpleBackendImpl::FlushWorkerPoolForTesting() {
737 if (g_sequenced_worker_pool) 750 g_sequenced_worker_pool.Get().FlushForTesting();
738 g_sequenced_worker_pool->FlushForTesting();
739 } 751 }
740 752
741 } // namespace disk_cache 753 } // 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