Class: Datadog::Core::Remote::Component::Barrier
- Inherits:
-
Object
- Object
- Datadog::Core::Remote::Component::Barrier
- Defined in:
- lib/datadog/core/remote/component.rb
Overview
Barrier provides a mechanism to fence execution until a condition happens
Instance Method Summary collapse
-
#initialize(timeout = nil) ⇒ Barrier
constructor
A new instance of Barrier.
-
#lift ⇒ Object
Release all current waiters.
-
#wait_next(timeout = nil) ⇒ Object
Wait for next lift to happen.
-
#wait_once(timeout = nil) ⇒ Object
Wait for first lift to happen, otherwise don’t wait.
Constructor Details
#initialize(timeout = nil) ⇒ Barrier
Returns a new instance of Barrier.
63 64 65 66 67 68 69 |
# File 'lib/datadog/core/remote/component.rb', line 63 def initialize(timeout = nil) @once = false @timeout = timeout @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Method Details
#lift ⇒ Object
Release all current waiters
104 105 106 107 108 109 110 111 112 |
# File 'lib/datadog/core/remote/component.rb', line 104 def lift @mutex.lock @once ||= true @condition.broadcast ensure @mutex.unlock end |
#wait_next(timeout = nil) ⇒ Object
Wait for next lift to happen
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/datadog/core/remote/component.rb', line 92 def wait_next(timeout = nil) @mutex.lock timeout ||= @timeout # rbs/core has a bug, timeout type is incorrectly ?Integer @condition.wait(@mutex, _ = timeout) ensure @mutex.unlock end |
#wait_once(timeout = nil) ⇒ Object
Wait for first lift to happen, otherwise don’t wait
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/datadog/core/remote/component.rb', line 72 def wait_once(timeout = nil) # TTAS (Test and Test-And-Set) optimisation # Since @once only ever goes from false to true, this is semantically valid return if @once begin @mutex.lock return if @once timeout ||= @timeout # rbs/core has a bug, timeout type is incorrectly ?Integer @condition.wait(@mutex, _ = timeout) ensure @mutex.unlock end end |