2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-16 07:32:11 +00:00
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Sync
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class wraps the logical ConditionVariable class to make it an easier to
|
|
|
|
# work with interface that is similar to Windows' synchronization events.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Event
|
|
|
|
|
2005-11-11 01:27:57 +00:00
|
|
|
Infinite = 10000
|
2005-07-16 07:32:11 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2005-07-16 07:32:11 +00:00
|
|
|
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
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
# Sets the event and wakes up anyone who was waiting.
|
|
|
|
#
|
|
|
|
def set(param = nil)
|
|
|
|
self.param = param
|
|
|
|
|
|
|
|
self.mutex.synchronize {
|
2009-11-02 18:14:57 +00:00
|
|
|
# If this event does not automatically reset its state,
|
2005-07-16 07:32:11 +00:00
|
|
|
# set the state to true
|
|
|
|
if (auto_reset == false)
|
|
|
|
self.state = true
|
|
|
|
end
|
|
|
|
|
|
|
|
self.cond.broadcast
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Resets the signaled state to false.
|
|
|
|
#
|
|
|
|
def reset
|
|
|
|
self.param = nil
|
|
|
|
self.state = false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Alias notify with set.
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
alias notify set
|
|
|
|
|
|
|
|
#
|
2009-11-02 18:14:57 +00:00
|
|
|
# Waits for the event to become signaled. Timeout is measured in
|
2005-07-16 07:32:11 +00:00
|
|
|
# seconds. Raises TimeoutError if the condition does not become signaled.
|
|
|
|
#
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2009-06-02 23:36:58 +00:00
|
|
|
begin
|
|
|
|
# XXX: we need to replace this code
|
|
|
|
# continuations slow down YARV
|
2009-06-25 17:59:51 +00:00
|
|
|
require "continuation" if not defined? callcc
|
2009-06-02 23:36:58 +00:00
|
|
|
rescue ::LoadError
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
def wait(t = Infinite)
|
|
|
|
callcc { |ctx|
|
|
|
|
self.mutex.synchronize {
|
|
|
|
ctx.call if (self.state == true)
|
|
|
|
|
2009-11-02 18:14:57 +00:00
|
|
|
Timeout.timeout(t) {
|
2005-07-16 07:32:11 +00:00
|
|
|
self.cond.wait(self.mutex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.param
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :state, :auto_reset # :nodoc:
|
|
|
|
attr_accessor :param, :mutex, :cond # :nodoc:
|
2005-07-16 07:32:11 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-06-02 23:36:58 +00:00
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|