class MultipleMan::ChannelMaintenance::GC

Attributes

executor[R]
queue[R]
reaper[R]
sweeper_thread[R]

Public Class Methods

finalizer(thread_id, channel, queue, reaper) click to toggle source
# File lib/multiple_man/channel_maintenance/gc.rb, line 4
def self.finalizer(thread_id, channel, queue, reaper)
  proc { queue << RemoveCommand.new(thread_id); reaper.push(channel) }
end
new(_, reaper) click to toggle source
# File lib/multiple_man/channel_maintenance/gc.rb, line 8
def initialize(_, reaper)
  @reaper = reaper
  @queue = Queue.new

  @executor = Thread.new do
    channels_by_thread = Hash.new {|h, k| h[k] = [] }
    loop do
      begin
        command = queue.pop
        command.execute(channels_by_thread)
      rescue
        puts "Sweeper died", $!
      end
    end
  end

  @sweeper_thread = Thread.new do
    loop do
      sleep 15
      queue << SweepCommand.new(queue, reaper)
    end
  end
end

Public Instance Methods

push(channel) click to toggle source
# File lib/multiple_man/channel_maintenance/gc.rb, line 32
def push(channel)
  thread_id = Thread.current.object_id

  finalizer = self.class.finalizer(thread_id, channel, queue, reaper)
  ObjectSpace.define_finalizer(Thread.current, finalizer)

  queue << AddCommand.new(thread_id, channel)

  puts "Opened channel #{channel.number}"
  self
end
stop() click to toggle source
# File lib/multiple_man/channel_maintenance/gc.rb, line 44
def stop
  executor.kill
  executor.join

  sweeper_thread.kill
  sweeper_thread.join

  reaper.stop
end