Class: Rex::Sync::Event
- Inherits:
-
Object
- Object
- Rex::Sync::Event
- Defined in:
- lib/rex/sync/event.rb
Overview
This class wraps the logical ConditionVariable class to make it an easier to work with interface that is similar to Windows’ synchronization events.
Constant Summary collapse
- Infinite =
10000
Instance Method Summary collapse
-
#initialize(state = false, auto_reset = true, param = nil) ⇒ Event
constructor
Initializes a waitable event.
-
#reset ⇒ Object
Resets the signaled state to false.
-
#set(param = nil) ⇒ Object
(also: #notify)
Sets the event and wakes up anyone who was waiting.
- #wait(t = Infinite) ⇒ Object
Constructor Details
#initialize(state = false, auto_reset = true, param = nil) ⇒ Event
Initializes a waitable event. The state parameter initializes the default state of the event. If auto_reset is true, any calls to set() will automatically reset the event back to an unset state.
21 22 23 24 25 26 27 |
# File 'lib/rex/sync/event.rb', line 21 def initialize(state = false, auto_reset = true, param = nil) self.state = state self.auto_reset = auto_reset self.param = param self.mutex = Mutex.new self.cond = ConditionVariable.new end |
Instance Method Details
#reset ⇒ Object
Resets the signaled state to false.
49 50 51 52 |
# File 'lib/rex/sync/event.rb', line 49 def reset self.param = nil self.state = false end |
#set(param = nil) ⇒ Object Also known as: notify
Sets the event and wakes up anyone who was waiting.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rex/sync/event.rb', line 32 def set(param = nil) self.param = param self.mutex.synchronize { # If this event does not automatically reset its state, # set the state to true if (auto_reset == false) self.state = true end self.cond.broadcast } end |
#wait(t = Infinite) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rex/sync/event.rb', line 71 def wait(t = Infinite) callcc { |ctx| self.mutex.synchronize { ctx.call if (self.state == true) Timeout.timeout(t) { self.cond.wait(self.mutex) } } } return self.param end |