Sha256: 6c25056acd21b5779fea731038267f7b9a66cc3ad2b56244450d88b439989bd5

Contents?: true

Size: 1.65 KB

Versions: 45

Compression:

Stored size: 1.65 KB

Contents

# -*- coding: binary -*-
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.
  #
  def wait(t = Infinite)
    self.mutex.synchronize {
      break 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

45 entries across 45 versions & 4 rubygems

Version Path
rex-core-0.1.31 lib/rex/sync/event.rb
rex-core-0.1.30 lib/rex/sync/event.rb
rex-core-0.1.29 lib/rex/sync/event.rb
rex-core-0.1.28 lib/rex/sync/event.rb
rex-core-0.1.27 lib/rex/sync/event.rb
rex-core-0.1.26 lib/rex/sync/event.rb
rex-core-0.1.25 lib/rex/sync/event.rb
rex-core-0.1.24 lib/rex/sync/event.rb
rex-core-0.1.23 lib/rex/sync/event.rb
rex-core-0.1.22 lib/rex/sync/event.rb
rex-core-0.1.21 lib/rex/sync/event.rb
rex-core-0.1.20 lib/rex/sync/event.rb
rex-core-0.1.19 lib/rex/sync/event.rb
rex-core-0.1.18 lib/rex/sync/event.rb
rex-core-0.1.17 lib/rex/sync/event.rb
rex-core-0.1.16 lib/rex/sync/event.rb
rex-core-0.1.15 lib/rex/sync/event.rb
rex-core-0.1.14 lib/rex/sync/event.rb
rex-2.0.13 lib/rex/sync/event.rb
rex-core-0.1.13 lib/rex/sync/event.rb