OLD | NEW |
---|---|
(Empty) | |
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 #include "base/task_scheduler/scheduler_stack.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace base { | |
12 namespace internal { | |
13 | |
14 namespace { | |
15 | |
16 constexpr uint64_t kBit = 1U; | |
17 | |
18 // Returns true if the |val| bit of |bit_field| is set. | |
19 bool BitIsSet(uint64_t bit_field, int val) { | |
20 DCHECK_GE(val, 0); | |
21 DCHECK_LE(val, 63); | |
22 return (bit_field & (kBit << val)) != 0; | |
23 } | |
24 | |
25 struct BitIsSetPredicate { | |
26 BitIsSetPredicate(uint64_t bit_field) : bit_field_(bit_field) {} | |
27 | |
28 bool operator()(int val) const { return BitIsSet(bit_field_, val); } | |
29 | |
30 const uint64_t bit_field_; | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 SchedulerStack::SchedulerStack() = default; | |
36 | |
37 SchedulerStack::~SchedulerStack() = default; | |
38 | |
39 void SchedulerStack::Push(int val) { | |
40 DCHECK_GE(val, 0); | |
41 DCHECK_LE(val, 63); | |
42 DCHECK(!BitIsSet(bit_field_, val)); | |
robliao
2016/04/19 00:41:16
Let's add a message: DCHECK(..) << "Value already
fdoray
2016/04/19 14:05:50
Done.
| |
43 | |
44 DCHECK_LE(stack_.size(), 64U); | |
45 if (stack_.size() >= 64U) { | |
46 // Remove from |stack_| all values whose bit isn't set in |bit_field_|. This | |
47 // prevents |stack_| from growing indefinitely. | |
48 stack_.erase(std::remove_if(stack_.begin(), stack_.end(), | |
49 BitIsSetPredicate(bit_field_)), | |
robliao
2016/04/19 00:41:16
Instead of predicate, this may be clearer as a lam
fdoray
2016/04/19 14:05:50
Done.
| |
50 stack_.end()); | |
51 } | |
52 | |
53 stack_.push_back(val); | |
54 bit_field_ |= kBit << val; | |
55 ++size_; | |
56 } | |
57 | |
58 int SchedulerStack::Pop() { | |
59 DCHECK(!Empty()); | |
60 | |
61 // Pop values from |stack_| until one whose bit is set in |bit_field_| is | |
62 // found. | |
63 int val = -1; | |
64 do { | |
65 DCHECK(!stack_.empty()); | |
66 val = stack_.back(); | |
67 stack_.pop_back(); | |
68 } while (!BitIsSet(bit_field_, val)); | |
69 | |
70 bit_field_ &= ~(kBit << val); | |
71 --size_; | |
72 return val; | |
73 } | |
74 | |
75 void SchedulerStack::Remove(int val) { | |
76 if (!BitIsSet(bit_field_, val)) | |
77 return; | |
78 bit_field_ &= ~(kBit << val); | |
79 --size_; | |
robliao
2016/04/19 00:41:16
It's worthwhile to note somewhere that an index on
fdoray
2016/04/19 14:05:50
Done, in scheduler_stack.h above |stack_|.
| |
80 } | |
81 | |
82 } // namespace internal | |
83 } // namespace base | |
OLD | NEW |