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

Unified Diff: src/locked-queue-inl.h

Issue 1448283004: Add lock-based unbounded queue (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Moved to atomic handling for Node.next Created 5 years, 1 month 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 | « src/locked-queue.h ('k') | test/unittests/locked-queue-unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/locked-queue-inl.h
diff --git a/src/locked-queue-inl.h b/src/locked-queue-inl.h
new file mode 100644
index 0000000000000000000000000000000000000000..8b3e9d02bb7ba00267032211b72dc1a8ad85545b
--- /dev/null
+++ b/src/locked-queue-inl.h
@@ -0,0 +1,91 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef V8_LOCKED_QUEUE_INL_
+#define V8_LOCKED_QUEUE_INL_
+
+#include "src/atomic-utils.h"
+#include "src/locked-queue.h"
+
+namespace v8 {
+namespace internal {
+
+template <typename Record>
+struct LockedQueue<Record>::Node : Malloced {
+ Node() : next(nullptr) {}
+ Record value;
+ AtomicValue<Node*> next;
+};
+
+
+template <typename Record>
+inline LockedQueue<Record>::LockedQueue() {
+ head_ = new Node();
+ CHECK(head_ != nullptr);
+ tail_ = head_;
+}
+
+
+template <typename Record>
+inline LockedQueue<Record>::~LockedQueue() {
+ // Destroy all remaining nodes. Note that we do not destroy the actual values.
+ Node* old_node = nullptr;
+ Node* cur_node = head_;
+ while (cur_node != nullptr) {
+ old_node = cur_node;
+ cur_node = cur_node->next.Value();
+ delete old_node;
+ }
+}
+
+
+template <typename Record>
+inline void LockedQueue<Record>::Enqueue(const Record& record) {
+ Node* n = new Node();
+ CHECK(n != nullptr);
+ n->value = record;
+ {
+ base::LockGuard<base::Mutex> guard(&tail_mutex_);
+ tail_->next.SetValue(n);
+ tail_ = n;
+ }
+}
+
+
+template <typename Record>
+inline bool LockedQueue<Record>::Dequeue(Record* record) {
+ Node* old_head = nullptr;
+ {
+ base::LockGuard<base::Mutex> guard(&head_mutex_);
+ old_head = head_;
+ Node* const next_node = head_->next.Value();
+ if (next_node == nullptr) return false;
+ *record = next_node->value;
+ head_ = next_node;
+ }
+ delete old_head;
+ return true;
+}
+
+
+template <typename Record>
+inline bool LockedQueue<Record>::IsEmpty() const {
+ base::LockGuard<base::Mutex> guard(&head_mutex_);
+ return head_->next.Value() == nullptr;
+}
+
+
+template <typename Record>
+inline bool LockedQueue<Record>::Peek(Record* record) const {
+ base::LockGuard<base::Mutex> guard(&head_mutex_);
+ Node* const next_node = head_->next.Value();
+ if (next_node == nullptr) return false;
+ *record = next_node->value;
+ return true;
+}
+
+} // namespace internal
+} // namespace v8
+
+#endif // V8_LOCKED_QUEUE_INL_
« no previous file with comments | « src/locked-queue.h ('k') | test/unittests/locked-queue-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698