blob: 2c428ee77e36d6d878529bb31872e1167d8a96cb [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 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_DEFAULT_PLATFORM_H_
6#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include <functional>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include <map>
10#include <queue>
11#include <vector>
12
13#include "include/v8-platform.h"
14#include "src/base/macros.h"
15#include "src/base/platform/mutex.h"
16#include "src/libplatform/task-queue.h"
17
18namespace v8 {
19namespace platform {
20
21class TaskQueue;
22class Thread;
23class WorkerThread;
24
25class DefaultPlatform : public Platform {
26 public:
27 DefaultPlatform();
28 virtual ~DefaultPlatform();
29
30 void SetThreadPoolSize(int thread_pool_size);
31
32 void EnsureInitialized();
33
34 bool PumpMessageLoop(v8::Isolate* isolate);
35
36 // v8::Platform implementation.
Ben Murdoch097c5b22016-05-18 11:27:45 +010037 size_t NumberOfAvailableBackgroundThreads() override;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 void CallOnBackgroundThread(Task* task,
39 ExpectedRuntime expected_runtime) override;
40 void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override;
41 void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
42 double delay_in_seconds) override;
43 void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) override;
44 bool IdleTasksEnabled(Isolate* isolate) override;
45 double MonotonicallyIncreasingTime() override;
46 const uint8_t* GetCategoryGroupEnabled(const char* name) override;
47 const char* GetCategoryGroupName(
48 const uint8_t* category_enabled_flag) override;
49 uint64_t AddTraceEvent(char phase, const uint8_t* category_enabled_flag,
50 const char* name, uint64_t id, uint64_t bind_id,
51 int32_t num_args, const char** arg_names,
52 const uint8_t* arg_types, const uint64_t* arg_values,
53 unsigned int flags) override;
54 void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
55 const char* name, uint64_t handle) override;
56
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057
58 private:
59 static const int kMaxThreadPoolSize;
60
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 Task* PopTaskInMainThreadQueue(v8::Isolate* isolate);
62 Task* PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate);
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 base::Mutex lock_;
65 bool initialized_;
66 int thread_pool_size_;
67 std::vector<WorkerThread*> thread_pool_;
68 TaskQueue queue_;
69 std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
70
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 typedef std::pair<double, Task*> DelayedEntry;
72 std::map<v8::Isolate*,
73 std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
74 std::greater<DelayedEntry> > >
75 main_thread_delayed_queue_;
76
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
78};
79
80
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081} // namespace platform
82} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083
84
85#endif // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_