Sha256: fbc4921681af4375208ed767eec9108a02814fd5b11331a78d55115b7a6cd474

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# @api public
# @since 0.14.0
# @version 0.17.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
  # @version 0.17.0
  def read_sync(&block)
    @active_reader = true
    if @write_lock.locked? && @write_lock.owned?
      yield
    else
      while @write_lock.locked? do; end
      yield
    end
  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
  # @version 0.16.0
  def write_sync(&block)
    if @write_lock.owned?
      yield
    else
      while @active_reader do; end
      @write_lock.synchronize do
        @active_reader = true
        begin
          yield
        ensure
          @active_reader = false
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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