Class: Concurrent::Event

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

Instance Method Summary collapse

Constructor Details

#initializeEvent

Returns a new instance of Event.



8
9
10
11
12
# File 'lib/concurrent/event.rb', line 8

def initialize
  @set = false
  @mutex = Mutex.new
  @waiters = []
end

Instance Method Details

#resetObject



27
28
29
30
# File 'lib/concurrent/event.rb', line 27

def reset
  @mutex.synchronize { @set = false; @waiters.clear }
  return true
end

#setObject



18
19
20
21
22
23
24
25
# File 'lib/concurrent/event.rb', line 18

def set
  return true if set?
  @mutex.synchronize do
    @set = true
    @waiters.each {|waiter| waiter.run if waiter.status == 'sleep'}
  end
  return true
end

#set?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/concurrent/event.rb', line 14

def set?
  return @set == true
end

#wait(timeout = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/concurrent/event.rb', line 32

def wait(timeout = nil)
  return true if set?

  @mutex.synchronize { @waiters << Thread.current }
  return true if set? # if event was set while waiting for mutex

  if timeout.nil?
    slept = sleep
  else
    slept = sleep(timeout)
  end
rescue
  # let it fail
ensure
  @mutex.synchronize { @waiters.delete(Thread.current) }
  return set?
end