Class: Ruactor::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ruactor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



19
20
21
22
23
# File 'lib/ruactor.rb', line 19

def initialize()
  @queue = Queue.new
  @started = false
  @lock = Mutex.new
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



17
18
19
# File 'lib/ruactor.rb', line 17

def queue
  @queue
end

Instance Method Details

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruactor.rb', line 25

def start
  @lock.synchronize {
    unless started?
      @threads = []
      5.times do
        thread = Thread.new do
          loop do
            obj, method, args, block = @queue.pop
            obj.send method, *args, &block
          end
        end
        @threads << thread
      end
      @started = true
    end
  }
end

#started?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ruactor.rb', line 51

def started?
  @started
end

#stopObject



43
44
45
46
47
48
49
# File 'lib/ruactor.rb', line 43

def stop
  @lock.synchronize {
    @threads.each { |t| t.exit }
    @threads.each { |t| t.join }
    @started = false
  }
end

#stopped?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ruactor.rb', line 55

def stopped?
  !started?
end