Skip to content

Commit

Permalink
Factor join and kill into class methods of Actor
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Jul 24, 2012
1 parent b44ff91 commit f28f593
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
15 changes: 15 additions & 0 deletions lib/celluloid/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ def monitoring?(actor)
def linked_to?(actor)
monitoring?(actor) && Thread.current[:actor].links.include?(actor)
end

# Forcibly kill a given actor
def kill(actor)
actor.thread.kill
begin
actor.mailbox.shutdown
rescue DeadActorError
end
end

# Wait for an actor to terminate
def join(actor)
actor.thread.join
actor
end
end

# Wrap the given subject with an Actor
Expand Down
12 changes: 4 additions & 8 deletions lib/celluloid/actor_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Celluloid
# dispatches calls and casts to normal Ruby objects which are running inside
# of their own threads.
class ActorProxy
attr_reader :mailbox
attr_reader :mailbox, :thread

def initialize(actor)
@mailbox, @thread, @klass = actor.mailbox, actor.thread, actor.subject.class.to_s
Expand Down Expand Up @@ -65,7 +65,7 @@ def future(method_name, *args, &block)
# Terminate the associated actor
def terminate
terminate!
join
Actor.join(self)
nil
end

Expand All @@ -77,16 +77,12 @@ def terminate!

# Forcibly kill a given actor
def kill
@thread.kill
begin
@mailbox.shutdown
rescue DeadActorError
end
Actor.kill(self)
end

# Wait for an actor to terminate
def join
@thread.join
Actor.join(self)
end

# method_missing black magic to call bang predicate methods asynchronously
Expand Down
3 changes: 2 additions & 1 deletion lib/celluloid/thread_handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ def alive?
# Forcibly kill the thread
def kill
!!@mutex.synchronize { @thread.kill if @thread }
self
end

# Join to a running thread, blocking until it terminates
def join
@mutex.synchronize { @join.wait(@mutex) if @thread }
nil
self
end
end
end

0 comments on commit f28f593

Please sign in to comment.