Class: SmartCore::Engine::ReadWriteLock

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/engine/read_write_lock.rb

Overview

Since:

  • 0.14.0

Version:

  • 0.17.0

Instance Method Summary collapse

Constructor Details

#initializevoid

Since:

  • 0.14.0



11
12
13
14
15
16
17
# File 'lib/smart_core/engine/read_write_lock.rb', line 11

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

Instance Method Details

#read_sync(&block) ⇒ Any

Parameters:

  • block (Block)

Returns:

  • (Any)

Since:

  • 0.14.0

Version:

  • 0.17.0



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smart_core/engine/read_write_lock.rb', line 25

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

#write_owned?Boolean

Returns:

  • (Boolean)

Since:

  • 0.15.0



41
42
43
# File 'lib/smart_core/engine/read_write_lock.rb', line 41

def write_owned?
  @write_lock.owned?
end

#write_sync(&block) ⇒ Any

Parameters:

  • block (Block)

Returns:

  • (Any)

Since:

  • 0.14.0

Version:

  • 0.16.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smart_core/engine/read_write_lock.rb', line 51

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