11cb0ef41Sopenharmony_ci// Copyright 2015 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_UTILS_LOCKED_QUEUE_H_
61cb0ef41Sopenharmony_ci#define V8_UTILS_LOCKED_QUEUE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <atomic>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "src/base/platform/platform.h"
111cb0ef41Sopenharmony_ci#include "src/utils/allocation.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// Simple lock-based unbounded size queue (multi producer; multi consumer) based
171cb0ef41Sopenharmony_ci// on "Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue
181cb0ef41Sopenharmony_ci// Algorithms" by M. Scott and M. Michael.
191cb0ef41Sopenharmony_ci// See:
201cb0ef41Sopenharmony_ci// https://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html
211cb0ef41Sopenharmony_citemplate <typename Record>
221cb0ef41Sopenharmony_ciclass LockedQueue final {
231cb0ef41Sopenharmony_ci public:
241cb0ef41Sopenharmony_ci  inline LockedQueue();
251cb0ef41Sopenharmony_ci  LockedQueue(const LockedQueue&) = delete;
261cb0ef41Sopenharmony_ci  LockedQueue& operator=(const LockedQueue&) = delete;
271cb0ef41Sopenharmony_ci  inline ~LockedQueue();
281cb0ef41Sopenharmony_ci  inline void Enqueue(Record record);
291cb0ef41Sopenharmony_ci  inline bool Dequeue(Record* record);
301cb0ef41Sopenharmony_ci  inline bool IsEmpty() const;
311cb0ef41Sopenharmony_ci  inline bool Peek(Record* record) const;
321cb0ef41Sopenharmony_ci  inline size_t size() const;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci private:
351cb0ef41Sopenharmony_ci  struct Node;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  mutable base::Mutex head_mutex_;
381cb0ef41Sopenharmony_ci  base::Mutex tail_mutex_;
391cb0ef41Sopenharmony_ci  Node* head_;
401cb0ef41Sopenharmony_ci  Node* tail_;
411cb0ef41Sopenharmony_ci  std::atomic<size_t> size_;
421cb0ef41Sopenharmony_ci};
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci}  // namespace internal
451cb0ef41Sopenharmony_ci}  // namespace v8
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci#endif  // V8_UTILS_LOCKED_QUEUE_H_
48