| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/task_queue.h" | 5 #include "base/task_queue.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 | 9 |
| 10 TaskQueue::TaskQueue() { | 10 TaskQueue::TaskQueue() { |
| 11 } | 11 } |
| 12 | 12 |
| 13 TaskQueue::~TaskQueue() { | 13 TaskQueue::~TaskQueue() { |
| 14 // We own all the pointes in |queue_|. It is our job to delete them. | 14 // We own all the pointes in |queue_|. It is our job to delete them. |
| 15 STLDeleteElements(&queue_); | 15 STLDeleteElements(&queue_); |
| 16 } | 16 } |
| 17 | 17 |
| 18 void TaskQueue::Push(Task* task) { |
| 19 DCHECK(task); |
| 20 |
| 21 // Add the task to the back of the queue. |
| 22 queue_.push_back(task); |
| 23 } |
| 24 |
| 25 void TaskQueue::Clear() { |
| 26 // Delete all the elements in the queue and clear the dead pointers. |
| 27 STLDeleteElements(&queue_); |
| 28 } |
| 29 |
| 30 bool TaskQueue::IsEmpty() const { |
| 31 return queue_.empty(); |
| 32 } |
| 33 |
| 18 void TaskQueue::Run() { | 34 void TaskQueue::Run() { |
| 19 // Nothing to run if our queue is empty. | 35 // Nothing to run if our queue is empty. |
| 20 if (queue_.empty()) | 36 if (queue_.empty()) |
| 21 return; | 37 return; |
| 22 | 38 |
| 23 std::deque<Task*> ready; | 39 std::deque<Task*> ready; |
| 24 queue_.swap(ready); | 40 queue_.swap(ready); |
| 25 | 41 |
| 26 // Run the tasks that are ready. | 42 // Run the tasks that are ready. |
| 27 std::deque<Task*>::const_iterator task; | 43 std::deque<Task*>::const_iterator task; |
| 28 for (task = ready.begin(); task != ready.end(); ++task) { | 44 for (task = ready.begin(); task != ready.end(); ++task) { |
| 29 // Run the task and then delete it. | 45 // Run the task and then delete it. |
| 30 (*task)->Run(); | 46 (*task)->Run(); |
| 31 delete (*task); | 47 delete (*task); |
| 32 } | 48 } |
| 33 } | 49 } |
| 34 | |
| 35 void TaskQueue::Push(Task* task) { | |
| 36 DCHECK(task); | |
| 37 | |
| 38 // Add the task to the back of the queue. | |
| 39 queue_.push_back(task); | |
| 40 } | |
| 41 | |
| 42 void TaskQueue::Clear() { | |
| 43 // Delete all the elements in the queue and clear the dead pointers. | |
| 44 STLDeleteElements(&queue_); | |
| 45 } | |
| 46 | |
| 47 bool TaskQueue::IsEmpty() const { | |
| 48 return queue_.empty(); | |
| 49 } | |
| OLD | NEW |