Class: Concurrent::Event
- Inherits:
-
Synchronization::LockableObject
- Object
- Synchronization::LockableObject
- Concurrent::Event
- Defined in:
- lib/concurrent/atomic/event.rb
Overview
Old school kernel-style event reminiscent of Win32 programming in C++.
When an ‘Event` is created it is in the `unset` state. Threads can choose to `#wait` on the event, blocking until released by another thread. When one thread wants to alert all blocking threads it calls the `#set` method which will then wake up all listeners. Once an `Event` has been set it remains set. New threads calling `#wait` will return immediately. An `Event` may be `#reset` at any time once it has been set.
Instance Method Summary collapse
-
#initialize ⇒ Event
constructor
Creates a new ‘Event` in the unset state.
-
#reset ⇒ Boolean
Reset a previously set event back to the ‘unset` state.
-
#set ⇒ Boolean
Trigger the event, setting the state to ‘set` and releasing all threads waiting on the event.
-
#set? ⇒ Boolean
Is the object in the set state?.
- #try? ⇒ Boolean
-
#wait(timeout = nil) ⇒ Boolean
Wait a given number of seconds for the ‘Event` to be set by another thread.
Constructor Details
#initialize ⇒ Event
Creates a new ‘Event` in the unset state. Threads calling `#wait` on the `Event` will block.
20 21 22 23 |
# File 'lib/concurrent/atomic/event.rb', line 20 def initialize super synchronize { ns_initialize } end |
Instance Method Details
#reset ⇒ Boolean
Reset a previously set event back to the ‘unset` state. Has no effect if the `Event` has not yet been set.
48 49 50 51 52 53 54 55 56 |
# File 'lib/concurrent/atomic/event.rb', line 48 def reset synchronize do if @set @set = false @iteration +=1 end true end end |
#set ⇒ Boolean
Trigger the event, setting the state to ‘set` and releasing all threads waiting on the event. Has no effect if the `Event` has already been set.
36 37 38 |
# File 'lib/concurrent/atomic/event.rb', line 36 def set synchronize { ns_set } end |
#set? ⇒ Boolean
Is the object in the set state?
28 29 30 |
# File 'lib/concurrent/atomic/event.rb', line 28 def set? synchronize { @set } end |
#try? ⇒ Boolean
40 41 42 |
# File 'lib/concurrent/atomic/event.rb', line 40 def try? synchronize { @set ? false : ns_set } end |
#wait(timeout = nil) ⇒ Boolean
Wait a given number of seconds for the ‘Event` to be set by another thread. Will wait forever when no `timeout` value is given. Returns immediately if the `Event` has already been set.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/concurrent/atomic/event.rb', line 63 def wait(timeout = nil) synchronize do unless @set iteration = @iteration ns_wait_until(timeout) { iteration < @iteration || @set } else true end end end |