1f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke// Copyright 2010 the V8 project authors. All rights reserved.
2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// found in the LICENSE file.
4f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#ifndef V8_PROFILER_UNBOUND_QUEUE_INL_H_
6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#define V8_PROFILER_UNBOUND_QUEUE_INL_H_
7f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
8014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/profiler/unbound-queue.h"
9f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
10f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarkenamespace v8 {
11f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarkenamespace internal {
12f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
13f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarketemplate<typename Record>
14f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarkestruct UnboundQueue<Record>::Node: public Malloced {
15f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  explicit Node(const Record& value)
16f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke      : value(value), next(NULL) {
17f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  }
18f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
19f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  Record value;
20f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  Node* next;
21f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke};
22f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
23f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
24f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarketemplate<typename Record>
25f7060e27768c550ace7ec48ad8c093466db52dfaLeon ClarkeUnboundQueue<Record>::UnboundQueue() {
26f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  first_ = new Node(Record());
27b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  divider_ = last_ = reinterpret_cast<base::AtomicWord>(first_);
28f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke}
29f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
30f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
31f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarketemplate<typename Record>
32f7060e27768c550ace7ec48ad8c093466db52dfaLeon ClarkeUnboundQueue<Record>::~UnboundQueue() {
33f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  while (first_ != NULL) DeleteFirst();
34f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke}
35f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
36f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
37f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarketemplate<typename Record>
38f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarkevoid UnboundQueue<Record>::DeleteFirst() {
39f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  Node* tmp = first_;
40f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  first_ = tmp->next;
41f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  delete tmp;
42f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke}
43f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
44f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
45f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarketemplate<typename Record>
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool UnboundQueue<Record>::Dequeue(Record* rec) {
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (divider_ == base::Acquire_Load(&last_)) return false;
48f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  Node* next = reinterpret_cast<Node*>(divider_)->next;
49f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  *rec = next->value;
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  base::Release_Store(&divider_, reinterpret_cast<base::AtomicWord>(next));
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return true;
52f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke}
53f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
54f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
55f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarketemplate<typename Record>
56f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarkevoid UnboundQueue<Record>::Enqueue(const Record& rec) {
57f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  Node*& next = reinterpret_cast<Node*>(last_)->next;
58f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke  next = new Node(rec);
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  base::Release_Store(&last_, reinterpret_cast<base::AtomicWord>(next));
60b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  while (first_ != reinterpret_cast<Node*>(base::Acquire_Load(&divider_))) {
62b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    DeleteFirst();
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
64b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
66b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
67b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate<typename Record>
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool UnboundQueue<Record>::IsEmpty() const {
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return base::NoBarrier_Load(&divider_) == base::NoBarrier_Load(&last_);
70f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke}
71f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
727f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch
737f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdochtemplate<typename Record>
74b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochRecord* UnboundQueue<Record>::Peek() const {
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (divider_ == base::Acquire_Load(&last_)) return NULL;
767f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch  Node* next = reinterpret_cast<Node*>(divider_)->next;
777f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch  return &next->value;
787f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch}
797f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch
80014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace internal
81014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace v8
82f7060e27768c550ace7ec48ad8c093466db52dfaLeon Clarke
83014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#endif  // V8_PROFILER_UNBOUND_QUEUE_INL_H_
84