Class: CountingSemaphore
- Inherits:
-
Object
- Object
- CountingSemaphore
- Defined in:
- lib/simrpc/semaphore.rb
Overview
$Id: semaphore.rb,v 1.2 2003/03/15 20:10:10 fukumoto Exp $
Copied unmodified from:
http://www.imasy.or.jp/~fukumoto/ruby/semaphore.rb
Licensed under The Ruby License:
http://raa.ruby-lang.org/project/semaphore/
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 |
# File 'lib/simrpc/semaphore.rb', line 10 def initialize(initvalue = 0) @counter = initvalue @waiting_list = [] end |
Instance Method Details
#exclusive ⇒ Object Also known as: synchronize
46 47 48 49 50 51 |
# File 'lib/simrpc/semaphore.rb', line 46 def exclusive wait yield ensure signal end |
#signal ⇒ Object Also known as: up, V
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/simrpc/semaphore.rb', line 26 def signal Thread.critical = true begin if (@counter += 1) <= 0 t = @waiting_list.shift t.wakeup if t end rescue ThreadError retry end self ensure Thread.critical = false end |
#wait ⇒ Object Also known as: down, P
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/simrpc/semaphore.rb', line 15 def wait Thread.critical = true if (@counter -= 1) < 0 @waiting_list.push(Thread.current) Thread.stop end self ensure Thread.critical = false end |