2010-05-22 05:27:19 +00:00
|
|
|
// Copyright 2010 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-05-22 05:27:19 +00:00
|
|
|
|
|
|
|
#ifndef V8_UNBOUND_QUEUE_INL_H_
|
|
|
|
#define V8_UNBOUND_QUEUE_INL_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/unbound-queue.h"
|
2010-05-22 05:27:19 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
template<typename Record>
|
|
|
|
struct UnboundQueue<Record>::Node: public Malloced {
|
|
|
|
explicit Node(const Record& value)
|
|
|
|
: value(value), next(NULL) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Record value;
|
|
|
|
Node* next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Record>
|
|
|
|
UnboundQueue<Record>::UnboundQueue() {
|
|
|
|
first_ = new Node(Record());
|
2014-06-05 12:14:47 +00:00
|
|
|
divider_ = last_ = reinterpret_cast<base::AtomicWord>(first_);
|
2010-05-22 05:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Record>
|
|
|
|
UnboundQueue<Record>::~UnboundQueue() {
|
|
|
|
while (first_ != NULL) DeleteFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Record>
|
|
|
|
void UnboundQueue<Record>::DeleteFirst() {
|
|
|
|
Node* tmp = first_;
|
|
|
|
first_ = tmp->next;
|
|
|
|
delete tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Record>
|
2013-06-20 06:23:34 +00:00
|
|
|
bool UnboundQueue<Record>::Dequeue(Record* rec) {
|
2014-06-05 12:14:47 +00:00
|
|
|
if (divider_ == base::Acquire_Load(&last_)) return false;
|
2010-05-22 05:27:19 +00:00
|
|
|
Node* next = reinterpret_cast<Node*>(divider_)->next;
|
|
|
|
*rec = next->value;
|
2014-06-05 12:14:47 +00:00
|
|
|
base::Release_Store(÷r_, reinterpret_cast<base::AtomicWord>(next));
|
2013-06-20 06:23:34 +00:00
|
|
|
return true;
|
2010-05-22 05:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Record>
|
|
|
|
void UnboundQueue<Record>::Enqueue(const Record& rec) {
|
|
|
|
Node*& next = reinterpret_cast<Node*>(last_)->next;
|
|
|
|
next = new Node(rec);
|
2014-06-05 12:14:47 +00:00
|
|
|
base::Release_Store(&last_, reinterpret_cast<base::AtomicWord>(next));
|
2013-06-20 06:23:34 +00:00
|
|
|
|
2014-06-05 12:14:47 +00:00
|
|
|
while (first_ != reinterpret_cast<Node*>(base::Acquire_Load(÷r_))) {
|
2013-06-20 06:23:34 +00:00
|
|
|
DeleteFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Record>
|
|
|
|
bool UnboundQueue<Record>::IsEmpty() const {
|
2014-06-05 12:14:47 +00:00
|
|
|
return base::NoBarrier_Load(÷r_) == base::NoBarrier_Load(&last_);
|
2010-05-22 05:27:19 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 13:52:49 +00:00
|
|
|
|
|
|
|
template<typename Record>
|
2013-06-20 06:23:34 +00:00
|
|
|
Record* UnboundQueue<Record>::Peek() const {
|
2014-06-05 12:14:47 +00:00
|
|
|
if (divider_ == base::Acquire_Load(&last_)) return NULL;
|
2010-06-01 13:52:49 +00:00
|
|
|
Node* next = reinterpret_cast<Node*>(divider_)->next;
|
|
|
|
return &next->value;
|
|
|
|
}
|
|
|
|
|
2010-05-22 05:27:19 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_UNBOUND_QUEUE_INL_H_
|