mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.2 KiB
81 lines
2.2 KiB
15 years ago
|
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
|
||
15 years ago
|
#include <node_signal_watcher.h>
|
||
15 years ago
|
#include <assert.h>
|
||
|
|
||
|
namespace node {
|
||
|
|
||
|
using namespace v8;
|
||
|
|
||
15 years ago
|
Persistent<FunctionTemplate> SignalWatcher::constructor_template;
|
||
15 years ago
|
static Persistent<String> signal_symbol;
|
||
15 years ago
|
|
||
15 years ago
|
void SignalWatcher::Initialize(Handle<Object> target) {
|
||
15 years ago
|
HandleScope scope;
|
||
|
|
||
15 years ago
|
Local<FunctionTemplate> t = FunctionTemplate::New(SignalWatcher::New);
|
||
15 years ago
|
constructor_template = Persistent<FunctionTemplate>::New(t);
|
||
|
constructor_template->Inherit(EventEmitter::constructor_template);
|
||
|
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
|
||
15 years ago
|
constructor_template->SetClassName(String::NewSymbol("SignalWatcher"));
|
||
15 years ago
|
|
||
15 years ago
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", SignalWatcher::Stop);
|
||
15 years ago
|
|
||
15 years ago
|
signal_symbol = NODE_PSYMBOL("signal");
|
||
|
|
||
15 years ago
|
target->Set(String::NewSymbol("SignalWatcher"),
|
||
15 years ago
|
constructor_template->GetFunction());
|
||
|
}
|
||
|
|
||
15 years ago
|
void SignalWatcher::OnSignal(EV_P_ ev_signal *watcher, int revents) {
|
||
|
SignalWatcher *w = static_cast<SignalWatcher*>(watcher->data);
|
||
15 years ago
|
HandleScope scope;
|
||
|
|
||
|
assert(revents == EV_SIGNAL);
|
||
|
|
||
15 years ago
|
w->Emit(signal_symbol, 0, NULL);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
SignalWatcher::~SignalWatcher() {
|
||
15 years ago
|
if (watcher_.active) {
|
||
15 years ago
|
ev_ref(EV_DEFAULT_UC);
|
||
15 years ago
|
ev_signal_stop(EV_DEFAULT_UC_ &watcher_);
|
||
|
}
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
Handle<Value> SignalWatcher::New(const Arguments& args) {
|
||
15 years ago
|
HandleScope scope;
|
||
|
|
||
|
if (args.Length() != 1 || !args[0]->IsInt32()) {
|
||
|
return ThrowException(String::New("Bad arguments"));
|
||
|
}
|
||
|
|
||
|
int sig = args[0]->Int32Value();
|
||
|
|
||
15 years ago
|
SignalWatcher *w = new SignalWatcher();
|
||
|
w->Wrap(args.Holder());
|
||
15 years ago
|
|
||
15 years ago
|
ev_signal_init(&w->watcher_, SignalWatcher::OnSignal, sig);
|
||
|
w->watcher_.data = w;
|
||
15 years ago
|
// Give signal handlers very high priority. The only thing that has higher
|
||
|
// priority is the garbage collector check.
|
||
15 years ago
|
ev_set_priority(&w->watcher_, EV_MAXPRI-1);
|
||
|
ev_signal_start(EV_DEFAULT_UC_ &w->watcher_);
|
||
15 years ago
|
ev_unref(EV_DEFAULT_UC);
|
||
15 years ago
|
|
||
15 years ago
|
w->Ref();
|
||
15 years ago
|
|
||
|
return args.This();
|
||
|
}
|
||
|
|
||
15 years ago
|
Handle<Value> SignalWatcher::Stop(const Arguments& args) {
|
||
15 years ago
|
HandleScope scope;
|
||
|
|
||
15 years ago
|
SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
|
||
15 years ago
|
ev_ref(EV_DEFAULT_UC);
|
||
15 years ago
|
ev_signal_stop(EV_DEFAULT_UC_ &w->watcher_);
|
||
|
w->Unref();
|
||
15 years ago
|
return Undefined();
|
||
|
}
|
||
|
|
||
|
} // namespace node
|