Class: Scrolls::AtomicObject

Inherits:
Object
  • Object
show all
Defined in:
lib/scrolls/atomic.rb

Overview

The result of issues with an update I made to Scrolls. After talking with Fabio Kung about a fix I started work on an atomic object, but he added some fixes to #context without it and then used Headius’ atomic gem.

The code below is the start and cleanup of my atomic object. It’s slim on details and eventually cleaned up around inspiration from Headius’ code.

LICENSE: Apache 2.0

See Headius’ atomic gem here: github.com/headius/ruby-atomic

Direct Known Subclasses

Atomic

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ AtomicObject

Returns a new instance of AtomicObject.



16
17
18
19
# File 'lib/scrolls/atomic.rb', line 16

def initialize(o)
  @mtx = Mutex.new
  @o = o
end

Instance Method Details

#getObject



21
22
23
# File 'lib/scrolls/atomic.rb', line 21

def get
  @mtx.synchronize { @o }
end

#set(n) ⇒ Object



25
26
27
# File 'lib/scrolls/atomic.rb', line 25

def set(n)
  @mtx.synchronize { @o = n }
end

#verify_set(o, n) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/scrolls/atomic.rb', line 29

def verify_set(o, n)
  return false unless @mtx.try_lock
  begin
    return false unless @o.equal? o
    @o = n
  ensure
    @mtx.unlock
  end
end