Tasks with a Thread backend
Run the given block within a task
# File lib/celluloid/task/threaded.rb, line 6 def initialize(type, meta) @resume_queue = Queue.new @exception_queue = Queue.new @yield_mutex = Mutex.new @yield_cond = ConditionVariable.new @thread = nil super end
# File lib/celluloid/task/threaded.rb, line 54 def backtrace @thread.backtrace end
# File lib/celluloid/task/threaded.rb, line 16 def create # TODO: move this to ActorSystem#get_thread (ThreadHandle inside Group::Pool) thread = Internals::ThreadHandle.new(Thread.current[:celluloid_actor_system], :task) do begin ex = @resume_queue.pop fail ex if ex.is_a?(TaskTerminated) yield rescue ::Exception => ex @exception_queue << ex ensure @yield_mutex.synchronize do @yield_cond.signal end end end @thread = thread end
# File lib/celluloid/task/threaded.rb, line 42 def deliver(value) fail DeadTaskError, "cannot resume a dead task" unless @thread.alive? @yield_mutex.synchronize do @resume_queue.push(value) @yield_cond.wait(@yield_mutex) fail @exception_queue.pop while @exception_queue.size > 0 end rescue ThreadError raise DeadTaskError, "cannot resume a dead task" end
# File lib/celluloid/task/threaded.rb, line 35 def signal @yield_mutex.synchronize do @yield_cond.signal end @resume_queue.pop end