// 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_H_ #define V8_LOCKED_QUEUE_INL_H_ #include "src/base/atomic-utils.h" #include "src/locked-queue.h" namespace v8 { namespace internal { template struct LockedQueue::Node : Malloced { Node() : next(nullptr) {} Record value; base::AtomicValue next; }; template inline LockedQueue::LockedQueue() { head_ = new Node(); CHECK_NOT_NULL(head_); tail_ = head_; } template inline LockedQueue::~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 inline void LockedQueue::Enqueue(const Record& record) { Node* n = new Node(); CHECK_NOT_NULL(n); n->value = record; { base::LockGuard guard(&tail_mutex_); tail_->next.SetValue(n); tail_ = n; } } template inline bool LockedQueue::Dequeue(Record* record) { Node* old_head = nullptr; { base::LockGuard 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 inline bool LockedQueue::IsEmpty() const { base::LockGuard guard(&head_mutex_); return head_->next.Value() == nullptr; } template inline bool LockedQueue::Peek(Record* record) const { base::LockGuard 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_H_