Class: Jabber::Semaphore
- Inherits:
-
Object
- Object
- Jabber::Semaphore
- Defined in:
- lib/xmpp4r/semaphore.rb
Overview
This class implements semaphore for threads synchronization.
Instance Method Summary (collapse)
-
- (Semaphore) initialize(val = 0)
constructor
Initialize new semaphore.
-
- (Object) run
Unlocks guarded section, increments number of free tickets.
-
- (Object) wait
Waits until are available some free tickets.
Constructor Details
- (Semaphore) initialize(val = 0)
Initialize new semaphore
val |
|
14 15 16 17 18 |
# File 'lib/xmpp4r/semaphore.rb', line 14 def initialize(val=0) @tickets = val @lock = Mutex.new @cond = ConditionVariable.new end |
Instance Method Details
- (Object) run
Unlocks guarded section, increments number of free tickets
31 32 33 34 35 36 |
# File 'lib/xmpp4r/semaphore.rb', line 31 def run @lock.synchronize { @tickets += 1 @cond.signal } end |
- (Object) wait
Waits until are available some free tickets
22 23 24 25 26 27 |
# File 'lib/xmpp4r/semaphore.rb', line 22 def wait @lock.synchronize { @cond.wait(@lock) while !(@tickets > 0) @tickets -= 1 } end |