Sha256: 05e0e2dd1f66e1dbd26a23cdebe7aaf2b5476fd316d09e18c9ed5b061c3536fb

Contents?: true

Size: 1.73 KB

Versions: 43

Compression:

Stored size: 1.73 KB

Contents

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

	Infinite = 10000

	#
	# 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.
	#
	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

	#
	# Sets the event and wakes up anyone who was waiting.
	#
	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

	#
	# Resets the signaled state to false.
	#
	def reset
		self.param = nil
		self.state = false
	end

	#
	# Alias notify with set.
	#
	alias notify set

	#
	# Waits for the event to become signaled.  Timeout is measured in
	# seconds.  Raises TimeoutError if the condition does not become signaled.
	#

	begin
		# XXX: we need to replace this code
		#      continuations slow down YARV
		require "continuation" if not defined? callcc
	rescue ::LoadError
	end

	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

protected

	attr_accessor :state, :auto_reset # :nodoc:
	attr_accessor :param, :mutex, :cond # :nodoc:

end

end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
librex-0.0.65 lib/rex/sync/event.rb
librex-0.0.63 lib/rex/sync/event.rb
librex-0.0.54 lib/rex/sync/event.rb
librex-0.0.53 lib/rex/sync/event.rb
librex-0.0.52 lib/rex/sync/event.rb
librex-0.0.51 lib/rex/sync/event.rb
librex-0.0.50 lib/rex/sync/event.rb
librex-0.0.49 lib/rex/sync/event.rb
librex-0.0.48 lib/rex/sync/event.rb
librex-0.0.47 lib/rex/sync/event.rb
librex-0.0.46 lib/rex/sync/event.rb
librex-0.0.44 lib/rex/sync/event.rb
librex-0.0.43 lib/rex/sync/event.rb
librex-0.0.42 lib/rex/sync/event.rb
librex-0.0.41 lib/rex/sync/event.rb
librex-0.0.40 lib/rex/sync/event.rb
librex-0.0.39 lib/rex/sync/event.rb
librex-0.0.38 lib/rex/sync/event.rb
librex-0.0.37 lib/rex/sync/event.rb
librex-0.0.36 lib/rex/sync/event.rb