class Rosarium::FixedThreadExecutor

Public Class Methods

new(max = 1) click to toggle source
# File lib/rosarium/fixed_thread_executor.rb, line 6
def initialize(max = 1)
  @max = max
  @mutex = Mutex.new
  @waiting = []
  @executing = 0
  @threads = []
end

Public Instance Methods

discard() click to toggle source
# File lib/rosarium/fixed_thread_executor.rb, line 25
def discard
  @mutex.synchronize { @waiting.clear }
end
submit(&block) click to toggle source
# File lib/rosarium/fixed_thread_executor.rb, line 14
def submit(&block)
  @mutex.synchronize do
    @waiting << block
    if @executing < @max
      @executing += 1
      t = Thread.new { execute_and_count_down }
      @threads.push t
    end
  end
end
wait_until_idle() click to toggle source
# File lib/rosarium/fixed_thread_executor.rb, line 29
def wait_until_idle
  loop do
    t = @mutex.synchronize { @threads.shift }
    t or break
    t.join
  end
end

Private Instance Methods

execute() click to toggle source
# File lib/rosarium/fixed_thread_executor.rb, line 47
def execute
  loop do
    block = @mutex.synchronize { @waiting.shift }
    block or break
    begin
      block.call
    rescue Exception
    end
  end
end
execute_and_count_down() click to toggle source
# File lib/rosarium/fixed_thread_executor.rb, line 39
def execute_and_count_down
  execute
ensure
  @mutex.synchronize do
    @executing -= 1
  end
end