Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4035)

Unified Diff: base/task_scheduler/scheduler_stack.cc

Issue 1892033003: TaskScheduler [10] SchedulerWorkerThreadStack (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@9_single_thread
Patch Set: self review Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/task_scheduler/scheduler_stack.h ('k') | base/task_scheduler/scheduler_stack_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/task_scheduler/scheduler_stack.cc
diff --git a/base/task_scheduler/scheduler_stack.cc b/base/task_scheduler/scheduler_stack.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0cb703bf88349d2446ec2e6e5f5455c4c9658275
--- /dev/null
+++ b/base/task_scheduler/scheduler_stack.cc
@@ -0,0 +1,83 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/task_scheduler/scheduler_stack.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+
+namespace base {
+namespace internal {
+
+namespace {
+
+constexpr uint64_t kBit = 1U;
+
+// Returns true if the |val| bit of |bit_field| is set.
+bool BitIsSet(uint64_t bit_field, int val) {
+ DCHECK_GE(val, 0);
+ DCHECK_LE(val, 63);
+ return (bit_field & (kBit << val)) != 0;
+}
+
+struct BitIsSetPredicate {
+ BitIsSetPredicate(uint64_t bit_field) : bit_field_(bit_field) {}
+
+ bool operator()(int val) const { return BitIsSet(bit_field_, val); }
+
+ const uint64_t bit_field_;
+};
+
+} // namespace
+
+SchedulerStack::SchedulerStack() = default;
+
+SchedulerStack::~SchedulerStack() = default;
+
+void SchedulerStack::Push(int val) {
+ DCHECK_GE(val, 0);
+ DCHECK_LE(val, 63);
+ 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.
+
+ DCHECK_LE(stack_.size(), 64U);
+ if (stack_.size() >= 64U) {
+ // Remove from |stack_| all values whose bit isn't set in |bit_field_|. This
+ // prevents |stack_| from growing indefinitely.
+ stack_.erase(std::remove_if(stack_.begin(), stack_.end(),
+ 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.
+ stack_.end());
+ }
+
+ stack_.push_back(val);
+ bit_field_ |= kBit << val;
+ ++size_;
+}
+
+int SchedulerStack::Pop() {
+ DCHECK(!Empty());
+
+ // Pop values from |stack_| until one whose bit is set in |bit_field_| is
+ // found.
+ int val = -1;
+ do {
+ DCHECK(!stack_.empty());
+ val = stack_.back();
+ stack_.pop_back();
+ } while (!BitIsSet(bit_field_, val));
+
+ bit_field_ &= ~(kBit << val);
+ --size_;
+ return val;
+}
+
+void SchedulerStack::Remove(int val) {
+ if (!BitIsSet(bit_field_, val))
+ return;
+ bit_field_ &= ~(kBit << val);
+ --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_|.
+}
+
+} // namespace internal
+} // namespace base
« no previous file with comments | « base/task_scheduler/scheduler_stack.h ('k') | base/task_scheduler/scheduler_stack_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698