Sha256: 4418a6fb402ff7f712aacdec6736364bca498e699ef3492dcf190b34b5a02a29

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

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
  # @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.16.0 lib/smart_core/engine/read_write_lock.rb