class Concurrent::MutexSemaphore

@!macro semaphore @!visibility private @!macro internal_implementation_note

Public Class Methods

new(count) click to toggle source

@!macro semaphore_method_initialize

Calls superclass method
# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 12
def initialize(count)
  Utility::NativeInteger.ensure_integer_and_bounds count

  super()
  synchronize { ns_initialize count }
end

Public Instance Methods

acquire(permits = 1) { || ... } click to toggle source

@!macro semaphore_method_acquire

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 20
def acquire(permits = 1)
  Utility::NativeInteger.ensure_integer_and_bounds permits
  Utility::NativeInteger.ensure_positive permits

  synchronize do
    try_acquire_timed(permits, nil)
  end

  return unless block_given?

  begin
    yield
  ensure
    release(permits)
  end
end
available_permits() click to toggle source

@!macro semaphore_method_available_permits

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 38
def available_permits
  synchronize { @free }
end
drain_permits() click to toggle source

@!macro semaphore_method_drain_permits

Acquires and returns all permits that are immediately available.

@return [Integer]
# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 47
def drain_permits
  synchronize do
    @free.tap { |_| @free = 0 }
  end
end
reduce_permits(reduction) click to toggle source

Shrinks the number of available permits by the indicated reduction.

@param [Fixnum] reduction Number of permits to remove.

@raise [ArgumentError] if `reduction` is not an integer or is negative

@raise [ArgumentError] if `@free` - `@reduction` is less than zero

@return [nil]

@!visibility private

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 99
def reduce_permits(reduction)
  Utility::NativeInteger.ensure_integer_and_bounds reduction
  Utility::NativeInteger.ensure_positive reduction

  synchronize { @free -= reduction }
  nil
end
release(permits = 1) click to toggle source

@!macro semaphore_method_release

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 77
def release(permits = 1)
  Utility::NativeInteger.ensure_integer_and_bounds permits
  Utility::NativeInteger.ensure_positive permits

  synchronize do
    @free += permits
    permits.times { ns_signal }
  end
  nil
end
try_acquire(permits = 1, timeout = nil) { || ... } click to toggle source

@!macro semaphore_method_try_acquire

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 54
def try_acquire(permits = 1, timeout = nil)
  Utility::NativeInteger.ensure_integer_and_bounds permits
  Utility::NativeInteger.ensure_positive permits

  acquired = synchronize do
    if timeout.nil?
      try_acquire_now(permits)
    else
      try_acquire_timed(permits, timeout)
    end
  end

  return acquired unless block_given?
  return unless acquired

  begin
    yield
  ensure
    release(permits)
  end
end

Protected Instance Methods

ns_initialize(count) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 110
def ns_initialize(count)
  @free = count
end

Private Instance Methods

try_acquire_now(permits) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 117
def try_acquire_now(permits)
  if @free >= permits
    @free -= permits
    true
  else
    false
  end
end
try_acquire_timed(permits, timeout) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb, line 127
def try_acquire_timed(permits, timeout)
  ns_wait_until(timeout) { try_acquire_now(permits) }
end