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 <map> 9 #include <memory> 10 11 #include "include/libplatform/libplatform-export.h" 12 #include "include/libplatform/libplatform.h" 13 #include "include/libplatform/v8-tracing.h" 14 #include "include/v8-platform.h" 15 #include "src/base/compiler-specific.h" 16 #include "src/base/macros.h" 17 #include "src/base/platform/mutex.h" 18 #include "src/base/platform/time.h" 19 20 namespace v8 { 21 namespace platform { 22 23 class Thread; 24 class WorkerThread; 25 class DefaultForegroundTaskRunner; 26 class DefaultWorkerThreadsTaskRunner; 27 class DefaultPageAllocator; 28 NON_EXPORTED_BASE(Platform)29class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) { 30 public: 31 explicit DefaultPlatform( 32 int thread_pool_size = 0, 33 IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled, 34 std::unique_ptr<v8::TracingController> tracing_controller = {}); 35 36 ~DefaultPlatform() override; 37 38 void EnsureBackgroundTaskRunnerInitialized(); 39 40 bool PumpMessageLoop( 41 v8::Isolate* isolate, 42 MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait); 43 44 void RunIdleTasks(v8::Isolate* isolate, double idle_time_in_seconds); 45 46 void SetTracingController( 47 std::unique_ptr<v8::TracingController> tracing_controller); 48 49 using TimeFunction = double (*)(); 50 51 void SetTimeFunctionForTesting(TimeFunction time_function); 52 53 // v8::Platform implementation. 54 int NumberOfWorkerThreads() override; 55 std::shared_ptr<TaskRunner> GetForegroundTaskRunner( 56 v8::Isolate* isolate) override; 57 void CallOnWorkerThread(std::unique_ptr<Task> task) override; 58 void CallDelayedOnWorkerThread(std::unique_ptr<Task> task, 59 double delay_in_seconds) override; 60 bool IdleTasksEnabled(Isolate* isolate) override; 61 std::unique_ptr<JobHandle> PostJob( 62 TaskPriority priority, std::unique_ptr<JobTask> job_state) override; 63 double MonotonicallyIncreasingTime() override; 64 double CurrentClockTimeMillis() override; 65 v8::TracingController* GetTracingController() override; 66 StackTracePrinter GetStackTracePrinter() override; 67 v8::PageAllocator* GetPageAllocator() override; 68 69 void NotifyIsolateShutdown(Isolate* isolate); 70 71 private: 72 base::Mutex lock_; 73 const int thread_pool_size_; 74 IdleTaskSupport idle_task_support_; 75 std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runner_; 76 std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>> 77 foreground_task_runner_map_; 78 79 std::unique_ptr<TracingController> tracing_controller_; 80 std::unique_ptr<PageAllocator> page_allocator_; 81 82 TimeFunction time_function_for_testing_ = nullptr; 83 DISALLOW_COPY_AND_ASSIGN(DefaultPlatform); 84 }; 85 86 } // namespace platform 87 } // namespace v8 88 89 90 #endif // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_ 91