Class: CountingSemaphore
- Inherits:
-
Object
- Object
- CountingSemaphore
- Defined in:
- lib/semaphore.rb
Overview
semaphore.rb Counting semaphore implementation for Ruby 1.9.3 Version: 0.0.1 Author: Luis Doubrava, Rob van Aarle Based on semaphore.rb,v 1.2 2003/03/15 20:10:10 fukumoto Exp
Instance Method Summary collapse
- #exclusive ⇒ Object (also: #synchronize)
-
#initialize(initvalue = 0) ⇒ CountingSemaphore
constructor
A new instance of CountingSemaphore.
- #signal ⇒ Object (also: #up, #V)
- #wait ⇒ Object (also: #down, #P)
Constructor Details
#initialize(initvalue = 0) ⇒ CountingSemaphore
Returns a new instance of CountingSemaphore.
10 11 12 13 14 15 |
# File 'lib/semaphore.rb', line 10 def initialize(initvalue = 0) @counter = initvalue @waiting_thread = nil @counter_mutex = Mutex.new @queue_mutex = Mutex.new end |
Instance Method Details
#exclusive ⇒ Object Also known as: synchronize
52 53 54 55 56 57 |
# File 'lib/semaphore.rb', line 52 def exclusive wait yield ensure signal end |
#signal ⇒ Object Also known as: up, V
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/semaphore.rb', line 31 def signal @counter_mutex.synchronize do begin if (@counter += 1) <= 0 if @waiting_thread @waiting_thread.wakeup @waiting_thread = nil end end rescue ThreadError retry end self end end |
#wait ⇒ Object Also known as: down, P
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/semaphore.rb', line 17 def wait @queue_mutex.synchronize do stop_thread = false @counter_mutex.synchronize do if (@counter -= 1) < 0 @waiting_thread = Thread.current stop_thread = true end self end Thread.stop if stop_thread end end |