class Dunder::Group

Attributes

max[R]
name[R]

Public Class Methods

new(max) click to toggle source
# File lib/dunder.rb, line 51
def initialize(max)
  raise ArgumentError,"You must specify a maximum number of threads for this group #{max}" unless max && max.is_a?(Integer)
  @max = max
  @running = 0
  @waiting = []
  @mutex = Mutex.new
end

Public Instance Methods

finish_thread() click to toggle source
# File lib/dunder.rb, line 98
def finish_thread
  thread = Thread.current
  @mutex.synchronize {
    @running -= 1
    unless @waiting.empty?
      # Schedule the next job
      t = @waiting.shift
      @running += 1
      t.wakeup
    end 
  }
end
init_thread() click to toggle source
# File lib/dunder.rb, line 85
def init_thread
  thread = Thread.current
  waiting = false
  @mutex.synchronize {
    if waiting = (@running >= @max)
      @waiting.push(thread)
    else
      @running += 1
    end
  }
  Thread.stop if waiting
end
lazy_load(&block) click to toggle source
# File lib/dunder.rb, line 71
def lazy_load(&block)
  Future.new(self,&block)
end
running() click to toggle source
# File lib/dunder.rb, line 59
def running
  @mutex.synchronize {
    @running
  }
end
start_thread(&block) click to toggle source
# File lib/dunder.rb, line 75
def start_thread(&block)
  group = self
  Thread.start {
    group.init_thread
    value = block.call
    group.finish_thread
    value
  }
end
waiting() click to toggle source
# File lib/dunder.rb, line 65
def waiting
  @mutex.synchronize {
    @waiting
  }
end