lib/journeta/asynchronous.rb in journeta-0.0.2 vs lib/journeta/asynchronous.rb in journeta-0.0.3

- old
+ new

@@ -1,34 +1,65 @@ -require 'journeta/logger' +# Copyright 2007, OpenRain, LLC. All rights reserved. -module Journeta - class Asynchronous - - include Logger - - attr_accessor :thread, :engine +require 'journeta/logger' - def initialize(engine) - @engine = engine +module Journeta + + class Asynchronous + + include Logger + + attr_accessor :thread, :engine + + + + def initialize(engine) + @engine = engine + @thread_lock = Mutex.new + @thread = nil + end + + + + # Start the +Thread+ for this instance, iff not already running. + def start + @thread_lock.synchronize do + if @thread + # Do not restart it. + else + putsd "Creating asynchronous thread for I/O: #{self.class.to_s}." + @thread = Thread.new { + go + } + end end - - def start - putsd "Creating asynchronous thread for I/O: #{self.class.to_s}." - @thread = Thread.new { - go # @engine - } + end + + # This method is intentionally not present because it would be of no real value. By the time a boolean is returned, the instance could be in a totally different state. + # def started + # stopped = true + # @thread_lock.synchronize do + # stopped = @thread.nil? + # end + # return stopped + # end + + # Stop the +Thread+ associated with this instance, iff not already stopped. + def stop + @thread_lock.synchronize do + if @thread + Thread.kill(@thread) + @thread.join + @thread = nil + end end - - def stop - Thread.kill(@thread) - @thread.join - @thread = nil - end - - def go #(engine) - raise NotImplementedException - end - - end - + end + + # Abstract thread logic method which must be implemented by the sub-class. + def go + raise NotImplementedException + end + + end + end