Class: GetSet

Inherits:
Object
  • Object
show all
Defined in:
lib/boxgrinder-build/util/concurrent/get_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = false) ⇒ GetSet

Returns a new instance of GetSet.



22
23
24
25
# File 'lib/boxgrinder-build/util/concurrent/get_set.rb', line 22

def initialize(initial_state=false)
  @val = initial_state
  @mutex = Mutex.new
end

Instance Method Details

#get_set(set_val = nil, &blk) ⇒ Object

Atomic get-and-set.

When used with a block, the existing value is provided as an argument to the block. The block's return value sets the object's value state.

When used without a block; if a nil set_val parameter is provided the existing state is returned. Else the object value state is set to set_val



36
37
38
39
40
41
42
43
44
45
# File 'lib/boxgrinder-build/util/concurrent/get_set.rb', line 36

def get_set(set_val=nil, &blk)
  @mutex.synchronize do
    if block_given?
      @val = blk.call(@val)
    else
      @val = set_val unless set_val.nil?
    end
    @val
  end
end