module Concurrent::ThreadSafe::Util::CheapLockable

def cheap_broadcast

def cheap_broadcast
  waiters = @waiters ||= []
  waiters.shift << true until waiters.empty?
  self
end

def cheap_broadcast

def cheap_broadcast
  JRuby.reference0(self).notify_all
end

def cheap_broadcast

Must only be called in +cheap_broadcast+'s block.
Wakes up all threads waiting for this object's +cheap_synchronize+ lock.
def cheap_broadcast
  if conditional_variable = @conditional_variable
    conditional_variable.broadcast
  end
end

def cheap_synchronize

Making use of the Rubinius' ability to lock via object headers to avoid the overhead of the extra Mutex objects.
def cheap_synchronize
  Rubinius.lock(self)
  begin
    yield
  ensure
    Rubinius.unlock(self)
  end
end

def cheap_synchronize

def cheap_synchronize
  JRuby.reference0(self).synchronized { yield }
end

def cheap_synchronize

Non-reentrant Mutex#syncrhonize
def cheap_synchronize
  true until (my_mutex = mutex) || cas_mutex(nil, my_mutex = Mutex.new)
  my_mutex.synchronize { yield }
end

def cheap_wait

def cheap_wait
  wchan = Rubinius::Channel.new
  begin
    waiters = @waiters ||= []
    waiters.push wchan
    Rubinius.unlock(self)
    signaled = wchan.receive_timeout nil
  ensure
    Rubinius.lock(self)
    unless signaled or waiters.delete(wchan)
      # we timed out, but got signaled afterwards (e.g. while waiting to
      # acquire @lock), so pass that signal on to the next waiter
      waiters.shift << true unless waiters.empty?
    end
  end
  self
end

def cheap_wait

def cheap_wait
  JRuby.reference0(self).wait
end

def cheap_wait

Must only be called in +cheap_broadcast+'s block.
Releases this object's +cheap_synchronize+ lock and goes to sleep waiting for other threads to +cheap_broadcast+, reacquires the lock on wakeup.
def cheap_wait
  conditional_variable = @conditional_variable ||= ConditionVariable.new
  conditional_variable.wait(mutex)
end