Sha256: b796662b8aea14347b7dbf4905bd8142c8cee4669e13dcbd41cb2d1cdcbde882

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

#--
# Pool
#
# Copyright (c) 2004-2005 Navel, all rights reserved.
#
# 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.
#
# ==========================================================================
# Revision History ::
# --------------------------------------------------------------------------
#  05.04.11  Trans   * Ported.
# ==========================================================================
#
# INVESTIGATE Could use the SizedQueue/Queue.
#
#++

#:title: Pool
#
# Generalized object pool implementation. Implemented as a thread 
# safe stack. Exclusive locking is needed both for push and pop. 
#
# == Usage
#
#   TODO
#
# == Author(s)
#
# * George Moschovitis  <gm@navel.gr>
#

require 'thread'
require 'monitor'

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

7 entries across 7 versions & 1 rubygems

Version Path
facets-1.0.3 packages/more/lib/facet/pool.rb
facets-1.2.0 lib/facets/more/pool.rb
facets-1.2.1 lib/facets/more/pool.rb
facets-1.3.0 lib/facets/more/pool.rb
facets-1.3.1 lib/facets/more/pool.rb
facets-1.3.2 lib/facets/more/pool.rb
facets-1.3.3 lib/facets/more/pool.rb