Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_platform.h
Go to the documentation of this file.
1 #ifndef SRC_NODE_PLATFORM_H_
2 #define SRC_NODE_PLATFORM_H_
3 
4 #include <queue>
5 #include <vector>
6 
7 #include "libplatform/libplatform.h"
8 #include "node_mutex.h"
9 #include "uv.h"
10 
11 namespace node {
12 
13 template <class T>
14 class TaskQueue {
15  public:
16  TaskQueue();
18 
19  void Push(T* task);
20  T* Pop();
21  T* BlockingPop();
22  void NotifyOfCompletion();
23  void BlockingDrain();
24  void Stop();
25 
26  private:
27  Mutex lock_;
28  ConditionVariable tasks_available_;
29  ConditionVariable tasks_drained_;
30  int outstanding_tasks_;
31  bool stopped_;
32  std::queue<T*> task_queue_;
33 };
34 
35 class NodePlatform : public v8::Platform {
36  public:
37  NodePlatform(int thread_pool_size, uv_loop_t* loop,
38  v8::TracingController* tracing_controller);
39  virtual ~NodePlatform() {}
40 
41  void DrainBackgroundTasks();
42  void FlushForegroundTasksInternal();
43  void Shutdown();
44 
45  // v8::Platform implementation.
46  size_t NumberOfAvailableBackgroundThreads() override;
47  void CallOnBackgroundThread(v8::Task* task,
48  ExpectedRuntime expected_runtime) override;
49  void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override;
50  void CallDelayedOnForegroundThread(v8::Isolate* isolate, v8::Task* task,
51  double delay_in_seconds) override;
52  bool IdleTasksEnabled(v8::Isolate* isolate) override;
53  double MonotonicallyIncreasingTime() override;
54  v8::TracingController* GetTracingController() override;
55 
56  private:
57  uv_loop_t* const loop_;
58  uv_async_t flush_tasks_;
59  TaskQueue<v8::Task> foreground_tasks_;
60  TaskQueue<std::pair<v8::Task*, double>> foreground_delayed_tasks_;
61  TaskQueue<v8::Task> background_tasks_;
62  std::vector<std::unique_ptr<uv_thread_t>> threads_;
63 
64  std::unique_ptr<v8::TracingController> tracing_controller_;
65 };
66 
67 } // namespace node
68 
69 #endif // SRC_NODE_PLATFORM_H_
void Push(T *task)
void NotifyOfCompletion()
virtual ~NodePlatform()
Definition: node_platform.h:39