jhawkins@chromium.org | 2af12f2 | 2011-11-16 08:36:30 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | #ifndef PENDING_TASK_H_ |
| 6 | #define PENDING_TASK_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <queue> |
| 10 | |
rsleevi@chromium.org | 3f19acb | 2011-11-18 15:08:02 +0900 | [diff] [blame] | 11 | #include "base/base_export.h" |
jhawkins@chromium.org | 2af12f2 | 2011-11-16 08:36:30 +0900 | [diff] [blame] | 12 | #include "base/callback.h" |
| 13 | #include "base/location.h" |
| 14 | #include "base/time.h" |
| 15 | #include "base/tracking_info.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue |
| 20 | // for use by classes that queue and execute tasks. |
rsleevi@chromium.org | 3f19acb | 2011-11-18 15:08:02 +0900 | [diff] [blame] | 21 | struct BASE_EXPORT PendingTask : public TrackingInfo { |
jhawkins@chromium.org | 2af12f2 | 2011-11-16 08:36:30 +0900 | [diff] [blame] | 22 | PendingTask(const tracked_objects::Location& posted_from, |
| 23 | const Closure& task); |
| 24 | PendingTask(const tracked_objects::Location& posted_from, |
| 25 | const Closure& task, |
| 26 | TimeTicks delayed_run_time, |
| 27 | bool nestable); |
| 28 | ~PendingTask(); |
| 29 | |
| 30 | // Used to support sorting. |
| 31 | bool operator<(const PendingTask& other) const; |
| 32 | |
| 33 | // The task to run. |
| 34 | Closure task; |
| 35 | |
| 36 | // The site this PendingTask was posted from. |
| 37 | tracked_objects::Location posted_from; |
| 38 | |
| 39 | // Secondary sort key for run time. |
| 40 | int sequence_num; |
| 41 | |
| 42 | // OK to dispatch from a nested loop. |
| 43 | bool nestable; |
| 44 | }; |
| 45 | |
| 46 | // Wrapper around std::queue specialized for PendingTask which adds a Swap |
| 47 | // helper method. |
rsleevi@chromium.org | 3f19acb | 2011-11-18 15:08:02 +0900 | [diff] [blame] | 48 | class BASE_EXPORT TaskQueue : public std::queue<PendingTask> { |
jhawkins@chromium.org | 2af12f2 | 2011-11-16 08:36:30 +0900 | [diff] [blame] | 49 | public: |
| 50 | void Swap(TaskQueue* queue); |
| 51 | }; |
| 52 | |
jhawkins@chromium.org | 303de23 | 2011-11-18 08:26:53 +0900 | [diff] [blame] | 53 | // PendingTasks are sorted by their |delayed_run_time| property. |
| 54 | typedef std::priority_queue<base::PendingTask> DelayedTaskQueue; |
| 55 | |
jhawkins@chromium.org | 2af12f2 | 2011-11-16 08:36:30 +0900 | [diff] [blame] | 56 | } // namespace base |
| 57 | |
| 58 | #endif // PENDING_TASK_H_ |