class Tribe::Mailbox

Public Class Methods

new(pool) click to toggle source
# File lib/tribe/mailbox.rb, line 3
def initialize(pool)
  @pool = pool
  @messages = []
  @alive = true
  @lock = Mutex.new
  @owner_thread = nil
end

Public Instance Methods

alive?() click to toggle source
# File lib/tribe/mailbox.rb, line 59
def alive?
  @lock.synchronize do
    return @alive
  end
end
kill() click to toggle source
# File lib/tribe/mailbox.rb, line 50
def kill
  @lock.synchronize do
    @alive = false
    @messages.clear
  end

  return nil
end
obtain_and_shift() click to toggle source
# File lib/tribe/mailbox.rb, line 22
def obtain_and_shift
  @lock.synchronize do
    return nil unless @alive

    if @owner_thread
      if @owner_thread == Thread.current
        return @messages.shift
      else
        return nil
      end
    else
      @owner_thread = Thread.current
      return @messages.shift
    end
  end
end
push(event, &block) click to toggle source
# File lib/tribe/mailbox.rb, line 11
def push(event, &block)
  @lock.synchronize do
    return nil unless @alive

    @messages.push(event)
    @pool.perform { block.call } unless @owner_thread
  end

  return nil
end
release(&block) click to toggle source
# File lib/tribe/mailbox.rb, line 39
def release(&block)
  @lock.synchronize do
    return nil unless @owner_thread == Thread.current

    @owner_thread = nil
    @pool.perform { block.call } if @alive && @messages.length > 0
  end

  return nil
end