Class: Concurrent::Event
- Inherits:
-
Object
- Object
- Concurrent::Event
- Defined in:
- lib/concurrent/event.rb
Instance Method Summary collapse
-
#initialize ⇒ Event
constructor
A new instance of Event.
- #reset ⇒ Object
- #set ⇒ Object
- #set? ⇒ Boolean
- #wait(timeout = nil) ⇒ Object
Constructor Details
#initialize ⇒ Event
Returns a new instance of Event.
8 9 10 11 12 |
# File 'lib/concurrent/event.rb', line 8 def initialize @set = false @mutex = Mutex.new @waiters = [] end |
Instance Method Details
#reset ⇒ Object
27 28 29 30 |
# File 'lib/concurrent/event.rb', line 27 def reset @mutex.synchronize { @set = false; @waiters.clear } return true end |
#set ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/concurrent/event.rb', line 18 def set return true if set? @mutex.synchronize do @set = true @waiters.each {|waiter| waiter.run if waiter.status == 'sleep'} end return true end |
#set? ⇒ Boolean
14 15 16 |
# File 'lib/concurrent/event.rb', line 14 def set? return @set == true end |
#wait(timeout = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/concurrent/event.rb', line 32 def wait(timeout = nil) return true if set? @mutex.synchronize { @waiters << Thread.current } return true if set? # if event was set while waiting for mutex if timeout.nil? slept = sleep else slept = sleep(timeout) end rescue # let it fail ensure @mutex.synchronize { @waiters.delete(Thread.current) } return set? end |