1// Copyright 2015 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_UTILS_LOCKED_QUEUE_H_
6#define V8_UTILS_LOCKED_QUEUE_H_
7
8#include <atomic>
9
10#include "src/base/platform/platform.h"
11#include "src/utils/allocation.h"
12
13namespace v8 {
14namespace internal {
15
16// Simple lock-based unbounded size queue (multi producer; multi consumer) based
17// on "Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue
18// Algorithms" by M. Scott and M. Michael.
19// See:
20// https://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html
21template <typename Record>
22class LockedQueue final {
23 public:
24  inline LockedQueue();
25  LockedQueue(const LockedQueue&) = delete;
26  LockedQueue& operator=(const LockedQueue&) = delete;
27  inline ~LockedQueue();
28  inline void Enqueue(Record record);
29  inline bool Dequeue(Record* record);
30  inline bool IsEmpty() const;
31  inline bool Peek(Record* record) const;
32  inline size_t size() const;
33
34 private:
35  struct Node;
36
37  mutable base::Mutex head_mutex_;
38  base::Mutex tail_mutex_;
39  Node* head_;
40  Node* tail_;
41  std::atomic<size_t> size_;
42};
43
44}  // namespace internal
45}  // namespace v8
46
47#endif  // V8_UTILS_LOCKED_QUEUE_H_
48