Parent

Namespace

Actor

Constants

ANY

Public Class Methods

[](name) click to toggle source
Alias for: lookup
[]=(name, actor) click to toggle source
Alias for: register
check_for_interrupt() click to toggle source

Polls for exit notifications

# File lib/girl_friday/actor.rb, line 92
def check_for_interrupt
  current._check_for_interrupt
  self
end
current() click to toggle source
# File lib/girl_friday/actor.rb, line 57
def current
  Thread.current[:__current_actor__] ||= private_new
end
lookup(name) click to toggle source

Lookup a locally named service

# File lib/girl_friday/actor.rb, line 152
def lookup(name)
  raise ArgumentError, "name must be a symbol" unless Symbol === name
  @@registered_lock.pop
  begin
    @@registered[name]
  ensure
    @@registered_lock << nil
  end
end
Also aliased as: []
new(*args, &block) click to toggle source
Also aliased as: private_new
Alias for: spawn
new() click to toggle source
# File lib/girl_friday/actor.rb, line 193
def initialize
  @lock = Queue.new

  @filter = nil
  @ready = Queue.new
  @action = nil
  @message = nil

  @mailbox = []
  @interrupts = []
  @links = []
  @alive = true
  @exit_reason = nil
  @trap_exit = false
  @thread = Thread.current

  @lock << nil

  if block_given?
    watchdog { yield self }
  else
    Thread.new { watchdog { @thread.join } }
  end
end
private_new(*args, &block) click to toggle source
Alias for: new
receive() click to toggle source

Waits until a matching message is received in the current actor's mailbox, and executes the appropriate action. May be interrupted by exit notifications.

# File lib/girl_friday/actor.rb, line 100
def receive #:yields: filter
  filter = Filter.new
  if block_given?
    yield filter
  else
    filter.when(ANY) { |m| m }
  end
  current._receive(filter)
end
register(name, actor) click to toggle source

Register an Actor locally as a named service

# File lib/girl_friday/actor.rb, line 164
def register(name, actor)
  raise ArgumentError, "name must be a symbol" unless Symbol === name
  unless actor.nil? or actor.is_a?(Actor)
    raise ArgumentError, "only actors may be registered"
  end

  @@registered_lock.pop
  begin
    if actor.nil?
      @@registered.delete(name)
    else
      @@registered[name] = actor
    end
  ensure
    @@registered_lock << nil
  end
end
Also aliased as: []=
send_exit(recipient, reason) click to toggle source

Send a "fake" exit notification to another actor, as if the current actor had exited with reason

# File lib/girl_friday/actor.rb, line 112
def send_exit(recipient, reason)
  recipient.notify_exited(current, reason)
  self
end
spawn(*args, &block) click to toggle source

Spawn a new Actor that will run in its own thread

# File lib/girl_friday/actor.rb, line 62
def spawn(*args, &block)
  raise ArgumentError, "no block given" unless block
  spawned = Queue.new
  Thread.new do
    private_new do |actor|
      Thread.current[:__current_actor__] = actor
      spawned << actor
      block.call(*args)
    end
  end
  spawned.pop
end
Also aliased as: new
trap_exit() click to toggle source

Is the Actor trapping exit?

# File lib/girl_friday/actor.rb, line 146
def trap_exit
  current._trap_exit
end
Also aliased as: trap_exit?
trap_exit=(value) click to toggle source

Actors trapping exit do not die when an error occurs in an Actor they are linked to. Instead the exit message is sent to their regular mailbox in the form [:exit, actor, reason]. This allows certain Actors to supervise sets of others and restart them in the event of an error. Setting the trap flag may be interrupted by pending exit notifications.

# File lib/girl_friday/actor.rb, line 140
def trap_exit=(value)
  current._trap_exit = value
  self
end
trap_exit?() click to toggle source
Alias for: trap_exit

Public Instance Methods

<<(message) click to toggle source
Alias for: send
notify_exited(actor, reason) click to toggle source

Notify this actor that one of the Actors it's linked to has exited; this is not intended to be used directly except by actor implementations. Most users will want to use Actor.send_exit instead.

# File lib/girl_friday/actor.rb, line 352
def notify_exited(actor, reason)
  exit_message = nil
  @lock.pop
  begin
    return self unless @alive
    @links.delete(actor)
    if @trap_exit
      exit_message = DeadActorError.new(actor, reason)
    elsif reason
      @interrupts << DeadActorError.new(actor, reason)
      if @filter
        @filter = nil
        @ready << nil
      end
    end
  ensure
    @lock << nil
  end
  send exit_message if exit_message
  self
end
send(message) click to toggle source
# File lib/girl_friday/actor.rb, line 218
def send(message)
  @lock.pop
  begin
    return self unless @alive
    if @filter
      @action = @filter.action_for(message)
      if @action
        @filter = nil
        @message = message
        @ready << nil
      else
        @mailbox << message
      end
    else
      @mailbox << message
    end
  ensure
    @lock << nil
  end
  self
end
Also aliased as: <<

[Validate]

Generated with the Darkfish Rdoc Generator 2.