|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #include "chrome/common/task_queue.h" |
|
6 |
|
7 #include "base/stl_util-inl.h" |
|
8 |
|
9 TaskQueue::TaskQueue() { |
|
10 } |
|
11 |
|
12 TaskQueue::~TaskQueue() { |
|
13 // We own all the pointes in |queue_|. It is our job to delete them. |
|
14 STLDeleteElements(&queue_); |
|
15 } |
|
16 |
|
17 void TaskQueue::Run() { |
|
18 // Nothing to run if our queue is empty. |
|
19 if (queue_.empty()) |
|
20 return; |
|
21 |
|
22 std::deque<Task*> ready; |
|
23 queue_.swap(ready); |
|
24 |
|
25 // Run the tasks that are ready. |
|
26 std::deque<Task*>::const_iterator task; |
|
27 for (task = ready.begin(); task != ready.end(); ++task) { |
|
28 // Run the task and then delete it. |
|
29 (*task)->Run(); |
|
30 delete (*task); |
|
31 } |
|
32 } |
|
33 |
|
34 void TaskQueue::Push(Task* task) { |
|
35 // Add the task to the back of the queue. |
|
36 queue_.push_back(task); |
|
37 } |
|
38 |
|
39 void TaskQueue::Clear() { |
|
40 // Delete all the elements in the queue and clear the dead pointers. |
|
41 STLDeleteElements(&queue_); |
|
42 } |
|
43 |
|
44 bool TaskQueue::Empty() const { |
|
45 return queue_.empty(); |
|
46 } |