Class: Pool

Inherits:
Array show all
Includes:
MonitorMixin
Defined in:
lib/mega/pool.rb

Instance Method Summary collapse

Methods included from DupReplaceSnapshotMixin

#restore_snapshot, #take_snapshot

Constructor Details

#initializePool

Returns a new instance of Pool.



46
47
48
49
# File 'lib/mega/pool.rb', line 46

def initialize
  super
  @cv = new_cond()
end

Instance Method Details

#obtainObject

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/mega/pool.rb', line 72

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

#popObject

Obtain an object from the pool.



62
63
64
65
66
67
# File 'lib/mega/pool.rb', line 62

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

#push(obj) ⇒ Object

Add, restore an object to the pool.



53
54
55
56
57
58
# File 'lib/mega/pool.rb', line 53

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