94ef9c5055de333a85bc0245d46d8779c324f218
[platform/upstream/v8.git] / src / libplatform / default-platform.h
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 <queue>
11 #include <vector>
12
13 #include "include/v8-platform.h"
14 #include "src/base/macros.h"
15 #include "src/base/platform/mutex.h"
16 #include "src/libplatform/task-queue.h"
17
18 namespace v8 {
19 namespace platform {
20
21 class TaskQueue;
22 class Thread;
23 class WorkerThread;
24
25 class DefaultPlatform : public Platform {
26  public:
27   DefaultPlatform();
28   virtual ~DefaultPlatform();
29
30   void SetThreadPoolSize(int thread_pool_size);
31
32   void EnsureInitialized();
33
34   bool PumpMessageLoop(v8::Isolate* isolate);
35
36   // v8::Platform implementation.
37   virtual void CallOnBackgroundThread(
38       Task* task, ExpectedRuntime expected_runtime) override;
39   virtual void CallOnForegroundThread(v8::Isolate* isolate,
40                                       Task* task) override;
41   virtual void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
42                                              double delay_in_seconds) override;
43   virtual void CallIdleOnForegroundThread(Isolate* isolate,
44                                           IdleTask* task) override;
45   virtual bool IdleTasksEnabled(Isolate* isolate) override;
46   double MonotonicallyIncreasingTime() override;
47
48  private:
49   static const int kMaxThreadPoolSize;
50
51   Task* PopTaskInMainThreadQueue(v8::Isolate* isolate);
52   Task* PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate);
53
54   base::Mutex lock_;
55   bool initialized_;
56   int thread_pool_size_;
57   std::vector<WorkerThread*> thread_pool_;
58   TaskQueue queue_;
59   std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
60
61   typedef std::pair<double, Task*> DelayedEntry;
62   std::map<v8::Isolate*,
63            std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
64                                std::greater<DelayedEntry> > >
65       main_thread_delayed_queue_;
66
67   DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
68 };
69
70
71 } }  // namespace v8::platform
72
73
74 #endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_