-
Notifications
You must be signed in to change notification settings - Fork 4
/
tread.cc
74 lines (43 loc) · 1.47 KB
/
tread.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <node.h>
#include <string>
#include <cstdlib>
#include <v8.h>
#include <thread>
#include <chrono>
#include <uv.h>
#include <sys/types.h>
#include <unistd.h>
using namespace v8;
struct Work {
uv_work_t request;
Persistent<Function> callback;
};
static void call_async(uv_work_t *req) {
printf("\n%s Thread id : gettid() == %d\n",__FUNCTION__,getpid());
Work *work = static_cast<Work *>(req->data);
usleep(4000 * 100);
};
static void call_async_complete(uv_work_t *req,int status) {
printf("\n%s Thread id : gettid() == %d\n",__FUNCTION__,getpid());
Isolate * isolate = Isolate::GetCurrent();
v8::HandleScope handleScope(isolate);
Work *work = static_cast<Work *>(req->data);
Handle<Value> argv[] = { };
Local<Function>::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 0, argv);
delete work;
};
void call(const FunctionCallbackInfo<Value>& args) {
printf("\n%s Thread id : gettid() == %d\n",__FUNCTION__,getpid());
Isolate* isolate = args.GetIsolate();
Work * work = new Work();
work->request.data = work;
Local<Function> callback = Local<Function>::Cast(args[0]);
work->callback.Reset(isolate, callback);
uv_queue_work(uv_default_loop(),&work->request,call_async,call_async_complete);
args.GetReturnValue().Set(Undefined(isolate));
return;
};
void init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "startServer", call);
}
NODE_MODULE(addon, init)