{"id":220206,"date":"2021-06-19T09:15:25","date_gmt":"2021-06-19T01:15:25","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=220206"},"modified":"2021-06-06T16:16:19","modified_gmt":"2021-06-06T08:16:19","slug":"libuv-node-js","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/libuv-node-js.html","title":{"rendered":"N-API\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\u89e3\u51b3Node.js\u4e0d\u9002\u5408\u5904\u7406\u8017\u65f6\u64cd\u4f5c\u95ee\u9898"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nNode.js\u4e0d\u9002\u5408\u5904\u7406\u8017\u65f6\u64cd\u4f5c\u662f\u4e00\u76f4\u5b58\u5728\u7684\u95ee\u9898\uff0c\u4e3a\u6b64Node.js\u63d0\u4f9b\u4e86\u4e09\u79cd\u89e3\u51b3\u65b9\u6848\u3002\u5b50\u8fdb\u7a0b\u3001\u5b50\u7ebf\u7a0b\u3001 Libuv\u7ebf\u7a0b\u6c60,\u524d\u4e24\u79cd\u662f\u5f00\u53d1\u6548\u7387\u6bd4\u8f83\u9ad8\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u5199js\u3002\u4f46\u662f\u4e5f\u6709\u4e9b\u7f3a\u70b9.\u6267\u884cjs\u7684\u6210\u672c,\u867d\u7136\u53ef\u4ee5\u95f4\u63a5\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\uff0c\u4f46\u662f\u53d7\u9650\u4e8eNode.js\u63d0\u4f9b\u7684API,\u65e0\u6cd5\u5229\u7528c\/c++\u5c42\u63d0\u4f9b\u7684\u89e3\u51b3\u65b9\u6848(\u5185\u7f6e\u6216\u4e1a\u754c\u7684)\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u7b2c\u4e09\u79cd\u89e3\u51b3\u65b9\u6848\u3002\u76f4\u63a5\u901a\u8fc7N-API\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\u3002\u4e0b\u9762\u6211\u4eec\u770b\u770b\u8fd9\u4e48\u505a\u3002N-API\u63d0\u4f9b\u4e86\u51e0\u4e2aAPI\u3002<\/p>\n

napi_create_async_work \/\/ \u521b\u5efa\u4e00\u4e2aworr\uff0c\u4f46\u662f\u8fd8\u6ca1\u6709\u6267\u884c \r\nnapi_delete_async_work \/\/ \u91ca\u653e\u4e0a\u9762\u521b\u5efa\u7684work\u7684\u5185\u5b58 \r\nnapi_queue_async_work \/\/ \u5f80Libuv\u63d0\u4ea4\u4e00\u4e2awork \r\nnapi_cancel_async_work \/\/ \u53d6\u6d88Libuv\u4e2d\u7684\u4efb\u52a1\uff0c\u5982\u679c\u5df2\u7ecf\u5728\u6267\u884c\u5219\u65e0\u6cd5\u53d6\u6d88 <\/pre>\n

\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u770b\u5982\u4f55\u901a\u8fc7N-API\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\u3002\u9996\u5148\u770b\u770bjs\u5c42\u3002<\/p>\n

const { submitWork } = require('.\/build\/Release\/test.node'); \r\nsubmitWork((sum) => { \r\n    console.log(sum) \r\n}) <\/pre>\n

js\u63d0\u4ea4\u4e00\u4e2a\u4efb\u52a1\uff0c\u7136\u540e\u4f20\u5165\u4e00\u4e2a\u56de\u8c03\u3002\u63a5\u7740\u770b\u770bN-API\u7684\u4ee3\u7801\u3002<\/p>\n

napi_value Init(napi_env env, napi_value exports) { \r\n  napi_value func; \r\n  napi_create_function(env, \r\n                      NULL, \r\n                      NAPI_AUTO_LENGTH, \r\n                      submitWork, \r\n                      NULL, \r\n                      &func); \r\n  napi_set_named_property(env, exports, \"submitWork\", func); \r\n  return exports; \r\n} \r\n \r\nNAPI_MODULE(NODE_GYP_MODULE_NAME, Init) <\/pre>\n

\u9996\u5148\u5b9a\u4e49\u5bfc\u51fa\u7684\u51fd\u6570\uff0c\u63a5\u7740\u770b\u6838\u5fc3\u903b\u8f91\u3002<\/p>\n

\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u4fdd\u5b58\u4e0a\u4e0b\u6587<\/strong><\/div>\n
struct info \r\n{ \r\n  int sum; \/\/ \u4fdd\u5b58\u8ba1\u7b97\u7ed3\u679c \r\n  napi_ref func; \/\/ \u4fdd\u5b58\u56de\u8c03 \r\n  napi_async_work worker; \/\/ \u4fdd\u5b58work\u5bf9\u8c61 \r\n}; <\/pre>\n
\u63d0\u4ea4\u4efb\u52a1\u5230Libuv<\/strong><\/div>\n
static napi_value submitWork(napi_env env, napi_callback_info info) { \r\n  napi_value resource_name; \r\n  napi_status status; \r\n   \r\n  size_t argc = 1; \r\n  napi_value args[1]; \r\n  struct info data = {0, nullptr, nullptr}; \r\n  struct info * ptr = &data; \r\n  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL); \r\n  if (status != napi_ok) { \r\n    goto done; \r\n  } \r\n  napi_create_reference(env, args[0], 1, &ptr->func); \r\n  status = napi_create_string_utf8(env,\"test\", NAPI_AUTO_LENGTH, &resource_name); \r\n  if (status != napi_ok) { \r\n    goto done; \r\n  } \r\n  \/\/ \u521b\u5efa\u4e00\u4e2awork\uff0cptr\u4fdd\u5b58\u7684\u4e0a\u4e0b\u6587\u4f1a\u5728work\u51fd\u6570\u548cdone\u51fd\u6570\u91cc\u4f7f\u7528 \r\n  status = napi_create_async_work(env, nullptr, resource_name, work, done, (void *) ptr, &ptr->worker); \r\n  if (status != napi_ok) { \r\n    goto done; \r\n  } \r\n  \/\/ \u63d0\u53cawork\u5230Libuv \r\n  status = napi_queue_async_work(env, ptr->worker); \r\n \r\n  done:  \r\n    napi_value ret; \r\n    napi_create_int32(env, status == napi_ok ? 0 : -1, &ret); \r\n    return  ret; \r\n} <\/pre>\n

\u6267\u884c\u4e0a\u9762\u7684\u51fd\u6570\uff0c\u4efb\u52a1\u5c31\u4f1a\u88ab\u63d0\u4ea4\u5230Libuv\u7ebf\u7a0b\u6c60\u4e86\u3002<\/p>\n

Libuv\u5b50\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1<\/strong><\/div>\n
void work(napi_env env, void* data) { \r\n  struct info *arg = (struct info *)data; \r\n  printf(\"doing...\\n\"); \r\n  int sum = 0; \r\n  for (int i = 0; i < 10; i++) { \r\n    sum += i; \r\n  } \r\n  arg->sum = sum; \r\n} <\/pre>\n

\u5f88\u7b80\u5355\uff0c\u8ba1\u7b97\u51e0\u4e2a\u6570\u3002\u5e76\u4e14\u4fdd\u5b58\u7ed3\u679c\u3002<\/p>\n

\u56de\u8c03js<\/strong><\/div>\n
void done(napi_env env, napi_status status, void* data) { \r\n  struct info *arg = (struct info *)data; \r\n  if (status == napi_cancelled) { \r\n    printf(\"cancel...\"); \r\n  } else if (status == napi_ok) { \r\n    printf(\"done...\\n\"); \r\n    napi_value callback; \r\n    napi_value global;   \r\n    napi_value result; \r\n    napi_value sum; \r\n    \/\/ \u62ff\u5230\u7ed3\u679c \r\n    napi_create_int32(env, arg->sum, &sum); \r\n    napi_get_reference_value(env, arg->func, &callback); \r\n    napi_get_global(env, &global); \r\n    \/\/ \u56de\u8c03js \r\n    napi_call_function(env, global, callback, 1, &sum, &result); \r\n    \/\/ \u6e05\u7406 \r\n    napi_delete_reference(env, arg->func); \r\n    napi_delete_async_work(env, arg->worker); \r\n  } \r\n} <\/pre>\n

\u5e76\u4e14\u6267\u884c\u540e\uff0c\u6211\u4eec\u770b\u5230\u8f93\u51fa\u4e8645\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5206\u6790\u5927\u81f4\u7684\u8fc7\u7a0b\u3002\u9996\u5148\u6211\u5462\u770b\u770bThreadPoolWork\uff0cThreadPoolWork\u662f\u5bf9Libuv work\u7684\u5c01\u88c5\u3002<\/p>\n

class ThreadPoolWork { \r\n public: \r\n  explicit inline ThreadPoolWork(Environment* env) : env_(env) { \r\n    CHECK_NOT_NULL(env); \r\n  } \r\n  inline virtual ~ThreadPoolWork() = default; \r\n \r\n  inline void ScheduleWork(); \r\n  inline int CancelWork(); \r\n \r\n  virtual void DoThreadPoolWork() = 0; \r\n  virtual void AfterThreadPoolWork(int status) = 0; \r\n \r\n  Environment* env() const { return env_; } \r\n \r\n private: \r\n  Environment* env_; \r\n  uv_work_t work_req_; \r\n}; <\/pre>\n

\u7c7b\u7684\u5b9a\u4e49\u5f88\u7b80\u5355\uff0c\u4e3b\u8981\u662f\u5c01\u88c5\u4e86uv_work_t\u3002\u6211\u4eec\u770b\u770b\u6bcf\u4e2a\u51fd\u6570\u7684\u610f\u4e49\u3002DoThreadPoolWork\u548cAfterThreadPoolWork\u662f\u865a\u51fd\u6570\uff0c\u7531\u5b50\u7c7b\u5b9e\u73b0\uff0c\u6211\u4eec\u4e00\u4f1a\u770b\u5b50\u7c7b\u7684\u65f6\u5019\u518d\u5206\u6790\u3002\u6211\u4eec\u770b\u770bScheduleWork<\/p>\n

void ThreadPoolWork::ScheduleWork() { \r\n  env_->IncreaseWaitingRequestCounter(); \r\n  int status = uv_queue_work( \r\n      env_->event_loop(), \r\n      &work_req_, \r\n      \/\/ Libuv\u5b50\u7ebf\u7a0b\u91cc\u6267\u884c\u7684\u4efb\u52a1\u51fd\u6570 \r\n      [](uv_work_t* req) { \r\n        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req); \r\n        self->DoThreadPoolWork(); \r\n      }, \r\n      \/\/ \u4efb\u52a1\u5904\u7406\u5b8c\u540e\u7684\u56de\u8c03 \r\n      [](uv_work_t* req, int status) { \r\n        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req); \r\n        self->env_->DecreaseWaitingRequestCounter(); \r\n        self->AfterThreadPoolWork(status); \r\n      }); \r\n  CHECK_EQ(status, 0); \r\n} <\/pre>\n

ScheduleWork\u662f\u8d1f\u8d23\u7ed9Libuv\u63d0\u4ea4\u4efb\u52a1\u7684\u51fd\u6570\u3002\u63a5\u7740\u770b\u770bCancelWork\u3002<\/p>\n

int ThreadPoolWork::CancelWork() { \r\n  return uv_cancel(reinterpret_cast(&work_req_)); \r\n} <\/pre>\n

\u76f4\u63a5\u8c03\u7528Libuv\u7684\u51fd\u6570\u53d6\u6d88\u4efb\u52a1\u3002\u770b\u5b8c\u7236\u7c7b\uff0c\u6211\u4eec\u770b\u770b\u5b50\u7c7b\u7684\u5b9a\u4e49\uff0c\u5b50\u7c7b\u5728N-API\u91cc\u5b9e\u73b0\u3002<\/p>\n

class Work : public node::AsyncResource, public node::ThreadPoolWork { \r\n private: \r\n  explicit Work(node_napi_env env, \r\n                v8::Local async_resource, \r\n                v8::Local async_resource_name, \r\n                napi_async_execute_callback execute, \r\n                napi_async_complete_callback complete = nullptr, \r\n                void* data = nullptr) \r\n    : AsyncResource(env->isolate, \r\n                    async_resource, \r\n                    *v8::String::Utf8Value(env->isolate, async_resource_name)), \r\n      ThreadPoolWork(env->node_env()), \r\n      _env(env), \r\n      _data(data), \r\n      _execute(execute), \r\n      _complete(complete) { \r\n  } \r\n \r\n  ~Work() override = default; \r\n \r\n public: \r\n  static Work* New(node_napi_env env, \r\n                   v8::Local async_resource, \r\n                   v8::Local async_resource_name, \r\n                   napi_async_execute_callback execute, \r\n                   napi_async_complete_callback complete, \r\n                   void* data) { \r\n    return new Work(env, async_resource, async_resource_name, \r\n                    execute, complete, data); \r\n  } \r\n  \/\/ \u91ca\u653e\u8be5\u7c7b\u5bf9\u8c61\u7684\u5185\u5b58 \r\n  static void Delete(Work* work) { \r\n    delete work; \r\n  } \r\n  \/\/ \u6267\u884c\u7528\u6237\u8bbe\u7f6e\u7684\u51fd\u6570 \r\n  void DoThreadPoolWork() override { \r\n    _execute(_env, _data); \r\n  } \r\n \r\n  void AfterThreadPoolWork(int status) override { \r\n   \/\/ \u6267\u884c\u7528\u6237\u8bbe\u7f6e\u7684\u56de\u8c03 \r\n    _complete(env, ConvertUVErrorCode(status), _data); \r\n  } \r\n \r\n private: \r\n  node_napi_env _env; \r\n  \/\/ \u7528\u6237\u8bbe\u7f6e\u7684\u6570\u636e\uff0c\u7528\u4e8e\u4fdd\u5b58\u6267\u884c\u7ed3\u679c\u7b49 \r\n  void* _data; \r\n  \/\/ \u6267\u884c\u4efb\u52a1\u7684\u51fd\u6570 \r\n  napi_async_execute_callback _execute; \r\n  \/\/ \u4efb\u52a1\u5904\u7406\u5b8c\u7684\u56de\u8c03 \r\n  napi_async_complete_callback _complete; \r\n}; <\/v8::string><\/v8::object><\/v8::string><\/v8::object><\/pre>\n

\u5728Work\u7c7b\u6211\u4eec\u770b\u5230\u4e86\u865a\u51fd\u6570DoThreadPoolWork\u548cAfterThreadPoolWork\u7684\u5b9e\u73b0\uff0c\u6ca1\u6709\u592a\u591a\u903b\u8f91\u3002\u6700\u540e\u6211\u4eec\u770b\u770bN-API\u63d0\u4f9b\u7684API\u7684\u5b9e\u73b0\u3002<\/p>\n

napi_status napi_create_async_work(napi_env env, \r\n                                   napi_value async_resource, \r\n                                   napi_value async_resource_name, \r\n                                   napi_async_execute_callback execute, \r\n                                   napi_async_complete_callback complete, \r\n                                   void* data, \r\n                                   napi_async_work* result) { \r\n  v8::Local context = env->context(); \r\n \r\n  v8::Local resource; \r\n  if (async_resource != nullptr) { \r\n    CHECK_TO_OBJECT(env, context, resource, async_resource); \r\n  } else { \r\n    resource = v8::Object::New(env->isolate); \r\n  } \r\n \r\n  v8::Local resource_name; \r\n  CHECK_TO_STRING(env, context, resource_name, async_resource_name); \r\n \r\n  uvimpl::Work* work = uvimpl::Work::New(reinterpret_cast(env), \r\n                                         resource, \r\n                                         resource_name, \r\n                                         execute, \r\n                                         complete, \r\n                                         data); \r\n \r\n  *result = reinterpret_cast(work); \r\n \r\n  return napi_clear_last_error(env); \r\n} <\/napi_async_work><\/node_napi_env><\/v8::string><\/v8::object><\/v8::context><\/pre>\n

napi_create_async_work\u672c\u8d28\u4e0a\u662f\u5bf9Work\u7684\u7b80\u5355\u5c01\u88c5\uff0c\u521b\u5efa\u4e00\u4e2aWork\u5e76\u8fd4\u56de\u7ed9\u7528\u6237\u3002<\/p>\n

napi_delete_async_work<\/strong><\/span><\/div>\n
napi_status napi_delete_async_work(napi_env env, napi_async_work work) { \r\n  CHECK_ENV(env); \r\n  CHECK_ARG(env, work); \r\n \r\n  uvimpl::Work::Delete(reinterpret_cast(work)); \r\n \r\n  return napi_clear_last_error(env); \r\n} <\/pre>\n

napi_delete_async_work\u7528\u4e8e\u4efb\u52a1\u6267\u884c\u5b8c\u540e\u91ca\u653eWork\u5bf9\u5e94\u7684\u5185\u5b58\u3002<\/p>\n

napi_queue_async_work<\/strong><\/span><\/div>\n
napi_status napi_queue_async_work(napi_env env, napi_async_work work) { \r\n  CHECK_ENV(env); \r\n  CHECK_ARG(env, work); \r\n \r\n  napi_status status; \r\n  uv_loop_t* event_loop = nullptr; \r\n  status = napi_get_uv_event_loop(env, &event_loop); \r\n  if (status != napi_ok) \r\n    return napi_set_last_error(env, status); \r\n \r\n  uvimpl::Work* w = reinterpret_cast(work); \r\n \r\n  w->ScheduleWork(); \r\n \r\n  return napi_clear_last_error(env); \r\n} <\/pre>\n

napi_queue_async_work\u662f\u5bf9ScheduleWork\u7684\u5c01\u88c5\uff0c\u4f5c\u7528\u662f\u7ed9Libuv\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4efb\u52a1\u3002<\/p>\n

napi_cancel_async_work<\/strong><\/span><\/div>\n
napi_status napi_cancel_async_work(napi_env env, napi_async_work work) { \r\n  CHECK_ENV(env); \r\n  CHECK_ARG(env, work); \r\n \r\n  uvimpl::Work* w = reinterpret_cast(work); \r\n \r\n  CALL_UV(env, w->CancelWork()); \r\n \r\n  return napi_clear_last_error(env); \r\n} <\/pre>\n

napi_cancel_async_work\u662f\u5bf9CancelWork\u7684\u5c01\u88c5\uff0c\u5373\u53d6\u6d88Libuv\u7ebf\u7a0b\u6c60\u7684\u4efb\u52a1\u3002\u6211\u4eec\u770b\u5230\u4e00\u5c42\u5c42\u5957\uff0c\u6ca1\u6709\u592a\u591a\u903b\u8f91\uff0c\u4e3b\u8981\u662f\u8981\u7b26\u5408N-API\u7684\u89c4\u8303\u3002<\/p>\n

\u603b\u7ed3\uff1a\u901a\u8fc7N-API\u63d0\u4f9b\u7684API\uff0c\u4f7f\u5f97\u6211\u4eec\u4e0d\u518d\u53d7\u9650\u4e8eNod.js\u672c\u8eab\u63d0\u4f9b\u7684\u4e00\u4e9b\u5f02\u6b65\u63a5\u53e3(\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\u7684\u63a5\u53e3)\uff0c\u800c\u662f\u76f4\u63a5\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\uff0c\u8fd9\u6837\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u81ea\u5df1\u5199c\/c++\uff0c\u8fd8\u53ef\u4ee5\u590d\u7528\u4e1a\u754c\u7684\u4e00\u4e9b\u89e3\u51b3\u65b9\u6848\u89e3\u51b3Node.js\u91cc\u7684\u4e00\u4e9b\u8017\u65f6\u4efb\u52a1\u3002<\/p>\n

\u4ed3\u5e93\uff1ahttps:\/\/github.com\/theanarkh\/learn-to-write-nodejs-addons<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u7b2c\u4e09\u79cd\u89e3\u51b3\u65b9\u6848\u3002\u76f4\u63a5\u901a\u8fc7N-API\u4f7f\u7528Libuv\u7ebf\u7a0b\u6c60\u3002\u4e0b\u9762\u6211\u4eec\u770b\u770b\u8fd9\u4e48\u505a\u3002N-API\u63d0\u4f9b\u4e86\u51e0\u4e2aA […]<\/p>\n","protected":false},"author":1920,"featured_media":220201,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-220206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=220206"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220206\/revisions"}],"predecessor-version":[{"id":220218,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220206\/revisions\/220218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/220201"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=220206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=220206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=220206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}