1 // Copyright 2020 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 INCLUDE_CPPGC_DEFAULT_PLATFORM_H_ 6 #define INCLUDE_CPPGC_DEFAULT_PLATFORM_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "cppgc/platform.h" 12 #include "libplatform/libplatform.h" 13 #include "v8config.h" // NOLINT(build/include_directory) 14 15 namespace cppgc { 16 17 /** 18 * Platform provided by cppgc. Uses V8's DefaultPlatform provided by 19 * libplatform internally. Exception: `GetForegroundTaskRunner()`, see below. 20 */ 21 class V8_EXPORT DefaultPlatform : public Platform { 22 public: 23 using IdleTaskSupport = v8::platform::IdleTaskSupport; 24 explicit DefaultPlatform( 25 int thread_pool_size = 0, 26 IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled) v8_platform_(v8::platform::NewDefaultPlatform (thread_pool_size,idle_task_support))27 : v8_platform_(v8::platform::NewDefaultPlatform(thread_pool_size, 28 idle_task_support)) {} 29 GetPageAllocator()30 cppgc::PageAllocator* GetPageAllocator() override { 31 return v8_platform_->GetPageAllocator(); 32 } 33 MonotonicallyIncreasingTime()34 double MonotonicallyIncreasingTime() override { 35 return v8_platform_->MonotonicallyIncreasingTime(); 36 } 37 GetForegroundTaskRunner()38 std::shared_ptr<cppgc::TaskRunner> GetForegroundTaskRunner() override { 39 // V8's default platform creates a new task runner when passed the 40 // `v8::Isolate` pointer the first time. For non-default platforms this will 41 // require getting the appropriate task runner. 42 return v8_platform_->GetForegroundTaskRunner(kNoIsolate); 43 } 44 PostJob(cppgc::TaskPriority priority,std::unique_ptr<cppgc::JobTask> job_task)45 std::unique_ptr<cppgc::JobHandle> PostJob( 46 cppgc::TaskPriority priority, 47 std::unique_ptr<cppgc::JobTask> job_task) override { 48 return v8_platform_->PostJob(priority, std::move(job_task)); 49 } 50 51 protected: 52 static constexpr v8::Isolate* kNoIsolate = nullptr; 53 54 std::unique_ptr<v8::Platform> v8_platform_; 55 }; 56 57 } // namespace cppgc 58 59 #endif // INCLUDE_CPPGC_DEFAULT_PLATFORM_H_ 60