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

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

Issue 845083005: [fsp] Simplify aborting logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. 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 unified diff | Download patch
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), completed(false) { 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), completed(false), 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 DCHECK_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 AbortCallback 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 const auto it = executed_.find(token); 41 DCHECK(executed_.find(token) == executed_.end());
42 DCHECK(it == executed_.end());
43 for (auto& task : pending_) { 42 for (auto& task : pending_) {
44 DCHECK(token != task.token); 43 DCHECK(token != task.token);
45 } 44 }
46 #endif 45 #endif
47 pending_.push_back(Task(token, callback)); 46 pending_.push_back(Task(token, callback));
48 base::ThreadTaskRunnerHandle::Get()->PostTask( 47 base::ThreadTaskRunnerHandle::Get()->PostTask(
49 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 48 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
50 return base::Bind(&Queue::Abort, weak_ptr_factory_.GetWeakPtr(), token);
51 } 49 }
52 50
53 void Queue::Complete(size_t token) { 51 void Queue::Complete(size_t token) {
54 const auto it = executed_.find(token); 52 const auto it = executed_.find(token);
55 DCHECK(it != executed_.end() && !it->second.completed); 53 DCHECK(it != executed_.end());
56 it->second.completed = true; 54 completed_[token] = it->second;
55 executed_.erase(it);
57 } 56 }
58 57
59 void Queue::Remove(size_t token) { 58 void Queue::Remove(size_t token) {
60 const auto it = executed_.find(token); 59 const auto it = completed_.find(token);
61 DCHECK(it != executed_.end() && it->second.completed); 60 if (it != completed_.end()) {
61 completed_.erase(it);
62 base::ThreadTaskRunnerHandle::Get()->PostTask(
63 FROM_HERE,
64 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
65 return;
66 }
62 67
63 executed_.erase(it); 68 // If not completed, then it must be aborted.
hirono 2015/01/14 09:27:04 nit: it must be aborted -> It sounds the task is a
mtomasz 2015/01/14 09:31:13 Good point. Done.
69 const auto aborted_it = aborted_.find(token);
70 DCHECK(aborted_it != aborted_.end());
71 aborted_.erase(aborted_it);
72
64 base::ThreadTaskRunnerHandle::Get()->PostTask( 73 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 74 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
66 } 75 }
67 76
68 void Queue::MaybeRun() { 77 void Queue::MaybeRun() {
69 if (executed_.size() == max_in_parallel_ || !pending_.size()) { 78 if (executed_.size() == max_in_parallel_ || !pending_.size()) {
70 return; 79 return;
71 } 80 }
72 81
73 DCHECK_GT(max_in_parallel_, executed_.size()); 82 DCHECK_GT(max_in_parallel_, executed_.size());
74 Task task = pending_.front(); 83 Task task = pending_.front();
75 pending_.pop_front(); 84 pending_.pop_front();
76 85
77 executed_[task.token] = task; 86 executed_[task.token] = task;
78 executed_[task.token].abort_callback = task.callback.Run(); 87 executed_[task.token].abort_callback = task.callback.Run();
79 } 88 }
80 89
81 void Queue::Abort(size_t token, 90 void Queue::Abort(size_t token) {
82 const storage::AsyncFileUtil::StatusCallback& callback) {
83 // Check if it's running. 91 // Check if it's running.
84 const auto it = executed_.find(token); 92 const auto it = executed_.find(token);
85 if (it != executed_.end()) { 93 if (it != executed_.end()) {
86 const Task& task = it->second; 94 Task task = it->second;
87 // If the task is marked as completed, then it's impossible to abort it. 95 aborted_[token] = task;
88 if (task.completed) { 96 executed_.erase(it);
89 callback.Run(base::File::FILE_ERROR_INVALID_OPERATION);
90 return;
91 }
92 DCHECK(!task.abort_callback.is_null()); 97 DCHECK(!task.abort_callback.is_null());
93 it->second.abort_callback.Run(callback); 98 task.abort_callback.Run();
94 executed_.erase(it);
95 base::ThreadTaskRunnerHandle::Get()->PostTask( 99 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE, 100 FROM_HERE,
97 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 101 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
98 return; 102 return;
99 } 103 }
100 104
101 // Aborting not running tasks is linear. TODO(mtomasz): Optimize if feasible. 105 // Aborting not running tasks is linear. TODO(mtomasz): Optimize if feasible.
102 for (auto it = pending_.begin(); it != pending_.end(); ++it) { 106 for (auto it = pending_.begin(); it != pending_.end(); ++it) {
103 if (token == it->token) { 107 if (token == it->token) {
108 aborted_[token] = *it;
104 pending_.erase(it); 109 pending_.erase(it);
105 callback.Run(base::File::FILE_OK);
106 base::ThreadTaskRunnerHandle::Get()->PostTask( 110 base::ThreadTaskRunnerHandle::Get()->PostTask(
107 FROM_HERE, 111 FROM_HERE,
108 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); 112 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
109 return; 113 return;
110 } 114 }
111 } 115 }
112 116
113 // The task is already removed. 117 // The task is already removed, marked as completed or aborted.
114 callback.Run(base::File::FILE_ERROR_INVALID_OPERATION); 118 NOTREACHED();
115 } 119 }
116 120
117 } // namespace file_system_provider 121 } // namespace file_system_provider
118 } // namespace chromeos 122 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/file_system_provider/queue.h ('k') | chrome/browser/chromeos/file_system_provider/queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698