123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::ConnectionAdapters::ConnectionPool::BiasableQueue::BiasedConditionVariable

Do not use. This class is for internal use only.

Class Method Summary

Instance Method Summary

Constructor Details

.new(lock, other_cond, preferred_thread) ⇒ BiasedConditionVariable

semantics of condition variables guarantee that #broadcast, #broadcast_on_biased, #signal and #wait methods are only called while holding a lock

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb', line 142

def initialize(lock, other_cond, preferred_thread)
  @real_cond = lock.new_cond
  @other_cond = other_cond
  @preferred_thread = preferred_thread
  @num_waiting_on_real_cond = 0
end

Instance Method Details

#broadcast

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb', line 149

def broadcast
  broadcast_on_biased
  @other_cond.broadcast
end

#broadcast_on_biased

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb', line 154

def broadcast_on_biased
  @num_waiting_on_real_cond = 0
  @real_cond.broadcast
end

#signal

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb', line 159

def signal
  if @num_waiting_on_real_cond > 0
    @num_waiting_on_real_cond -= 1
    @real_cond
  else
    @other_cond
  end.signal
end

#wait(timeout)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb', line 168

def wait(timeout)
  if Thread.current == @preferred_thread
    @num_waiting_on_real_cond += 1
    @real_cond
  else
    @other_cond
  end.wait(timeout)
end