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

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/abstract/connection_pool.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(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



213
214
215
216
217
218
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 213

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

#broadcastObject



220
221
222
223
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 220

def broadcast
  broadcast_on_biased
  @other_cond.broadcast
end

#broadcast_on_biasedObject



225
226
227
228
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 225

def broadcast_on_biased
  @num_waiting_on_real_cond = 0
  @real_cond.broadcast
end

#signalObject



230
231
232
233
234
235
236
237
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 230

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) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/active_record/connection_adapters/abstract/connection_pool.rb', line 239

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