Class | GetSet |
In: |
lib/boxgrinder-build/util/concurrent/get_set.rb
lib/boxgrinder-build/util/concurrent/get_set.rb |
Parent: | Object |
# File lib/boxgrinder-build/util/concurrent/get_set.rb, line 22 22: def initialize(initial_state=false) 23: @val = initial_state 24: @mutex = Mutex.new 25: end
# File lib/boxgrinder-build/util/concurrent/get_set.rb, line 22 22: def initialize(initial_state=false) 23: @val = initial_state 24: @mutex = Mutex.new 25: end
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
# File lib/boxgrinder-build/util/concurrent/get_set.rb, line 36 36: def get_set(set_val=nil, &blk) 37: @mutex.synchronize do 38: if block_given? 39: @val = blk.call(@val) 40: else 41: @val = set_val unless set_val.nil? 42: end 43: @val 44: end 45: end
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
# File lib/boxgrinder-build/util/concurrent/get_set.rb, line 36 36: def get_set(set_val=nil, &blk) 37: @mutex.synchronize do 38: if block_given? 39: @val = blk.call(@val) 40: else 41: @val = set_val unless set_val.nil? 42: end 43: @val 44: end 45: end