Class: CountingSemaphore
Overview
:title: Semaphore (CountingSemaphore)
Technically a semaphore is simply an integer variable which has an execution queue associated with it.
Usage
s = CountingSemaphore.new
TODO
Author(s)
-
fukumoto
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.
42 43 44 45 |
# File 'lib/mega/semaphore.rb', line 42 def initialize(initvalue = 0) @counter = initvalue @waiting_list = [] end |
Instance Method Details
#exclusive ⇒ Object Also known as: synchronize
78 79 80 81 82 83 |
# File 'lib/mega/semaphore.rb', line 78 def exclusive wait yield ensure signal end |
#signal ⇒ Object Also known as: up, v
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mega/semaphore.rb', line 58 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
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mega/semaphore.rb', line 47 def wait Thread.critical = true if (@counter -= 1) < 0 @waiting_list.push(Thread.current) Thread.stop end self ensure Thread.critical = false end |