blob: ea39abc28bd4c3792ad9f1ff44d827689e8f06ff [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,
Ben Murdochda12d292016-06-02 14:46:10 +010050 const char* name, const char* scope, uint64_t id,
51 uint64_t bind_id, int32_t num_args,
52 const char** arg_names, const uint8_t* arg_types,
53 const uint64_t* arg_values,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 unsigned int flags) override;
55 void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
56 const char* name, uint64_t handle) override;
57
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058
59 private:
60 static const int kMaxThreadPoolSize;
61
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 Task* PopTaskInMainThreadQueue(v8::Isolate* isolate);
63 Task* PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate);
64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 base::Mutex lock_;
66 bool initialized_;
67 int thread_pool_size_;
68 std::vector<WorkerThread*> thread_pool_;
69 TaskQueue queue_;
70 std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
71
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 typedef std::pair<double, Task*> DelayedEntry;
73 std::map<v8::Isolate*,
74 std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
75 std::greater<DelayedEntry> > >
76 main_thread_delayed_queue_;
77
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
79};
80
81
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082} // namespace platform
83} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084
85
86#endif // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_