Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_watchdog.h
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef SRC_NODE_WATCHDOG_H_
23 #define SRC_NODE_WATCHDOG_H_
24 
25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26 
27 #include "v8.h"
28 #include "uv.h"
29 #include "node_mutex.h"
30 #include <vector>
31 
32 #ifdef __POSIX__
33 #include <pthread.h>
34 #endif
35 
36 namespace node {
37 
38 class Watchdog {
39  public:
40  explicit Watchdog(v8::Isolate* isolate,
41  uint64_t ms,
42  bool* timed_out = nullptr);
43  ~Watchdog();
44  v8::Isolate* isolate() { return isolate_; }
45 
46  private:
47  static void Run(void* arg);
48  static void Async(uv_async_t* async);
49  static void Timer(uv_timer_t* timer);
50 
51  v8::Isolate* isolate_;
52  uv_thread_t thread_;
53  uv_loop_t* loop_;
54  uv_async_t async_;
55  uv_timer_t timer_;
56  bool* timed_out_;
57 };
58 
59 class SigintWatchdog {
60  public:
61  explicit SigintWatchdog(v8::Isolate* isolate,
62  bool* received_signal = nullptr);
63  ~SigintWatchdog();
64  v8::Isolate* isolate() { return isolate_; }
65  void HandleSigint();
66 
67  private:
68  v8::Isolate* isolate_;
69  bool* received_signal_;
70 };
71 
72 class SigintWatchdogHelper {
73  public:
74  static SigintWatchdogHelper* GetInstance() { return &instance; }
75  void Register(SigintWatchdog* watchdog);
76  void Unregister(SigintWatchdog* watchdog);
77  bool HasPendingSignal();
78 
79  int Start();
80  bool Stop();
81 
82  private:
83  SigintWatchdogHelper();
84  ~SigintWatchdogHelper();
85 
86  static bool InformWatchdogsAboutSignal();
87  static SigintWatchdogHelper instance;
88 
89  int start_stop_count_;
90 
91  Mutex mutex_;
92  Mutex list_mutex_;
93  std::vector<SigintWatchdog*> watchdogs_;
94  bool has_pending_signal_;
95 
96 #ifdef __POSIX__
97  pthread_t thread_;
98  uv_sem_t sem_;
99  bool has_running_thread_;
100  bool stopping_;
101 
102  static void* RunSigintWatchdog(void* arg);
103  static void HandleSignal(int signum);
104 #else
105  bool watchdog_disabled_;
106  static BOOL WINAPI WinCtrlCHandlerRoutine(DWORD dwCtrlType);
107 #endif
108 };
109 
110 } // namespace node
111 
112 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
113 
114 #endif // SRC_NODE_WATCHDOG_H_
MutexBase< LibuvMutexTraits > Mutex
Definition: node_mutex.h:14
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536