Class: Datadog::Core::Remote::Component::Barrier Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/remote/component.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Barrier provides a mechanism to fence execution until a condition happens

Instance Method Summary collapse

Constructor Details

#initialize(timeout = nil) ⇒ Barrier

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Barrier.



106
107
108
109
110
111
112
# File 'lib/datadog/core/remote/component.rb', line 106

def initialize(timeout = nil)
  @once = false
  @timeout = timeout

  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Method Details

#liftObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Release all current waiters



150
151
152
153
154
155
156
# File 'lib/datadog/core/remote/component.rb', line 150

def lift
  @mutex.synchronize do
    @once ||= true

    @condition.broadcast
  end
end

#wait_once(timeout = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wait for first lift to happen, otherwise don’t wait



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/datadog/core/remote/component.rb', line 115

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 :pass if @once

  begin
    @mutex.lock

    return :pass if @once

    timeout ||= @timeout

    # - starting with Ruby 3.2, ConditionVariable#wait returns nil on
    #   timeout and an integer otherwise
    # - before Ruby 3.2, ConditionVariable returns itself
    # so we have to rely on @once having been set
    if RUBY_VERSION >= '3.2'
      lifted = @condition.wait(@mutex, timeout)
    else
      @condition.wait(@mutex, timeout)
      lifted = @once
    end

    if lifted
      :lift
    else
      @once = true
      :timeout
    end
  ensure
    @mutex.unlock
  end
end