Class: MutexPool
- Inherits:
-
Object
- Object
- MutexPool
- Defined in:
- lib/queueing_rabbit/misc/mutex_pool.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#initialize(size = 1) ⇒ MutexPool
constructor
A new instance of MutexPool.
- #lock ⇒ Object
- #synchronize(&block) ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(size = 1) ⇒ MutexPool
Returns a new instance of MutexPool.
4 5 6 7 8 9 10 11 |
# File 'lib/queueing_rabbit/misc/mutex_pool.rb', line 4 def initialize(size = 1) @pool = Queue.new @size = size @pop_mutex = Mutex.new @lock_mutex = Mutex.new @locked_pool = [] @size.times { @pool << Mutex.new } end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
2 3 4 |
# File 'lib/queueing_rabbit/misc/mutex_pool.rb', line 2 def size @size end |
Instance Method Details
#lock ⇒ Object
20 21 22 23 24 |
# File 'lib/queueing_rabbit/misc/mutex_pool.rb', line 20 def lock @lock_mutex.lock @pop_mutex.lock @locked_pool = @size.times.map { |i| @pool.pop } end |
#synchronize(&block) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/queueing_rabbit/misc/mutex_pool.rb', line 13 def synchronize(&block) mutex = @pop_mutex.synchronize { @pool.pop } mutex.synchronize(&block) ensure @pool << mutex if mutex end |
#unlock ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/queueing_rabbit/misc/mutex_pool.rb', line 26 def unlock raise ThreadError, 'The pool is not locked' unless @lock_mutex.locked? @locked_pool.each { |m| @pool << m } @locked_pool = [] @pop_mutex.unlock @lock_mutex.unlock end |