Class: Higgs::Pool
- Inherits:
-
Object
- Object
- Higgs::Pool
- Defined in:
- lib/higgs/thread.rb
Defined Under Namespace
Classes: ShutdownException
Constant Summary collapse
- CVS_ID =
for ident(1)
'$Id: thread.rb 841 2008-12-24 09:23:20Z toki $'
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(size) ⇒ Pool
constructor
A new instance of Pool.
- #restore(obj) ⇒ Object
- #shutdown ⇒ Object
- #transaction ⇒ Object
Constructor Details
#initialize(size) ⇒ Pool
Returns a new instance of Pool.
423 424 425 426 427 428 429 430 431 432 |
# File 'lib/higgs/thread.rb', line 423 def initialize(size) @size = size @running = true @queue = [] @q_lock = Mutex.new @q_cond = ConditionVariable.new @size.times do @queue << yield end end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
434 435 436 |
# File 'lib/higgs/thread.rb', line 434 def size @size end |
Instance Method Details
#fetch ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/higgs/thread.rb', line 436 def fetch @q_lock.synchronize{ loop do unless (@running) then @q_cond.signal # for shutdown raise ShutdownException, 'pool shutdown' end if (@queue.empty?) then @q_cond.wait(@q_lock) else break end end @queue.shift } end |
#restore(obj) ⇒ Object
453 454 455 456 457 458 459 |
# File 'lib/higgs/thread.rb', line 453 def restore(obj) @q_lock.synchronize{ @queue.push(obj) @q_cond.signal } nil end |
#shutdown ⇒ Object
471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/higgs/thread.rb', line 471 def shutdown @size.times do obj = @q_lock.synchronize{ @running = false while (@queue.empty?) @q_cond.wait(@q_lock) end @queue.shift } yield(obj) if block_given? end nil end |
#transaction ⇒ Object
461 462 463 464 465 466 467 468 469 |
# File 'lib/higgs/thread.rb', line 461 def transaction obj = fetch begin r = yield(obj) ensure restore(obj) end r end |