class CZTop::Z85::Pipe::Strategy::Parallel

Uses three threads:

  1. reads from source

  2. encodes/decodes

  3. writes to sink

This might give a performance increase on truly parallel platforms such as Rubinius and JRuby (and multiple CPU cores).

Public Class Methods

new(*) click to toggle source

Initializes the 2 sized queues used.

Calls superclass method CZTop::Z85::Pipe::Strategy::new
# File lib/cztop/z85/pipe.rb, line 128
def initialize(*)
  super
  # @source
  # |
  # V
  @source_queue = SizedQueue.new(20) # limit memory usage
  # |
  # V
  # xcode
  # |
  # V
  @sink_queue   = SizedQueue.new(20) # limit memory usage
  # |
  # V
  # @sink
end

Public Instance Methods

execute() click to toggle source

Runs the algorithm. @raise [void]

# File lib/cztop/z85/pipe.rb, line 148
def execute
  Thread.new { read }
  Thread.new { xcode }
  write
end

Private Instance Methods

read() click to toggle source

Reads all chunks and pushes them into the source queue. Then pushes a nil into the queue. @return [void]

# File lib/cztop/z85/pipe.rb, line 159
def read
  while (chunk = @source.read(@read_sz))
    @source_queue << chunk
  end
  @source_queue << nil
end
write() click to toggle source

Pops all chunks from the sink queue and writes them to the sink. @return [void]

# File lib/cztop/z85/pipe.rb, line 190
def write
  while (chunk = @sink_queue.pop)
    @sink << chunk
  end
end
xcode() click to toggle source

Pops all chunks from the source queue, encodes or decodes them, and pushes the result into the sink queue. Then pushes a nil into the queue. @return [void]

# File lib/cztop/z85/pipe.rb, line 171
def xcode
  # Encode all but the last chunk with pure Z85.
  previous_chunk = nil
  while true
    chunk = @source_queue.pop

    # call @xcode for the trailing nil-chunk as well
    @sink_queue << @xcode.call(chunk, previous_chunk)

    break if chunk.nil?

    previous_chunk = chunk
  end
  @sink_queue << nil
end