Sha256: 62c4b8d5987e3b455170f615f465568111112ef705369ef879d7fe8c710c951d

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

# TITLE:
#
#   Pool
#
# DESCRIPTION:
#
#   Generalized object pool implementation. Implemented as a thread
#   safe stack. Exclusive locking is needed both for push and pop.
#
# AUTHOR:
#
#   - George Moschovitis
#
# LICENSE:
#
#   Copyright (c) 2004 George Moschovitis
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# TODO:
#
#   - Could use the SizedQueue/Queue ?
#


require 'thread'
require 'monitor'

# = Pool
#
# Generalized object pool implementation. Implemented as a thread
# safe stack. Exclusive locking is needed both for push and pop.

class Pool < Array

  include MonitorMixin

  def initialize
    super
    @cv = new_cond()
  end

  # Add, restore an object to the pool.

  def push(obj)
    synchronize do
      super
      @cv.signal()
    end
  end

  # Obtain an object from the pool.

  def pop
    synchronize do
      @cv.wait_while { empty? }
      super
    end
  end

  # Obtains an object, passes it to a block for processing
  # and restores it to the pool.

  def obtain
    result = nil
    begin
      obj = pop()
      result = yield(obj)
    ensure
      push(obj)
    end
    return result
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

# TODO

=begin #testing

=end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
facets-2.0.0 lib/more/facets/pool.rb
facets-2.0.1 lib/more/facets/pool.rb
facets-2.0.2 lib/more/facets/pool.rb
facets-2.0.5 lib/more/facets/pool.rb
facets-2.1.0 lib/more/facets/pool.rb
facets-2.1.2 lib/more/facets/pool.rb
facets-2.1.1 lib/more/facets/pool.rb
facets-2.0.3 lib/more/facets/pool.rb
facets-2.0.4 lib/more/facets/pool.rb
facets-2.3.0 lib/class/facets/pool.rb
facets-2.1.3 lib/more/facets/pool.rb
facets-2.2.1 lib/more/facets/pool.rb
facets-2.2.0 lib/more/facets/pool.rb