Module: Vuf::Pool

Defined in:
lib/vuf/pool.rb

Defined Under Namespace

Modules: PoolClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__init__(klass) ⇒ Object

:nodoc:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vuf/pool.rb', line 38

def __init__(klass) # :nodoc:
  klass.instance_eval {
    @pool__instances__ = Queue.new
    @used__instances__ = Hash.new
    @pool__mutex__ = Mutex.new
  }
  
  def klass.instance # :nodoc:
    instance__ = nil
    @pool__mutex__.synchronize do
      instance__ = @used__instances__[Thread.current]
      if instance__.nil? && @pool__instances__.size > 0
        instance__ = @pool__instances__.pop 
      end
      instance__ = new() if instance__.nil?
      @used__instances__[Thread.current] = instance__
    end
    instance__
  end

  def klass.release # :nodoc:
    instance__ = nil
    @pool__mutex__.synchronize do
      instance__ = @used__instances__.delete(Thread.current)
      @pool__instances__.push(instance__) unless instance__.nil?
    end
    !instance__.nil?
  end

  def klass.count
    count = 0
    @pool__mutex__.synchronize do
      count = @pool__instances__.size + @used__instances__.size
    end
    return count
  end

  def klass.running
    count = 0
    @pool__mutex__.synchronize do
      count = @used__instances__.size
    end
    return count
  end
  
  def klass.finalize # :nodoc:
    @pool__mutex__.synchronize do
      until @pool__instances__.empty?
        i = @pool__instances__.pop
        i.finalize if i.respond_to?(:finalize)
      end
    end
  end
  
  klass
end

Instance Method Details

#_dump(depth = -1)) ⇒ Object

By default, do not retain any state when marshalling.



14
15
16
# File 'lib/vuf/pool.rb', line 14

def _dump(depth = -1)
  ''
end

#cloneObject

Raises a TypeError to prevent cloning.

Raises:

  • (TypeError)


4
5
6
# File 'lib/vuf/pool.rb', line 4

def clone
  raise TypeError, "can't clone instance of Pool #{self.class}"
end

#dupObject

Raises a TypeError to prevent duping.

Raises:

  • (TypeError)


9
10
11
# File 'lib/vuf/pool.rb', line 9

def dup
  raise TypeError, "can't dup instance of Pool #{self.class}"
end