Class: Procrastinate::Utils::OneTimeFlag

Inherits:
Object
  • Object
show all
Defined in:
lib/procrastinate/utils/one_time_flag.rb,
lib/procrastinate/utils/one_time_flag_ruby18_shim.rb

Instance Method Summary collapse

Constructor Details

#initializeOneTimeFlag

Returns a new instance of OneTimeFlag.



2
3
4
5
6
# File 'lib/procrastinate/utils/one_time_flag.rb', line 2

def initialize
  @waiting   = []
  @waiting_m = Mutex.new
  @set       = false
end

Instance Method Details

#setObject

Sets the flag and releases all waiting threads.



21
22
23
24
25
26
27
# File 'lib/procrastinate/utils/one_time_flag.rb', line 21

def set
  @set = true
  @waiting_m.synchronize do
    @waiting.each { |t| t.run }
    @waiting = [] # cleanup
  end
end

#set?Boolean

Non blocking: Is the flag set?

Returns:

  • (Boolean)


31
32
33
# File 'lib/procrastinate/utils/one_time_flag.rb', line 31

def set?
  @set
end

#waitObject

If the flag is set, does nothing. If it isn’t, it blocks until the flag is set.



10
11
12
13
14
15
16
17
# File 'lib/procrastinate/utils/one_time_flag.rb', line 10

def wait
  return if set?
  
  @waiting_m.synchronize do
    @waiting << Thread.current
    @waiting_m.sleep(0.001) until set?
  end
end