diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-05-07 00:36:10 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-05-09 06:43:46 +0300 |
commit | fea01c11792cdbeca9a81525ed46a1b83192f802 (patch) | |
tree | 06a12c4f1a4083316b39fb043bca41d0d5b0a9cd /src/callback_queue-inl.h | |
parent | 2c3c9f5a3f6d54230dcc65efacb5b57d0979b462 (diff) |
src: split out callback queue implementation from Environment
This isn’t conceptually tied to anything Node.js-specific at all.
PR-URL: https://github.com/nodejs/node/pull/33272
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'src/callback_queue-inl.h')
-rw-r--r-- | src/callback_queue-inl.h | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/callback_queue-inl.h b/src/callback_queue-inl.h new file mode 100644 index 00000000000..e83c81cd0dd --- /dev/null +++ b/src/callback_queue-inl.h @@ -0,0 +1,97 @@ +#ifndef SRC_CALLBACK_QUEUE_INL_H_ +#define SRC_CALLBACK_QUEUE_INL_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "callback_queue.h" + +namespace node { + +template <typename R, typename... Args> +template <typename Fn> +std::unique_ptr<typename CallbackQueue<R, Args...>::Callback> +CallbackQueue<R, Args...>::CreateCallback(Fn&& fn, bool refed) { + return std::make_unique<CallbackImpl<Fn>>(std::move(fn), refed); +} + +template <typename R, typename... Args> +std::unique_ptr<typename CallbackQueue<R, Args...>::Callback> +CallbackQueue<R, Args...>::Shift() { + std::unique_ptr<Callback> ret = std::move(head_); + if (ret) { + head_ = ret->get_next(); + if (!head_) + tail_ = nullptr; // The queue is now empty. + } + size_--; + return ret; +} + +template <typename R, typename... Args> +void CallbackQueue<R, Args...>::Push(std::unique_ptr<Callback> cb) { + Callback* prev_tail = tail_; + + size_++; + tail_ = cb.get(); + if (prev_tail != nullptr) + prev_tail->set_next(std::move(cb)); + else + head_ = std::move(cb); +} + +template <typename R, typename... Args> +void CallbackQueue<R, Args...>::ConcatMove(CallbackQueue<R, Args...>&& other) { + size_ += other.size_; + if (tail_ != nullptr) + tail_->set_next(std::move(other.head_)); + else + head_ = std::move(other.head_); + tail_ = other.tail_; + other.tail_ = nullptr; + other.size_ = 0; +} + +template <typename R, typename... Args> +size_t CallbackQueue<R, Args...>::size() const { + return size_.load(); +} + +template <typename R, typename... Args> +CallbackQueue<R, Args...>::Callback::Callback(bool refed) + : refed_(refed) {} + +template <typename R, typename... Args> +bool CallbackQueue<R, Args...>::Callback::is_refed() const { + return refed_; +} + +template <typename R, typename... Args> +std::unique_ptr<typename CallbackQueue<R, Args...>::Callback> +CallbackQueue<R, Args...>::Callback::get_next() { + return std::move(next_); +} + +template <typename R, typename... Args> +void CallbackQueue<R, Args...>::Callback::set_next( + std::unique_ptr<Callback> next) { + next_ = std::move(next); +} + +template <typename R, typename... Args> +template <typename Fn> +CallbackQueue<R, Args...>::CallbackImpl<Fn>::CallbackImpl( + Fn&& callback, bool refed) + : Callback(refed), + callback_(std::move(callback)) {} + +template <typename R, typename... Args> +template <typename Fn> +R CallbackQueue<R, Args...>::CallbackImpl<Fn>::Call(Args... args) { + return callback_(std::forward<Args>(args)...); +} + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_CALLBACK_QUEUE_INL_H_ |