11cb0ef41Sopenharmony_ci// Copyright 2019 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_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
61cb0ef41Sopenharmony_ci#define V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <map>
91cb0ef41Sopenharmony_ci#include <memory>
101cb0ef41Sopenharmony_ci#include <queue>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "include/libplatform/libplatform-export.h"
131cb0ef41Sopenharmony_ci#include "src/base/macros.h"
141cb0ef41Sopenharmony_ci#include "src/base/platform/condition-variable.h"
151cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace v8 {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciclass Task;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cinamespace platform {
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// DelayedTaskQueue provides queueing for immediate and delayed tasks. It does
241cb0ef41Sopenharmony_ci// not provide any guarantees about ordering of tasks, except that immediate
251cb0ef41Sopenharmony_ci// tasks will be run in the order that they are posted.
261cb0ef41Sopenharmony_ciclass V8_PLATFORM_EXPORT DelayedTaskQueue {
271cb0ef41Sopenharmony_ci public:
281cb0ef41Sopenharmony_ci  using TimeFunction = double (*)();
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  explicit DelayedTaskQueue(TimeFunction time_function);
311cb0ef41Sopenharmony_ci  ~DelayedTaskQueue();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  DelayedTaskQueue(const DelayedTaskQueue&) = delete;
341cb0ef41Sopenharmony_ci  DelayedTaskQueue& operator=(const DelayedTaskQueue&) = delete;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  double MonotonicallyIncreasingTime();
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  // Appends an immediate task to the queue. The queue takes ownership of
391cb0ef41Sopenharmony_ci  // |task|. Tasks appended via this method will be run in order. Thread-safe.
401cb0ef41Sopenharmony_ci  void Append(std::unique_ptr<Task> task);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  // Appends a delayed task to the queue. There is no ordering guarantee
431cb0ef41Sopenharmony_ci  // provided regarding delayed tasks, both with respect to other delayed tasks
441cb0ef41Sopenharmony_ci  // and non-delayed tasks that were appended using Append(). Thread-safe.
451cb0ef41Sopenharmony_ci  void AppendDelayed(std::unique_ptr<Task> task, double delay_in_seconds);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  // Returns the next task to process. Blocks if no task is available.
481cb0ef41Sopenharmony_ci  // Returns nullptr if the queue is terminated. Will return either an immediate
491cb0ef41Sopenharmony_ci  // task posted using Append() or a delayed task where the deadline has passed,
501cb0ef41Sopenharmony_ci  // according to the |time_function| provided in the constructor. Thread-safe.
511cb0ef41Sopenharmony_ci  std::unique_ptr<Task> GetNext();
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  // Terminate the queue.
541cb0ef41Sopenharmony_ci  void Terminate();
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci private:
571cb0ef41Sopenharmony_ci  std::unique_ptr<Task> PopTaskFromDelayedQueue(double now);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  base::ConditionVariable queues_condition_var_;
601cb0ef41Sopenharmony_ci  base::Mutex lock_;
611cb0ef41Sopenharmony_ci  std::queue<std::unique_ptr<Task>> task_queue_;
621cb0ef41Sopenharmony_ci  std::multimap<double, std::unique_ptr<Task>> delayed_task_queue_;
631cb0ef41Sopenharmony_ci  bool terminated_ = false;
641cb0ef41Sopenharmony_ci  TimeFunction time_function_;
651cb0ef41Sopenharmony_ci};
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci}  // namespace platform
681cb0ef41Sopenharmony_ci}  // namespace v8
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci#endif  // V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
71