VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/lib/gems/2.5.0/gems/eventmachine-1.2.7/lib/em/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/lib/gems/2.5.0/gems/eventmachine-1.2.7/lib/em/queue.rb
module EventMachine
  # A cross thread, reactor scheduled, linear queue.
  #
  # This class provides a simple queue abstraction on top of the reactor
  # scheduler. It services two primary purposes:
  #
  # * API sugar for stateful protocols
  # * Pushing processing onto the reactor thread
  #
  # @example
  #
  #  q = EM::Queue.new
  #  q.push('one', 'two', 'three')
  #  3.times do
  #    q.pop { |msg| puts(msg) }
  #  end
  #
  class Queue
    def initialize
      @sink  = []
      @drain = []
      @popq  = []
    end

    # Pop items off the queue, running the block on the reactor thread. The pop
    # will not happen immediately, but at some point in the future, either in
    # the next tick, if the queue has data, or when the queue is populated.
    #
    # @return [NilClass] nil
    def pop(*a, &b)
      cb = EM::Callback(*a, &b)
      EM.schedule do
        if @drain.empty?
          @drain = @sink
          @sink = []
        end
        if @drain.empty?
          @popq << cb
        else
          cb.call @drain.shift
        end
      end
      nil # Always returns nil
    end

    # Push items onto the queue in the reactor thread. The items will not appear
    # in the queue immediately, but will be scheduled for addition during the
    # next reactor tick.
    def push(*items)
      EM.schedule do
        @sink.push(*items)
        unless @popq.empty?
          @drain = @sink
          @sink = []
          @popq.shift.call @drain.shift until @drain.empty? || @popq.empty?
        end
      end
    end
    alias :<< :push

    # @return [Boolean]
    # @note This is a peek, it's not thread safe, and may only tend toward accuracy.
    def empty?
      @drain.empty? && @sink.empty?
    end

    # @return [Integer] Queue size
    # @note This is a peek, it's not thread safe, and may only tend toward accuracy.
    def size
      @drain.size + @sink.size
    end

    # @return [Integer] Waiting size
    # @note This is a peek at the number of jobs that are currently waiting on the Queue
    def num_waiting
      @popq.size
    end

  end # Queue
end # EventMachine

VaKeR 2022