fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 1 | // Copyright 2016 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 BASE_TASK_SCHEDULER_SEQUENCE_H_ |
| 6 | #define BASE_TASK_SCHEDULER_SEQUENCE_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 10 | #include "base/base_export.h" |
Brett Wilson | 3d88e776 | 2017-08-14 19:54:38 | [diff] [blame] | 11 | #include "base/containers/queue.h" |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
Robert Liao | d07daf9 | 2017-12-18 01:38:07 | [diff] [blame] | 14 | #include "base/optional.h" |
fdoray | 4bf182a | 2016-07-26 23:48:06 | [diff] [blame] | 15 | #include "base/sequence_token.h" |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 16 | #include "base/task_scheduler/scheduler_lock.h" |
| 17 | #include "base/task_scheduler/sequence_sort_key.h" |
| 18 | #include "base/task_scheduler/task.h" |
| 19 | #include "base/task_scheduler/task_traits.h" |
Jeffrey He | c42cc008 | 2017-06-13 14:39:16 | [diff] [blame] | 20 | #include "base/threading/sequence_local_storage_map.h" |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | namespace internal { |
| 24 | |
fdoray | 0c97aa3 | 2016-10-12 12:21:27 | [diff] [blame] | 25 | // A Sequence holds slots each containing up to a single Task that must be |
| 26 | // executed in posting order. |
| 27 | // |
| 28 | // In comments below, an "empty Sequence" is a Sequence with no slot. |
gab | a8e9e2c | 2016-05-02 21:17:47 | [diff] [blame] | 29 | // |
| 30 | // Note: there is a known refcounted-ownership cycle in the Scheduler |
| 31 | // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ... |
| 32 | // This is okay so long as the other owners of Sequence (PriorityQueue and |
robliao | 7a58439 | 2016-06-22 18:36:41 | [diff] [blame] | 33 | // SchedulerWorker in alternation and |
| 34 | // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork() |
gab | a8e9e2c | 2016-05-02 21:17:47 | [diff] [blame] | 35 | // temporarily) keep running it (and taking Tasks from it as a result). A |
| 36 | // dangling reference cycle would only occur should they release their reference |
| 37 | // to it while it's not empty. In other words, it is only correct for them to |
| 38 | // release it after PopTask() returns false to indicate it was made empty by |
| 39 | // that call (in which case the next PushTask() will return true to indicate to |
| 40 | // the caller that the Sequence should be re-enqueued for execution). |
| 41 | // |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 42 | // This class is thread-safe. |
| 43 | class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { |
| 44 | public: |
| 45 | Sequence(); |
| 46 | |
fdoray | 0c97aa3 | 2016-10-12 12:21:27 | [diff] [blame] | 47 | // Adds |task| in a new slot at the end of the Sequence. Returns true if the |
| 48 | // Sequence was empty before this operation. |
Robert Liao | d07daf9 | 2017-12-18 01:38:07 | [diff] [blame] | 49 | bool PushTask(Task task); |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 50 | |
fdoray | 0c97aa3 | 2016-10-12 12:21:27 | [diff] [blame] | 51 | // Transfers ownership of the Task in the front slot of the Sequence to the |
| 52 | // caller. The front slot of the Sequence will be nullptr and remain until |
| 53 | // Pop(). Cannot be called on an empty Sequence or a Sequence whose front slot |
| 54 | // is already nullptr. |
Robert Liao | d07daf9 | 2017-12-18 01:38:07 | [diff] [blame] | 55 | // |
| 56 | // Because this method cannot be called on an empty Sequence, the returned |
| 57 | // Optional<Task> is never nullptr. An Optional is used in preparation for the |
| 58 | // merge between TaskScheduler and TaskQueueManager (in Blink). |
| 59 | // https://crbug.com/783309 |
| 60 | Optional<Task> TakeTask(); |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 61 | |
fdoray | 0c97aa3 | 2016-10-12 12:21:27 | [diff] [blame] | 62 | // Returns the TaskTraits of the Task in front of the Sequence. Cannot be |
| 63 | // called on an empty Sequence or on a Sequence whose front slot is empty. |
| 64 | TaskTraits PeekTaskTraits() const; |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 65 | |
fdoray | 0c97aa3 | 2016-10-12 12:21:27 | [diff] [blame] | 66 | // Removes the front slot of the Sequence. The front slot must have been |
| 67 | // emptied by TakeTask() before this is called. Cannot be called on an empty |
| 68 | // Sequence. Returns true if the Sequence is empty after this operation. |
| 69 | bool Pop(); |
| 70 | |
| 71 | // Returns a SequenceSortKey representing the priority of the Sequence. Cannot |
| 72 | // be called on an empty Sequence. |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 73 | SequenceSortKey GetSortKey() const; |
| 74 | |
fdoray | 4bf182a | 2016-07-26 23:48:06 | [diff] [blame] | 75 | // Returns a token that uniquely identifies this Sequence. |
| 76 | const SequenceToken& token() const { return token_; } |
| 77 | |
Jeffrey He | c42cc008 | 2017-06-13 14:39:16 | [diff] [blame] | 78 | SequenceLocalStorageMap* sequence_local_storage() { |
| 79 | return &sequence_local_storage_; |
| 80 | } |
| 81 | |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 82 | private: |
| 83 | friend class RefCountedThreadSafe<Sequence>; |
| 84 | ~Sequence(); |
| 85 | |
fdoray | 4bf182a | 2016-07-26 23:48:06 | [diff] [blame] | 86 | const SequenceToken token_ = SequenceToken::Create(); |
| 87 | |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 88 | // Synchronizes access to all members. |
| 89 | mutable SchedulerLock lock_; |
| 90 | |
| 91 | // Queue of tasks to execute. |
Robert Liao | d07daf9 | 2017-12-18 01:38:07 | [diff] [blame] | 92 | base::queue<Task> queue_; |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 93 | |
fdoray | 0c97aa3 | 2016-10-12 12:21:27 | [diff] [blame] | 94 | // Number of tasks contained in the Sequence for each priority. |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 95 | size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = |
| 96 | {}; |
| 97 | |
Jeffrey He | c42cc008 | 2017-06-13 14:39:16 | [diff] [blame] | 98 | // Holds data stored through the SequenceLocalStorageSlot API. |
| 99 | SequenceLocalStorageMap sequence_local_storage_; |
| 100 | |
fdoray | c48d5f09 | 2016-03-17 01:57:42 | [diff] [blame] | 101 | DISALLOW_COPY_AND_ASSIGN(Sequence); |
| 102 | }; |
| 103 | |
| 104 | } // namespace internal |
| 105 | } // namespace base |
| 106 | |
| 107 | #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ |