V8 Project
default-platform.cc
Go to the documentation of this file.
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 
6 
7 #include <algorithm>
8 #include <queue>
9 
10 #include "src/base/logging.h"
12 #include "src/base/sys-info.h"
14 
15 namespace v8 {
16 namespace platform {
17 
18 
19 v8::Platform* CreateDefaultPlatform(int thread_pool_size) {
20  DefaultPlatform* platform = new DefaultPlatform();
21  platform->SetThreadPoolSize(thread_pool_size);
22  platform->EnsureInitialized();
23  return platform;
24 }
25 
26 
27 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) {
28  return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate);
29 }
30 
31 
33 
34 
36  : initialized_(false), thread_pool_size_(0) {}
37 
38 
40  base::LockGuard<base::Mutex> guard(&lock_);
41  queue_.Terminate();
42  if (initialized_) {
43  for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin();
44  i != thread_pool_.end(); ++i) {
45  delete *i;
46  }
47  }
48  for (std::map<v8::Isolate*, std::queue<Task*> >::iterator i =
49  main_thread_queue_.begin();
50  i != main_thread_queue_.end(); ++i) {
51  while (!i->second.empty()) {
52  delete i->second.front();
53  i->second.pop();
54  }
55  }
56 }
57 
58 
59 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
60  base::LockGuard<base::Mutex> guard(&lock_);
61  DCHECK(thread_pool_size >= 0);
62  if (thread_pool_size < 1) {
63  thread_pool_size = base::SysInfo::NumberOfProcessors();
64  }
66  std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
67 }
68 
69 
71  base::LockGuard<base::Mutex> guard(&lock_);
72  if (initialized_) return;
73  initialized_ = true;
74 
75  for (int i = 0; i < thread_pool_size_; ++i)
76  thread_pool_.push_back(new WorkerThread(&queue_));
77 }
78 
79 
81  Task* task = NULL;
82  {
83  base::LockGuard<base::Mutex> guard(&lock_);
84  std::map<v8::Isolate*, std::queue<Task*> >::iterator it =
85  main_thread_queue_.find(isolate);
86  if (it == main_thread_queue_.end() || it->second.empty()) {
87  return false;
88  }
89  task = it->second.front();
90  it->second.pop();
91  }
92  task->Run();
93  delete task;
94  return true;
95 }
96 
98  ExpectedRuntime expected_runtime) {
100  queue_.Append(task);
101 }
102 
103 
105  base::LockGuard<base::Mutex> guard(&lock_);
106  main_thread_queue_[isolate].push(task);
107 }
108 
109 } } // namespace v8::platform
Isolate represents an isolated instance of the V8 engine.
Definition: v8.h:4356
V8 Platform abstraction layer.
Definition: v8-platform.h:28
ExpectedRuntime
This enum is used to indicate whether a task is potentially long running, or causes a long wait.
Definition: v8-platform.h:35
A Task represents a unit of work.
Definition: v8-platform.h:15
virtual void Run()=0
std::vector< WorkerThread * > thread_pool_
std::map< v8::Isolate *, std::queue< Task * > > main_thread_queue_
virtual void CallOnBackgroundThread(Task *task, ExpectedRuntime expected_runtime) OVERRIDE
Schedules a task to be invoked on a background thread.
void SetThreadPoolSize(int thread_pool_size)
bool PumpMessageLoop(v8::Isolate *isolate)
virtual void CallOnForegroundThread(v8::Isolate *isolate, Task *task) OVERRIDE
Schedules a task to be invoked on a foreground thread wrt a specific |isolate|.
static const int kMaxThreadPoolSize
void Append(Task *task)
Definition: task-queue.cc:22
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf map
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
static int min(int a, int b)
Definition: liveedit.cc:273
bool PumpMessageLoop(v8::Platform *platform, v8::Isolate *isolate)
Pumps the message loop for the given isolate.
v8::Platform * CreateDefaultPlatform(int thread_pool_size)
Returns a new instance of the default v8::Platform implementation.
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20