class Procrastinate::Utils::OneTimeFlag

A flag that will allow threads to wait until it is set. Once it is set, it cannot be unset.

Guarantees that this class tries to make:

1) No thread will start waiting for the flag once it is already set. There

no set-hole, meaning that no thread goes to sleep while we're waking up
threads because the flag has been set.

Candidate stdlib classes violate some of these guarantees, here are some candidates:

* ConditionVariable - violates 1)

Public Class Methods

new() click to toggle source
# File lib/procrastinate/utils/one_time_flag.rb, line 16
def initialize
  @waiting   = []
  @waiting_m = Mutex.new
  @set       = false
end

Public Instance Methods

set() click to toggle source

Sets the flag and releases all waiting threads.

# File lib/procrastinate/utils/one_time_flag.rb, line 37
def set
  @set = true
  @waiting_m.synchronize do
    @waiting.each { |t| t.run }
    @waiting = [] # cleanup
  end
end
set?() click to toggle source

Non blocking: Is the flag set?

# File lib/procrastinate/utils/one_time_flag.rb, line 47
def set?
  @set
end
wait() click to toggle source

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

# File lib/procrastinate/utils/one_time_flag.rb, line 24
def wait
  return if set?
  
  @waiting_m.synchronize do
    @waiting << Thread.current
    until set?
      @waiting_m.sleep(0.001) 
    end
  end
end