class Alexandria::ExecutionQueue

Public Class Methods

current() click to toggle source
# File lib/alexandria/execution_queue.rb, line 42
def self.current
  @@current_queue
rescue StandardError
  nil
end
new() click to toggle source
# File lib/alexandria/execution_queue.rb, line 33
def initialize
  @pending_calls = []
  @pending_retvals = []
  @protect_pending_calls = Mutex.new
  @protect_pending_retvals = Mutex.new
  @id = 0
  @@current_queue = self
end

Public Instance Methods

call(procedure, *args) click to toggle source

For the requesting thread.

# File lib/alexandria/execution_queue.rb, line 49
def call(procedure, *args)
  push(procedure, args, false)
end
iterate() click to toggle source

For the executing thread.

# File lib/alexandria/execution_queue.rb, line 58
def iterate
  ary = @protect_pending_calls.synchronize do
    break @pending_calls.pop
  end
  return if ary.nil?

  id, procedure, args, need_retval = ary
  retval = procedure.call(*args)

  return unless need_retval

  @protect_pending_retvals.synchronize do
    @pending_retvals << [id, retval]
  end
end
stop() click to toggle source
# File lib/alexandria/execution_queue.rb, line 74
def stop
  @@current_queue = nil
end
sync_call(procedure, *args) click to toggle source
# File lib/alexandria/execution_queue.rb, line 53
def sync_call(procedure, *args)
  push(procedure, args, true)
end

Private Instance Methods

push(procedure, args, need_retval = false) click to toggle source
# File lib/alexandria/execution_queue.rb, line 80
def push(procedure, args, need_retval = false)
  @protect_pending_calls.synchronize do
    @id += 1
    @pending_calls << [@id, procedure, args, need_retval]
  end
  return unless need_retval

  loop do
    @protect_pending_retvals.synchronize do
      ary = @pending_retvals.find { |id, _retval| id == @id }
      if ary
        @pending_retvals.delete(ary)
        return ary[1]
      end
    end
  end
end