Class: Iarm::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/iarm/timer.rb

Defined Under Namespace

Classes: Poke, Timeout

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



9
10
11
12
13
# File 'lib/iarm/timer.rb', line 9

def initialize
  @poked = false
  @mutex = Mutex.new
  @resource = ConditionVariable.new
end

Class Method Details

.critObject



71
72
73
# File 'lib/iarm/timer.rb', line 71

def crit
  (@mutex ||= Mutex.new).synchronize { yield }
end

.poke(thr) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/iarm/timer.rb', line 49

def poke(thr)
  if(thr)# && thr.stop?)
    thr.raise(Poke.new)
    true
  else
    false
  end
end

.wait(timeout) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/iarm/timer.rb', line 58

def wait(timeout)
  timer = create_timer(timeout)
  yield(true) if block_given?
  Thread.stop
rescue Timeout
  return false
rescue Poke
  return true
ensure
  Thread.kill(timer) if(timer && timer.alive?)
  yield(false) if block_given?
end

Instance Method Details

#poke(timeout = false) ⇒ Object



15
16
17
18
19
20
# File 'lib/iarm/timer.rb', line 15

def poke(timeout=false)
  @mutex.synchronize do 
    @poked = true if timeout
    @resource.signal
  end
end

#wait(timeout) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iarm/timer.rb', line 22

def wait(timeout)
  timer = create_timer(timeout)
  should_wait = true
  should_wait = yield(true) if block_given?
  if should_wait
    @mutex.synchronize do 
      @resource.wait(@mutex) 
    end
  end
  @poked
rescue Timeout
ensure
  Thread.kill(timer) if(timer && timer.alive?)
  yield(false)  if block_given?
end