Add V8 platform API to call delayed task.
[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   double MonotonicallyIncreasingTime() override;
44
45  private:
46   static const int kMaxThreadPoolSize;
47
48   Task* PopTaskInMainThreadQueue(v8::Isolate* isolate);
49   Task* PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate);
50
51   base::Mutex lock_;
52   bool initialized_;
53   int thread_pool_size_;
54   std::vector<WorkerThread*> thread_pool_;
55   TaskQueue queue_;
56   std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
57
58   typedef std::pair<double, Task*> DelayedEntry;
59   std::map<v8::Isolate*,
60            std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
61                                std::greater<DelayedEntry> > >
62       main_thread_delayed_queue_;
63
64   DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
65 };
66
67
68 } }  // namespace v8::platform
69
70
71 #endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_