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

Side by Side Diff: chrome/browser/chromeos/file_system_provider/queue.cc

Issue 957983002: Change DCHECKs to CHECKs to investigate a crash when aborting in Files app. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaned up. 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 | chrome/browser/chromeos/file_system_provider/throttled_file_system.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/location.h" 6 #include "base/location.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/single_thread_task_runner.h" 8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h" 9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/browser/chromeos/file_system_provider/queue.h" 10 #include "chrome/browser/chromeos/file_system_provider/queue.h"
11 11
12 namespace chromeos { 12 namespace chromeos {
13 namespace file_system_provider { 13 namespace file_system_provider {
14 14
15 Queue::Task::Task() : token(0) { 15 Queue::Task::Task() : token(0) {
16 } 16 }
17 17
18 Queue::Task::Task(size_t token, const AbortableCallback& callback) 18 Queue::Task::Task(size_t token, const AbortableCallback& callback)
19 : token(token), callback(callback) { 19 : token(token), callback(callback) {
20 } 20 }
21 21
22 Queue::Task::~Task() { 22 Queue::Task::~Task() {
23 } 23 }
24 24
25 Queue::Queue(size_t max_in_parallel) 25 Queue::Queue(size_t max_in_parallel)
26 : max_in_parallel_(max_in_parallel), 26 : max_in_parallel_(max_in_parallel),
27 next_token_(1), 27 next_token_(1),
28 weak_ptr_factory_(this) { 28 weak_ptr_factory_(this) {
29 DCHECK_LT(0u, max_in_parallel); 29 CHECK_LT(0u, max_in_parallel);
30 } 30 }
31 31
32 Queue::~Queue() { 32 Queue::~Queue() {
33 } 33 }
34 34
35 size_t Queue::NewToken() { 35 size_t Queue::NewToken() {
36 return next_token_++; 36 return next_token_++;
37 } 37 }
38 38
39 void Queue::Enqueue(size_t token, const AbortableCallback& callback) { 39 void Queue::Enqueue(size_t token, const AbortableCallback& callback) {
40 #if !NDEBUG 40 #if !NDEBUG
41 DCHECK(executed_.find(token) == executed_.end()); 41 CHECK(executed_.find(token) == executed_.end());
42 for (auto& task : pending_) { 42 for (auto& task : pending_) {
43 DCHECK(token != task.token); 43 CHECK(token != task.token);
44 } 44 }
45 #endif 45 #endif
46 pending_.push_back(Task(token, callback)); 46 pending_.push_back(Task(token, callback));
47 base::ThreadTaskRunnerHandle::Get()->PostTask( 47 base::ThreadTaskRunnerHandle::Get()->PostTask(
48 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 48 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
49 } 49 }
50 50
51 void Queue::Complete(size_t token) { 51 void Queue::Complete(size_t token) {
52 const auto it = executed_.find(token); 52 const auto it = executed_.find(token);
53 DCHECK(it != executed_.end()); 53 CHECK(it != executed_.end());
54 completed_[token] = it->second; 54 completed_[token] = it->second;
55 executed_.erase(it); 55 executed_.erase(it);
56 } 56 }
57 57
58 void Queue::Remove(size_t token) { 58 void Queue::Remove(size_t token) {
59 const auto it = completed_.find(token); 59 const auto it = completed_.find(token);
60 if (it != completed_.end()) { 60 if (it != completed_.end()) {
61 completed_.erase(it); 61 completed_.erase(it);
62 base::ThreadTaskRunnerHandle::Get()->PostTask( 62 base::ThreadTaskRunnerHandle::Get()->PostTask(
63 FROM_HERE, 63 FROM_HERE,
64 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 64 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
65 return; 65 return;
66 } 66 }
67 67
68 // If not completed, then it must have been aborted. 68 // If not completed, then it must have been aborted.
69 const auto aborted_it = aborted_.find(token); 69 const auto aborted_it = aborted_.find(token);
70 DCHECK(aborted_it != aborted_.end()); 70 CHECK(aborted_it != aborted_.end());
71 aborted_.erase(aborted_it); 71 aborted_.erase(aborted_it);
72 72
73 base::ThreadTaskRunnerHandle::Get()->PostTask( 73 base::ThreadTaskRunnerHandle::Get()->PostTask(
74 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 74 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
75 } 75 }
76 76
77 void Queue::MaybeRun() { 77 void Queue::MaybeRun() {
78 if (executed_.size() + completed_.size() == max_in_parallel_ || 78 if (executed_.size() + completed_.size() == max_in_parallel_ ||
79 !pending_.size()) { 79 !pending_.size()) {
80 return; 80 return;
81 } 81 }
82 82
83 DCHECK_GT(max_in_parallel_, executed_.size() + completed_.size()); 83 CHECK_GT(max_in_parallel_, executed_.size() + completed_.size());
84 Task task = pending_.front(); 84 Task task = pending_.front();
85 pending_.pop_front(); 85 pending_.pop_front();
86 86
87 executed_[task.token] = task; 87 executed_[task.token] = task;
88 AbortCallback abort_callback = task.callback.Run(); 88 AbortCallback abort_callback = task.callback.Run();
89 89
90 // It may happen that the task is completed and removed synchronously. Hence, 90 // It may happen that the task is completed and removed synchronously. Hence,
91 // we need to check if the task is still in the executed collection. 91 // we need to check if the task is still in the executed collection.
92 const auto executed_task_it = executed_.find(task.token); 92 const auto executed_task_it = executed_.find(task.token);
93 if (executed_task_it != executed_.end()) 93 if (executed_task_it != executed_.end())
94 executed_task_it->second.abort_callback = abort_callback; 94 executed_task_it->second.abort_callback = abort_callback;
95 } 95 }
96 96
97 void Queue::Abort(size_t token) { 97 void Queue::Abort(size_t token) {
98 // Check if it's running. 98 // Check if it's running.
99 const auto it = executed_.find(token); 99 const auto it = executed_.find(token);
100 if (it != executed_.end()) { 100 if (it != executed_.end()) {
101 Task task = it->second; 101 Task task = it->second;
102 aborted_[token] = task; 102 aborted_[token] = task;
103 executed_.erase(it); 103 executed_.erase(it);
104 DCHECK(!task.abort_callback.is_null()); 104 CHECK(!task.abort_callback.is_null());
105 task.abort_callback.Run(); 105 task.abort_callback.Run();
106 base::ThreadTaskRunnerHandle::Get()->PostTask( 106 base::ThreadTaskRunnerHandle::Get()->PostTask(
107 FROM_HERE, 107 FROM_HERE,
108 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 108 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
109 return; 109 return;
110 } 110 }
111 111
112 // Aborting not running tasks is linear. TODO(mtomasz): Optimize if feasible. 112 // Aborting not running tasks is linear. TODO(mtomasz): Optimize if feasible.
113 for (auto it = pending_.begin(); it != pending_.end(); ++it) { 113 for (auto it = pending_.begin(); it != pending_.end(); ++it) {
114 if (token == it->token) { 114 if (token == it->token) {
115 aborted_[token] = *it; 115 aborted_[token] = *it;
116 pending_.erase(it); 116 pending_.erase(it);
117 base::ThreadTaskRunnerHandle::Get()->PostTask( 117 base::ThreadTaskRunnerHandle::Get()->PostTask(
118 FROM_HERE, 118 FROM_HERE,
119 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 119 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
120 return; 120 return;
121 } 121 }
122 } 122 }
123 123
124 // The task is already removed, marked as completed or aborted. 124 // The task is already removed, marked as completed or aborted.
125 NOTREACHED(); 125 NOTREACHED();
126 } 126 }
127 127
128 } // namespace file_system_provider 128 } // namespace file_system_provider
129 } // namespace chromeos 129 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/file_system_provider/throttled_file_system.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698