- 
          
- 
        Couldn't load subscription status. 
- Fork 33.6k
src: split out callback queue implementation from Environment #33272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
        
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.