• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
8 #include <functional>
9 #include <map>
10 #include <memory>
11 #include <queue>
12 #include <vector>
13 
14 #include "include/libplatform/libplatform-export.h"
15 #include "include/libplatform/v8-tracing.h"
16 #include "include/v8-platform.h"
17 #include "src/base/compiler-specific.h"
18 #include "src/base/macros.h"
19 #include "src/base/platform/mutex.h"
20 #include "src/libplatform/task-queue.h"
21 
22 namespace v8 {
23 namespace platform {
24 
25 class TaskQueue;
26 class Thread;
27 class WorkerThread;
28 
29 namespace tracing {
30 class TracingController;
31 }
32 
NON_EXPORTED_BASE(Platform)33 class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
34  public:
35   DefaultPlatform();
36   virtual ~DefaultPlatform();
37 
38   void SetThreadPoolSize(int thread_pool_size);
39 
40   void EnsureInitialized();
41 
42   bool PumpMessageLoop(v8::Isolate* isolate);
43 
44   void RunIdleTasks(v8::Isolate* isolate, double idle_time_in_seconds);
45 
46   // v8::Platform implementation.
47   size_t NumberOfAvailableBackgroundThreads() override;
48   void CallOnBackgroundThread(Task* task,
49                               ExpectedRuntime expected_runtime) override;
50   void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override;
51   void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
52                                      double delay_in_seconds) override;
53   void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) override;
54   bool IdleTasksEnabled(Isolate* isolate) override;
55   double MonotonicallyIncreasingTime() override;
56   const uint8_t* GetCategoryGroupEnabled(const char* name) override;
57   const char* GetCategoryGroupName(
58       const uint8_t* category_enabled_flag) override;
59   using Platform::AddTraceEvent;
60   uint64_t AddTraceEvent(
61       char phase, const uint8_t* category_enabled_flag, const char* name,
62       const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
63       const char** arg_names, const uint8_t* arg_types,
64       const uint64_t* arg_values,
65       std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
66       unsigned int flags) override;
67   void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
68                                 const char* name, uint64_t handle) override;
69   void SetTracingController(tracing::TracingController* tracing_controller);
70 
71   void AddTraceStateObserver(TraceStateObserver* observer) override;
72   void RemoveTraceStateObserver(TraceStateObserver* observer) override;
73 
74  private:
75   static const int kMaxThreadPoolSize;
76 
77   Task* PopTaskInMainThreadQueue(v8::Isolate* isolate);
78   Task* PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate);
79   IdleTask* PopTaskInMainThreadIdleQueue(v8::Isolate* isolate);
80 
81   base::Mutex lock_;
82   bool initialized_;
83   int thread_pool_size_;
84   std::vector<WorkerThread*> thread_pool_;
85   TaskQueue queue_;
86   std::map<v8::Isolate*, std::queue<Task*>> main_thread_queue_;
87   std::map<v8::Isolate*, std::queue<IdleTask*>> main_thread_idle_queue_;
88 
89   typedef std::pair<double, Task*> DelayedEntry;
90   std::map<v8::Isolate*,
91            std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
92                                std::greater<DelayedEntry> > >
93       main_thread_delayed_queue_;
94   std::unique_ptr<tracing::TracingController> tracing_controller_;
95 
96   DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
97 };
98 
99 
100 }  // namespace platform
101 }  // namespace v8
102 
103 
104 #endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
105