Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created January 25, 2012 15:49
Show Gist options
  • Save shigeki/1676875 to your computer and use it in GitHub Desktop.
Save shigeki/1676875 to your computer and use it in GitHub Desktop.
node module for uv_unref
var unref = require('./build/Release/unref.node').unref;
var http = require('http');
http.createServer(function(req,res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Loop Counter='+unref()+'\n');
}).listen(8080);
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <uv.h>
using namespace v8;
Handle<Value> Unref(const Arguments& args) {
HandleScope scope;
uv_unref(uv_default_loop());
Local<Number> num = Number::New(uv_loop_refcount(uv_default_loop()));
return scope.Close(num);
}
void Init(Handle<Object> target) {
target->Set(String::NewSymbol("unref"),
FunctionTemplate::New(Unref)->GetFunction());
}
NODE_MODULE(unref, Init)
srcdir = '.'
blddir = 'build'
VERSION = '0.0.1'
def set_options(opt):
opt.tool_options('compiler_cxx')
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.target = 'unref'
obj.source = ['unref.cc']
unix:~/tmp/addon> node-waf configure build
Checking for program g++ or c++ : /usr/bin/g++
Checking for program cpp : /usr/bin/cpp
Checking for program ar : /usr/bin/ar
Checking for program ranlib : /usr/bin/ranlib
Checking for g++ : ok
Checking for node path : not found
Checking for node prefix : ok /usr/local
'configure' finished successfully (0.025s)
Waf: Entering directory `/home/ohtsu/tmp/addon/build'
Waf: Leaving directory `/home/ohtsu/tmp/addon/build'
'build' finished successfully (0.005s)
unix:~/tmp/addon> node http_server.js
ブラウザーを使って port 8080 へアクセス。
リロードを続けて画面に出力されるイベントループのカウンター値が0になると自動的に node が終了します。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment