diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-09 17:40:24 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-05-10 14:15:18 +0200 |
commit | c0720570498895d06dcec4e8f01e8922a81ac78b (patch) | |
tree | 1eb0602394de051999fc044b08399b3ae5a57f43 /src/node_internals.h | |
parent | 2b3150466e5f4db841f5980cda32172555bdda15 (diff) | |
download | android-node-v8-c0720570498895d06dcec4e8f01e8922a81ac78b.tar.gz android-node-v8-c0720570498895d06dcec4e8f01e8922a81ac78b.tar.bz2 android-node-v8-c0720570498895d06dcec4e8f01e8922a81ac78b.zip |
src: unify thread pool work
Instead of using the libuv mechanism directly, provide an internal
`ThreadPoolWork` wrapper that takes care of increasing/decreasing
the waiting request counter.
PR-URL: https://github.com/nodejs/node/pull/19377
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_internals.h')
-rw-r--r-- | src/node_internals.h | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/node_internals.h b/src/node_internals.h index e15df78ffd..8aa4631880 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -503,6 +503,41 @@ class InternalCallbackScope { bool closed_ = false; }; +class ThreadPoolWork { + public: + explicit inline ThreadPoolWork(Environment* env) : env_(env) {} + inline void ScheduleWork(); + inline int CancelWork(); + + virtual void DoThreadPoolWork() = 0; + virtual void AfterThreadPoolWork(int status) = 0; + + private: + Environment* env_; + uv_work_t work_req_; +}; + +void ThreadPoolWork::ScheduleWork() { + env_->IncreaseWaitingRequestCounter(); + int status = uv_queue_work( + env_->event_loop(), + &work_req_, + [](uv_work_t* req) { + ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req); + self->DoThreadPoolWork(); + }, + [](uv_work_t* req, int status) { + ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req); + self->env_->DecreaseWaitingRequestCounter(); + self->AfterThreadPoolWork(status); + }); + CHECK_EQ(status, 0); +} + +int ThreadPoolWork::CancelWork() { + return uv_cancel(reinterpret_cast<uv_req_t*>(&work_req_)); +} + static inline const char *errno_string(int errorno) { #define ERRNO_CASE(e) case e: return #e; switch (errorno) { |