James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium 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 | #include "gin/public/v8_platform.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/location.h" |
James Robinson | 74f86ec | 2015-03-05 13:07:08 -0800 | [diff] [blame] | 9 | #include "base/threading/thread.h" |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 10 | #include "gin/per_isolate_data.h" |
| 11 | |
| 12 | namespace gin { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | // static |
| 21 | V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } |
| 22 | |
| 23 | V8Platform::V8Platform() {} |
| 24 | |
| 25 | V8Platform::~V8Platform() {} |
| 26 | |
| 27 | void V8Platform::CallOnBackgroundThread( |
| 28 | v8::Task* task, |
| 29 | v8::Platform::ExpectedRuntime expected_runtime) { |
James Robinson | 74f86ec | 2015-03-05 13:07:08 -0800 | [diff] [blame] | 30 | if (!background_thread_) { |
| 31 | background_thread_.reset(new base::Thread("gin_background")); |
| 32 | background_thread_->Start(); |
| 33 | } |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 34 | background_thread_->task_runner()->PostTask( |
| 35 | FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task))); |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 39 | PerIsolateData::From(isolate)->task_runner()->PostTask( |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 40 | FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task))); |
| 41 | } |
| 42 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 43 | double V8Platform::MonotonicallyIncreasingTime() { |
| 44 | return base::TimeTicks::Now().ToInternalValue() / |
| 45 | static_cast<double>(base::Time::kMicrosecondsPerSecond); |
| 46 | } |
| 47 | |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 48 | } // namespace gin |