Skip to content

Commit

Permalink
Simplify Future to only contain messaging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
halorgium committed May 9, 2013
1 parent 09125dc commit 7a46161
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 2 additions & 0 deletions examples/simple_pmap.rb
Expand Up @@ -10,3 +10,5 @@ def pmap(&block)
futures.map { |future| future.value }
end
end

p 100.times.pmap {|n| n * 2}
44 changes: 17 additions & 27 deletions lib/celluloid/future.rb
Expand Up @@ -4,38 +4,29 @@ module Celluloid
# Celluloid::Future objects allow methods and blocks to run in the
# background, their values requested later
class Future
def self.new(*args, &block)
return super unless block

future = new
Celluloid.internal_pool.get do
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

attr_reader :address

# Create a future bound to a given receiver, or with a block to compute
def initialize(*args, &block)

def initialize
@address = Celluloid.uuid
@mutex = Mutex.new
@ready = false
@result = nil
@forwards = nil

if block
@call = SyncCall.new(self, :call, args)
Celluloid.internal_pool.get do
begin
@call.dispatch(block)
rescue
# Exceptions in blocks will get raised when the value is retrieved
end
end
else
@call = nil
end
end

# Execute the given method in future context
def execute(receiver, method, args, block)
@mutex.synchronize do
raise "already calling" if @call
@call = SyncCall.new(self, method, args, block)
end

receiver << @call
end

# Check if this future has a value yet
Expand All @@ -49,7 +40,6 @@ def value(timeout = nil)

begin
@mutex.lock
raise "no call requested" unless @call

if @ready
ready = true
Expand Down
10 changes: 9 additions & 1 deletion lib/celluloid/proxies/future_proxy.rb
Expand Up @@ -17,8 +17,16 @@ def method_missing(meth, *args, &block)
# FIXME: nicer exception
raise "Cannot use blocks with futures yet"
end

future = Future.new
future.execute(@mailbox, meth, args, block)
call = SyncCall.new(future, meth, args, block)

begin
@mailbox << call
rescue MailboxError
raise DeadActorError, "attempted to call a dead actor"
end

future
end
end
Expand Down

0 comments on commit 7a46161

Please sign in to comment.