Sha256: 3eef43f151fbdf8856c9b06e8c1f8f8cc4fd238949ee6e35939e2df2aaccabee

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# Copyright © 2007 OpenRain, LLC. All rights reserved.
#
# Preston Lee <preston.lee@openrain.com>


require 'journeta/logger'


module Journeta
  
  # See Journeta::Engine
  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
    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
    end
    
    # Abstract thread logic method which must be implemented by the sub-class.
    def go
      raise NotImplementedException
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
journeta-0.1.0 lib/journeta/asynchronous.rb
journeta-0.1.1 lib/journeta/asynchronous.rb