Sha256: ff91332ba3cc030dbcdc1814e80c973c687d7614073b502635e3171e90e525a0

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

require 'set'
require 'thread'

module Celluloid
  # Thread safe storage of inter-actor links
  class Links
    include Enumerable
    
    def initialize
      @links = Set.new
      @lock  = Mutex.new
    end
    
    def <<(actor)
      @lock.synchronize do
        @links << actor
      end
      actor
    end
    
    def include?(actor)
      @lock.synchronize do
        @links.include? actor
      end
    end
    
    def delete(actor)
      @lock.synchronize do
        @links.delete actor
      end
      actor
    end
    
    def each(&block)
      @lock.synchronize do
        @links.each(&block)
      end
    end
    
    def inspect
      @lock.synchronize do
        links = @links.to_a.map { |l| "#{l.class}:#{l.object_id}" }.join(',')
        "#<Celluloid::Links[#{links}]>"
      end
    end
  end
  
  # Support for linking actors together so they can crash or react to errors
  module Linking
    # Link this actor to another, allowing it to crash or react to errors
    def link(actor)
      actor.notify_link(@proxy)
      self.notify_link(actor)
    end
    
    # Remove links to another actor
    def unlink(actor)
      actor.notify_unlink(@proxy)
      self.notify_unlink(actor)
    end
    
    def notify_link(actor)
      @links << actor
    end
    
    def notify_unlink(actor)
      @links.delete actor
    end
    
    # Is this actor linked to another?
    def linked_to?(actor)
      @links.include? actor
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
celluloid-0.0.3 lib/celluloid/linking.rb
celluloid-0.0.1 lib/celluloid/linking.rb