├── .gitignore ├── README ├── nas.cc ├── package.json ├── test.js └── wscript /.gitignore: -------------------------------------------------------------------------------- 1 | .lock-wscript 2 | build/ 3 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is about the simplest reduction you can get for using eio_custom. 2 | 3 | To run: 4 | 5 | node-waf configure build && node test.js 6 | 7 | Or, you can link or install it with npm. Note that it has hardly anything 8 | in the package.json, either. 9 | 10 | npm link && npm test async-simple 11 | -------------------------------------------------------------------------------- /nas.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | using namespace node; 10 | using namespace v8; 11 | 12 | static Handle DoSomethingAsync (const Arguments&); 13 | static int DoSomething (eio_req *); 14 | static int DoSomething_After (eio_req *); 15 | extern "C" void init (Handle); 16 | 17 | struct simple_request { 18 | int x; 19 | int y; 20 | Persistent cb; 21 | // maybe it matters to put the char[] last? not sure. 22 | char name[1]; 23 | }; 24 | 25 | static Handle DoSomethingAsync (const Arguments& args) { 26 | HandleScope scope; 27 | const char *usage = "usage: doSomething(x, y, name, cb)"; 28 | if (args.Length() != 4) { 29 | return ThrowException(Exception::Error(String::New(usage))); 30 | } 31 | int x = args[0]->Int32Value(); 32 | int y = args[1]->Int32Value(); 33 | String::Utf8Value name(args[2]); 34 | Local cb = Local::Cast(args[3]); 35 | 36 | simple_request *sr = (simple_request *) 37 | malloc(sizeof(struct simple_request) + name.length() + 1); 38 | 39 | sr->cb = Persistent::New(cb); 40 | strncpy(sr->name, *name, name.length() + 1); 41 | sr->x = x; 42 | sr->y = y; 43 | 44 | eio_custom(DoSomething, EIO_PRI_DEFAULT, DoSomething_After, sr); 45 | ev_ref(EV_DEFAULT_UC); 46 | return Undefined(); 47 | } 48 | 49 | // this function happens on the thread pool 50 | // doing v8 things in here will make bad happen. 51 | static int DoSomething (eio_req *req) { 52 | struct simple_request * sr = (struct simple_request *)req->data; 53 | sleep(2); // just to make it less pointless to be async. 54 | req->result = sr->x + sr->y; 55 | return 0; 56 | } 57 | 58 | static int DoSomething_After (eio_req *req) { 59 | HandleScope scope; 60 | ev_unref(EV_DEFAULT_UC); 61 | struct simple_request * sr = (struct simple_request *)req->data; 62 | Local argv[3]; 63 | argv[0] = Local::New(Null()); 64 | argv[1] = Integer::New(req->result); 65 | argv[2] = String::New(sr->name); 66 | TryCatch try_catch; 67 | sr->cb->Call(Context::GetCurrent()->Global(), 3, argv); 68 | if (try_catch.HasCaught()) { 69 | FatalException(try_catch); 70 | } 71 | sr->cb.Dispose(); 72 | free(sr); 73 | return 0; 74 | } 75 | 76 | extern "C" void init (Handle target) { 77 | HandleScope scope; 78 | NODE_SET_METHOD(target, "doSomething", DoSomethingAsync); 79 | } 80 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { "name" : "async-simple" 2 | , "description" : "An eio_custom example program" 3 | , "version" : "1.0.0" 4 | , "author" : "Isaac Z. Schlueter (http://blog.izs.me/)" 5 | , "main" : "build/default/nas.node" 6 | , "scripts" : { "test" : "node test.js" } 7 | } 8 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var nas = require("./build/default/nas") 2 | , start = Date.now() 3 | 4 | console.log("js "+(Date.now() - start), "before hello") 5 | nas.doSomething(1, 2, "hello", function (er, res, n) { 6 | console.log("js "+(Date.now() - start), er, res, n) 7 | }) 8 | console.log("js "+(Date.now() - start), "before goodbye") 9 | nas.doSomething(3, 4, "goodbye", function (er, res, n) { 10 | console.log("js "+(Date.now() - start), er, res, n) 11 | }) 12 | -------------------------------------------------------------------------------- /wscript: -------------------------------------------------------------------------------- 1 | srcdir = "." 2 | blddir = "build" 3 | VERSION = "0.0.1" 4 | 5 | def set_options(opt): 6 | opt.tool_options("compiler_cxx") 7 | 8 | def configure(conf): 9 | conf.check_tool("compiler_cxx") 10 | conf.check_tool("node_addon") 11 | 12 | def build(bld): 13 | obj = bld.new_task_gen("cxx", "shlib", "node_addon") 14 | # without this, eio_custom doesn't keep a ref to the req->data 15 | obj.cxxflags = ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"] 16 | obj.target = "nas" 17 | obj.source = "nas.cc" --------------------------------------------------------------------------------