Parent

Namespace

Class/Module Index [+]

Quicksearch

Celluloid::Future

Celluloid::Future objects allow methods and blocks to run in the background, their values requested later

Attributes

address[R]

Public Class Methods

new(*args, &block) click to toggle source
# File lib/celluloid/future.rb, line 7
def self.new(*args, &block)
  return super unless block

  future = new
  Celluloid.internal_pool.get do
    Thread.current.role = :future
    begin
      call = SyncCall.new(future, :call, args)
      call.dispatch(block)
    rescue
      # Exceptions in blocks will get raised when the value is retrieved
    end
  end
  future
end
new() click to toggle source
# File lib/celluloid/future.rb, line 25
def initialize
  @address = Celluloid.uuid
  @mutex = Mutex.new
  @ready = false
  @result = nil
  @forwards = nil
end

Public Instance Methods

<<(value) click to toggle source
Alias for: signal
call(timeout = nil) click to toggle source
Alias for: value
ready?() click to toggle source

Check if this future has a value yet

# File lib/celluloid/future.rb, line 34
def ready?
  @ready
end
signal(value) click to toggle source

Signal this future with the given result value

# File lib/celluloid/future.rb, line 77
def signal(value)
  result = Result.new(value, self)

  @mutex.synchronize do
    raise "the future has already happened!" if @ready

    if @forwards
      @forwards.is_a?(Array) ? @forwards.each { |f| f << result } : @forwards << result
    end

    @result = result
    @ready = true
  end
end
Also aliased as: <<
value(timeout = nil) click to toggle source

Obtain the value for this Future

# File lib/celluloid/future.rb, line 39
def value(timeout = nil)
  ready = result = nil

  begin
    @mutex.lock

    if @ready
      ready = true
      result = @result
    else
      case @forwards
      when Array
        @forwards << Celluloid.mailbox
      when NilClass
        @forwards = Celluloid.mailbox
      else
        @forwards = [@forwards, Celluloid.mailbox]
      end
    end
  ensure
    @mutex.unlock
  end

  unless ready
    result = Celluloid.receive(timeout) do |msg|
      msg.is_a?(Future::Result) && msg.future == self
    end
  end

  if result
    result.value
  else
    raise TimeoutError, "Timed out"
  end
end
Also aliased as: call

[Validate]

Generated with the Darkfish Rdoc Generator 2.