Class: CountingSemaphore
- Inherits:
-
Object
- Object
- CountingSemaphore
- Defined in:
- lib/motel/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
Originally 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.
11 12 13 14 |
# File 'lib/motel/semaphore.rb', line 11 def initialize(initvalue = 0) @counter = initvalue @waiting_list = [] end |
Instance Method Details
#exclusive ⇒ Object Also known as: synchronize
47 48 49 50 51 52 |
# File 'lib/motel/semaphore.rb', line 47 def exclusive wait yield ensure signal end |
#signal ⇒ Object Also known as: up, V
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/motel/semaphore.rb', line 27 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
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/motel/semaphore.rb', line 16 def wait Thread.critical = true if (@counter -= 1) < 0 @waiting_list.push(Thread.current) Thread.stop end self ensure Thread.critical = false end |