| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_STACK_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_STACK_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_STACK_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_STACK_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 namespace internal { | 16 namespace internal { |
| 17 | 17 |
| 18 class SchedulerWorker; | 18 class SchedulerWorker; |
| 19 | 19 |
| 20 // A stack of SchedulerWorkers. Supports removal of arbitrary | 20 // A stack of SchedulerWorkers. Supports removal of arbitrary SchedulerWorkers. |
| 21 // SchedulerWorkers. DCHECKs when a SchedulerWorker is inserted | 21 // DCHECKs when a SchedulerWorker is inserted multiple times. SchedulerWorkers |
| 22 // multiple times. SchedulerWorkers are not owned by the stack. Push() is | 22 // are not owned by the stack. Push() is amortized O(1). Pop(), Peek(), Size() |
| 23 // amortized O(1). Pop(), Size() and Empty() are O(1). Remove is O(n). This | 23 // and Empty() are O(1). Contains() and Remove() are O(n). |
| 24 // class is NOT thread-safe. | 24 // This class is NOT thread-safe. |
| 25 class BASE_EXPORT SchedulerWorkerStack { | 25 class BASE_EXPORT SchedulerWorkerStack { |
| 26 public: | 26 public: |
| 27 SchedulerWorkerStack(); | 27 SchedulerWorkerStack(); |
| 28 ~SchedulerWorkerStack(); | 28 ~SchedulerWorkerStack(); |
| 29 | 29 |
| 30 // Inserts |worker| at the top of the stack. |worker| must not already be on | 30 // Inserts |worker| at the top of the stack. |worker| must not already be on |
| 31 // the stack. | 31 // the stack. |
| 32 void Push(SchedulerWorker* worker); | 32 void Push(SchedulerWorker* worker); |
| 33 | 33 |
| 34 // Removes the top SchedulerWorker from the stack and returns it. | 34 // Removes the top SchedulerWorker from the stack and returns it. |
| 35 // Returns nullptr if the stack is empty. | 35 // Returns nullptr if the stack is empty. |
| 36 SchedulerWorker* Pop(); | 36 SchedulerWorker* Pop(); |
| 37 | 37 |
| 38 // Removes the top SchedulerWorker from the stack and returns it. |
| 39 // Returns nullptr if the stack is empty. |
| 40 SchedulerWorker* Peek() const; |
| 41 |
| 42 // Returns true if |worker| is already on the stack. |
| 43 bool Contains(const SchedulerWorker* worker) const; |
| 44 |
| 38 // Removes |worker| from the stack. | 45 // Removes |worker| from the stack. |
| 39 void Remove(const SchedulerWorker* worker); | 46 void Remove(const SchedulerWorker* worker); |
| 40 | 47 |
| 41 // Returns the number of SchedulerWorkers on the stack. | 48 // Returns the number of SchedulerWorkers on the stack. |
| 42 size_t Size() const { return stack_.size(); } | 49 size_t Size() const { return stack_.size(); } |
| 43 | 50 |
| 44 // Returns true if the stack is empty. | 51 // Returns true if the stack is empty. |
| 45 bool IsEmpty() const { return stack_.empty(); } | 52 bool IsEmpty() const { return stack_.empty(); } |
| 46 | 53 |
| 47 private: | 54 private: |
| 48 std::vector<SchedulerWorker*> stack_; | 55 std::vector<SchedulerWorker*> stack_; |
| 49 | 56 |
| 50 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerStack); | 57 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerStack); |
| 51 }; | 58 }; |
| 52 | 59 |
| 53 } // namespace internal | 60 } // namespace internal |
| 54 } // namespace base | 61 } // namespace base |
| 55 | 62 |
| 56 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_STACK_H_ | 63 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_STACK_H_ |
| OLD | NEW |