Class: Pigeon::Dispatcher

Inherits:
Object
  • Object
show all
Extended by:
OptionAccessor
Defined in:
lib/pigeon/dispatcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionAccessor

option_accessor, option_reader, option_writer

Constructor Details

#initialize(limit = nil) ⇒ Dispatcher

Creates a new instance of a dispatcher. An optional limit parameter is used to specify how many threads can be used, which if nil will use the concurrency_limit established for the class.



24
25
26
27
28
29
30
# File 'lib/pigeon/dispatcher.rb', line 24

def initialize(limit = nil)
  @thread_limit = limit
  @backlog = [ ]
  @threads = [ ]
  @exceptions = [ ]
  @sempaphore = Mutex.new
end

Instance Attribute Details

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



15
16
17
# File 'lib/pigeon/dispatcher.rb', line 15

def exceptions
  @exceptions
end

Instance Method Details

#backlog_sizeObject

Returns the number of items in the backlog queue.



54
55
56
# File 'lib/pigeon/dispatcher.rb', line 54

def backlog_size
  @backlog.length
end

#empty?Boolean

Returns true if there are no operations in the backlog queue or running, false otherwise.



44
45
46
# File 'lib/pigeon/dispatcher.rb', line 44

def empty?
  @backlog.empty? and @threads.empty?
end

#exceptions?Boolean

Returns true if any exceptions have been generated, false otherwise.



49
50
51
# File 'lib/pigeon/dispatcher.rb', line 49

def exceptions?
  !@exceptions.empty?
end

#perform(*args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/pigeon/dispatcher.rb', line 32

def perform(*args, &block)
  @backlog << [ block, args, caller(0) ]

  @sempaphore.synchronize do
    if (@threads.length < self.thread_limit and @threads.length < @backlog.length)
      create_thread
    end
  end
end

#thread_countObject

Returns the current number of threads executing.



59
60
61
# File 'lib/pigeon/dispatcher.rb', line 59

def thread_count
  @threads.length
end

#wait!Object

Waits until all operations have completed, including the backlog.



64
65
66
67
68
# File 'lib/pigeon/dispatcher.rb', line 64

def wait!
  while (!@threads.empty?)
    ThreadsWait.new(@threads).join
  end
end