v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
delayed-task-queue.h
Go to the documentation of this file.
1// Copyright 2019 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_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
6#define V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
7
8#include <map>
9#include <memory>
10#include <queue>
11
15
16namespace v8 {
17
18class Task;
19
20namespace platform {
21
22// DelayedTaskQueue provides queueing for immediate and delayed tasks. It does
23// not provide any guarantees about ordering of tasks, except that immediate
24// tasks will be run in the order that they are posted.
25//
26// This class is not thread-safe, and should be guarded by a lock.
28 public:
29 using TimeFunction = double (*)();
30
31 explicit DelayedTaskQueue(TimeFunction time_function);
33
36
37 double MonotonicallyIncreasingTime();
38
39 // Appends an immediate task to the queue. The queue takes ownership of
40 // |task|. Tasks appended via this method will be run in order.
41 void Append(std::unique_ptr<Task> task);
42
43 // Appends a delayed task to the queue. There is no ordering guarantee
44 // provided regarding delayed tasks, both with respect to other delayed tasks
45 // and non-delayed tasks that were appended using Append().
46 void AppendDelayed(std::unique_ptr<Task> task, double delay_in_seconds);
47
49 enum { kTask, kWaitIndefinite, kWaitDelayed, kTerminated } state;
50 std::unique_ptr<Task> task;
52 };
53 // Returns the next task to process, or the amount of time to wait until the
54 // next delayed task. Returns nullptr if the queue is terminated. Will return
55 // either an immediate task posted using Append() or a delayed task where the
56 // deadline has passed, according to the |time_function| provided in the
57 // constructor.
58 MaybeNextTask TryGetNext();
59
60 // Terminate the queue.
61 void Terminate();
62
63 private:
64 std::unique_ptr<Task> PopTaskFromDelayedQueue(double now);
65
66 std::queue<std::unique_ptr<Task>> task_queue_;
67 std::multimap<double, std::unique_ptr<Task>> delayed_task_queue_;
68 bool terminated_ = false;
70};
71
72} // namespace platform
73} // namespace v8
74
75#endif // V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
DelayedTaskQueue & operator=(const DelayedTaskQueue &)=delete
DelayedTaskQueue(const DelayedTaskQueue &)=delete
std::queue< std::unique_ptr< Task > > task_queue_
std::multimap< double, std::unique_ptr< Task > > delayed_task_queue_
#define V8_PLATFORM_EXPORT
LiftoffAssembler::CacheState state
v8::Task Task
Definition platform.h:23