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