Class: Procrastinate::Utils::OneTimeFlag
- Inherits:
-
Object
- Object
- Procrastinate::Utils::OneTimeFlag
- Defined in:
- lib/procrastinate/utils/one_time_flag.rb,
lib/procrastinate/utils/one_time_flag_ruby18_shim.rb
Instance Method Summary collapse
-
#initialize ⇒ OneTimeFlag
constructor
A new instance of OneTimeFlag.
-
#set ⇒ Object
Sets the flag and releases all waiting threads.
-
#set? ⇒ Boolean
Non blocking: Is the flag set?.
-
#wait ⇒ Object
If the flag is set, does nothing.
Constructor Details
#initialize ⇒ OneTimeFlag
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
#set ⇒ Object
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?
31 32 33 |
# File 'lib/procrastinate/utils/one_time_flag.rb', line 31 def set? @set end |
#wait ⇒ Object
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 |