1// Copyright 2010 the V8 project 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#ifndef V8_UNBOUND_QUEUE_INL_H_
6#define V8_UNBOUND_QUEUE_INL_H_
7
8#include "src/unbound-queue.h"
9
10namespace v8 {
11namespace internal {
12
13template<typename Record>
14struct UnboundQueue<Record>::Node: public Malloced {
15  explicit Node(const Record& value)
16      : value(value), next(NULL) {
17  }
18
19  Record value;
20  Node* next;
21};
22
23
24template<typename Record>
25UnboundQueue<Record>::UnboundQueue() {
26  first_ = new Node(Record());
27  divider_ = last_ = reinterpret_cast<base::AtomicWord>(first_);
28}
29
30
31template<typename Record>
32UnboundQueue<Record>::~UnboundQueue() {
33  while (first_ != NULL) DeleteFirst();
34}
35
36
37template<typename Record>
38void UnboundQueue<Record>::DeleteFirst() {
39  Node* tmp = first_;
40  first_ = tmp->next;
41  delete tmp;
42}
43
44
45template<typename Record>
46bool UnboundQueue<Record>::Dequeue(Record* rec) {
47  if (divider_ == base::Acquire_Load(&last_)) return false;
48  Node* next = reinterpret_cast<Node*>(divider_)->next;
49  *rec = next->value;
50  base::Release_Store(&divider_, reinterpret_cast<base::AtomicWord>(next));
51  return true;
52}
53
54
55template<typename Record>
56void UnboundQueue<Record>::Enqueue(const Record& rec) {
57  Node*& next = reinterpret_cast<Node*>(last_)->next;
58  next = new Node(rec);
59  base::Release_Store(&last_, reinterpret_cast<base::AtomicWord>(next));
60
61  while (first_ != reinterpret_cast<Node*>(base::Acquire_Load(&divider_))) {
62    DeleteFirst();
63  }
64}
65
66
67template<typename Record>
68bool UnboundQueue<Record>::IsEmpty() const {
69  return base::NoBarrier_Load(&divider_) == base::NoBarrier_Load(&last_);
70}
71
72
73template<typename Record>
74Record* UnboundQueue<Record>::Peek() const {
75  if (divider_ == base::Acquire_Load(&last_)) return NULL;
76  Node* next = reinterpret_cast<Node*>(divider_)->next;
77  return &next->value;
78}
79
80} }  // namespace v8::internal
81
82#endif  // V8_UNBOUND_QUEUE_INL_H_
83