Sha256: 79b26680f08123f649a1694cab350fc13807f538f21f58a79dbb5e4bf20beed1

Contents?: true

Size: 1013 Bytes

Versions: 1

Compression:

Stored size: 1013 Bytes

Contents

# frozen_string_literal: true

# @api public
# @since 0.14.0
# @version 0.15.0
class SmartCore::Engine::ReadWriteLock
  # @return [void]
  #
  # @api public
  # @sicne 0.14.0
  def initialize
    # NOTE:
    #   ivars has no readers cuz we want to avoid
    #   Ruby VM's context-switching during reade-method invokation.
    @active_reader = false
    @write_lock = ::Mutex.new
  end

  # @param block [Block]
  # @return [Any]
  #
  # @api public
  # @since 0.14.0
  def read_sync(&block)
    @active_reader = true
    while @write_lock.locked? do; end
    yield
  ensure
    @active_reader = false
  end

  # @return [Boolean]
  #
  # @api public
  # @since 0.15.0
  def write_owned?
    @write_lock.owned?
  end

  # @param block [Block]
  # @return [Any]
  #
  # @api public
  # @since 0.14.0
  def write_sync(&block)
    while @active_reader do; end
    @write_lock.synchronize do
      @active_reader = true
      begin
        yield
      ensure
        @active_reader = false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_engine-0.15.0 lib/smart_core/engine/read_write_lock.rb