Class: ApplicationInsights::Channel::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/application_insights/channel/event.rb

Overview

An event class that allows simple cross-thread signalling.

An object of this type managers an internal flag that can be set to true via the #set method and reset via the #clear method. Calling the #wait method will block until the flag is set to true.

Examples:

require 'application_insights'
require 'thread'
event = ApplicationInsights::Channel::Event.new
Thread.new do
  sleep 1
  event.set
end
puts 'Main screen turn on.'
result = event.wait
puts 'All your base are belong to us.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvent

Initializes a new instance of the class.



25
26
27
28
29
# File 'lib/application_insights/channel/event.rb', line 25

def initialize
  @mutex = Mutex.new
  @condition_variable = ConditionVariable.new
  @signal = false
end

Instance Attribute Details

#signalBoolean (readonly)

The signal value for this object. Note that the value of this property is not synchronized with respect to #set and #clear meaning that it could return false positives or negatives.

Returns:

  • (Boolean)

    the signal value.



35
36
37
# File 'lib/application_insights/channel/event.rb', line 35

def signal
  @signal
end

Instance Method Details

#clearObject

Sets the internal flag to false.



47
48
49
50
51
# File 'lib/application_insights/channel/event.rb', line 47

def clear
  @mutex.synchronize do
    @signal = false
  end
end

#setObject

Sets the internal flag to true. Calling this method will also cause all waiting threads to awaken.



39
40
41
42
43
44
# File 'lib/application_insights/channel/event.rb', line 39

def set
  @mutex.synchronize do
    @signal = true
    @condition_variable.broadcast
  end
end

#wait(timeout = nil) ⇒ Boolean

Calling this method will block until the internal flag is set to true. If the flag is set to true before calling this method, we will return immediately. If the timeout parameter is specified, the method will unblock after the specified number of seconds.

Parameters:

  • timeout (Fixnum) (defaults to: nil)

    the timeout for the operation in seconds.

Returns:

  • (Boolean)

    the value of the internal flag on exit.



59
60
61
62
63
64
65
# File 'lib/application_insights/channel/event.rb', line 59

def wait(timeout=nil)
  @mutex.synchronize do
    @condition_variable.wait(@mutex, timeout) unless @signal
  end

  @signal
end