-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: split out callback queue implementation from Environment
This isn’t conceptually tied to anything Node.js-specific at all. PR-URL: #33272 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
- Loading branch information
1 parent
ad86807
commit 548cedd
Showing
6 changed files
with
179 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#ifndef SRC_CALLBACK_QUEUE_H_ | ||
#define SRC_CALLBACK_QUEUE_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include <atomic> | ||
|
||
namespace node { | ||
|
||
// A queue of C++ functions that take Args... as arguments and return R | ||
// (this is similar to the signature of std::function). | ||
// New entries are added using `CreateCallback()`/`Push()`, and removed using | ||
// `Shift()`. | ||
// The `refed` flag is left for easier use in situations in which some of these | ||
// should be run even if nothing else is keeping the event loop alive. | ||
template <typename R, typename... Args> | ||
class CallbackQueue { | ||
public: | ||
class Callback { | ||
public: | ||
explicit inline Callback(bool refed); | ||
|
||
virtual ~Callback() = default; | ||
virtual R Call(Args... args) = 0; | ||
|
||
inline bool is_refed() const; | ||
|
||
private: | ||
inline std::unique_ptr<Callback> get_next(); | ||
inline void set_next(std::unique_ptr<Callback> next); | ||
|
||
bool refed_; | ||
std::unique_ptr<Callback> next_; | ||
|
||
friend class CallbackQueue; | ||
}; | ||
|
||
template <typename Fn> | ||
inline std::unique_ptr<Callback> CreateCallback(Fn&& fn, bool refed); | ||
|
||
inline std::unique_ptr<Callback> Shift(); | ||
inline void Push(std::unique_ptr<Callback> cb); | ||
// ConcatMove adds elements from 'other' to the end of this list, and clears | ||
// 'other' afterwards. | ||
inline void ConcatMove(CallbackQueue&& other); | ||
|
||
// size() is atomic and may be called from any thread. | ||
inline size_t size() const; | ||
|
||
private: | ||
template <typename Fn> | ||
class CallbackImpl final : public Callback { | ||
public: | ||
CallbackImpl(Fn&& callback, bool refed); | ||
R Call(Args... args) override; | ||
|
||
private: | ||
Fn callback_; | ||
}; | ||
|
||
std::atomic<size_t> size_ {0}; | ||
std::unique_ptr<Callback> head_; | ||
Callback* tail_ = nullptr; | ||
}; | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_CALLBACK_QUEUE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters