Class: Variable::MVar

Inherits:
Variable show all
Defined in:
lib/variable.rb

Overview

Shared variable that can be written multiple times

ignore :reek:InstanceVariableAssumption

Constant Summary

Constants inherited from Variable

EMPTY, TIMEOUT

Instance Method Summary collapse

Methods inherited from Variable

#read, #take, #take_timeout, #try_put, #with

Constructor Details

#initialize(condition_variable:, mutex:, value: EMPTY) ⇒ undefined

Initialize object

Parameters:

  • value (Object) (defaults to: EMPTY)

    the initial value



281
282
283
284
# File 'lib/variable.rb', line 281

def initialize(condition_variable:, mutex:, value: EMPTY)
  super
  @empty = condition_variable.new
end

Instance Method Details

#modifyObject

Modify value, blocks if empty

Returns:

  • (Object)


303
304
305
306
307
308
# File 'lib/variable.rb', line 303

def modify
  synchronize do
    wait_full
    perform_put(yield(@value))
  end
end

#put(value) ⇒ self

Put value, block on full

Parameters:

  • value (Object)

Returns:

  • (self)


291
292
293
294
295
296
297
298
# File 'lib/variable.rb', line 291

def put(value)
  synchronize do
    wait(@empty, &method(:empty?))
    perform_put(value)
  end

  self
end