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 |
|
511 |
v8::Isolate* isolate() { return isolate_; } |
45 |
|
|
|
46 |
|
|
private: |
47 |
|
|
static void Run(void* arg); |
48 |
|
|
static void Timer(uv_timer_t* timer); |
49 |
|
|
|
50 |
|
|
v8::Isolate* isolate_; |
51 |
|
|
uv_thread_t thread_; |
52 |
|
|
uv_loop_t loop_; |
53 |
|
|
uv_async_t async_; |
54 |
|
|
uv_timer_t timer_; |
55 |
|
|
bool* timed_out_; |
56 |
|
|
}; |
57 |
|
|
|
58 |
|
|
class SigintWatchdog { |
59 |
|
|
public: |
60 |
|
|
explicit SigintWatchdog(v8::Isolate* isolate, |
61 |
|
|
bool* received_signal = nullptr); |
62 |
|
|
~SigintWatchdog(); |
63 |
|
|
v8::Isolate* isolate() { return isolate_; } |
64 |
|
|
void HandleSigint(); |
65 |
|
|
|
66 |
|
|
private: |
67 |
|
|
v8::Isolate* isolate_; |
68 |
|
|
bool* received_signal_; |
69 |
|
|
}; |
70 |
|
|
|
71 |
|
|
class SigintWatchdogHelper { |
72 |
|
|
public: |
73 |
|
567 |
static SigintWatchdogHelper* GetInstance() { return &instance; } |
74 |
|
|
void Register(SigintWatchdog* watchdog); |
75 |
|
|
void Unregister(SigintWatchdog* watchdog); |
76 |
|
|
bool HasPendingSignal(); |
77 |
|
|
|
78 |
|
|
int Start(); |
79 |
|
|
bool Stop(); |
80 |
|
|
|
81 |
|
|
private: |
82 |
|
|
SigintWatchdogHelper(); |
83 |
|
|
~SigintWatchdogHelper(); |
84 |
|
|
|
85 |
|
|
static bool InformWatchdogsAboutSignal(); |
86 |
|
|
static SigintWatchdogHelper instance; |
87 |
|
|
|
88 |
|
|
int start_stop_count_; |
89 |
|
|
|
90 |
|
|
Mutex mutex_; |
91 |
|
|
Mutex list_mutex_; |
92 |
|
|
std::vector<SigintWatchdog*> watchdogs_; |
93 |
|
|
bool has_pending_signal_; |
94 |
|
|
|
95 |
|
|
#ifdef __POSIX__ |
96 |
|
|
pthread_t thread_; |
97 |
|
|
uv_sem_t sem_; |
98 |
|
|
bool has_running_thread_; |
99 |
|
|
bool stopping_; |
100 |
|
|
|
101 |
|
|
static void* RunSigintWatchdog(void* arg); |
102 |
|
|
static void HandleSignal(int signum, siginfo_t* info, void* ucontext); |
103 |
|
|
#else |
104 |
|
|
bool watchdog_disabled_; |
105 |
|
|
static BOOL WINAPI WinCtrlCHandlerRoutine(DWORD dwCtrlType); |
106 |
|
|
#endif |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
} // namespace node |
110 |
|
|
|
111 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
112 |
|
|
|
113 |
|
|
#endif // SRC_NODE_WATCHDOG_H_ |