1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #include "node_watchdog.h"
31 Watchdog::Watchdog(uint64_t ms) : destroyed_(false) {
33 loop_ = new uv_loop_t;
35 rc = uv_loop_init(loop_);
38 rc = uv_async_init(loop_, &async_, &Watchdog::Async);
41 rc = uv_timer_init(loop_, &timer_);
44 rc = uv_timer_start(&timer_, &Watchdog::Timer, ms, 0);
47 rc = uv_thread_create(&thread_, &Watchdog::Run, this);
52 Watchdog::~Watchdog() {
57 void Watchdog::Dispose() {
62 void Watchdog::Destroy() {
67 uv_async_send(&async_);
68 uv_thread_join(&thread_);
70 uv_close(reinterpret_cast<uv_handle_t*>(&async_), NULL);
72 // UV_RUN_DEFAULT so that libuv has a chance to clean up.
73 uv_run(loop_, UV_RUN_DEFAULT);
75 int rc = uv_loop_close(loop_);
84 void Watchdog::Run(void* arg) {
85 Watchdog* wd = static_cast<Watchdog*>(arg);
87 // UV_RUN_ONCE so async_ or timer_ wakeup exits uv_run() call.
88 uv_run(wd->loop_, UV_RUN_ONCE);
90 // Loop ref count reaches zero when both handles are closed.
91 // Close the timer handle on this side and let Destroy() close async_
92 uv_close(reinterpret_cast<uv_handle_t*>(&wd->timer_), NULL);
96 void Watchdog::Async(uv_async_t* async, int status) {
100 void Watchdog::Timer(uv_timer_t* timer, int status) {
101 V8::TerminateExecution();