Class: Ractor::TMVar

Inherits:
Object
  • Object
show all
Defined in:
lib/ractor/tmvar.rb,
lib/ractor/tmvar/version.rb

Overview

TMVar for Ractor inspired by Haskell’s TMVar based on Ractor::TVar.

Constant Summary collapse

EMPTY =

represents “empty” value for TMVar

:RACTOR_TMVAR_EMPTY
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ TMVar

initialize TMVar

Parameters:

  • value (Object) (defaults to: nil)

    Value to set TVar. It needs to be shareable.



20
21
22
# File 'lib/ractor/tmvar.rb', line 20

def initialize(value = nil)
  @tvar = Ractor::TVar.new(value)
end

Instance Method Details

#empty?Boolean

return the value is “empty” or not.

Parameters:

  • (Boolean)

Returns:

  • (Boolean)


109
110
111
# File 'lib/ractor/tmvar.rb', line 109

def empty?
  @tvar.value == EMPTY
end

#put(new_value) ⇒ Object

write the given value. If the current value is not “empty”, it retries the transaction.

Parameters:

  • new_value (Object)

    neet to be shareable

Raises:

  • (Ractor::RetryTransaction)


84
85
86
87
88
# File 'lib/ractor/tmvar.rb', line 84

def put(new_value)
  raise Ractor::RetryTransaction if @tvar.value != EMPTY

  @tvar.value = new_value
end

#readObject

get the value like take. The difference between take and read is take leaves the value blank, but read not change the value to blank.

Returns:

  • (Object)

    value of internal TVar.

Raises:

  • (Ractor::RetryTransaction)


61
62
63
64
65
66
# File 'lib/ractor/tmvar.rb', line 61

def read
  v = @tvar.value
  raise Ractor::RetryTransaction if v == EMPTY

  v
end

#swap(new) ⇒ Object

get the the value like get, and replace the value to the given value if the current value is not “empty”

Parameters:

  • new_value (Object)

    neet to be shareable

Raises:

  • (Ractor::RetryTransaction)


118
119
120
121
122
123
124
# File 'lib/ractor/tmvar.rb', line 118

def swap(new)
  v = @tvar.value
  raise Ractor::RetryTransaction if v == EMPTY

  @tvar.value = new
  v
end

#takeObject

Note:

You need to wrap it by Ractor.atomically even if you only call take because TVar#value= needs atomically.

get the value and leave the value to “empty” If the value is already “empty”, it will retry the transaction.

Returns:

  • (Object)

    value of internal TVar.

Raises:

  • (Ractor::RetryTransaction)


32
33
34
35
36
37
38
# File 'lib/ractor/tmvar.rb', line 32

def take
  v = @tvar.value
  raise Ractor::RetryTransaction if v == EMPTY

  @tvar.value = EMPTY
  v
end

#try_put(new_value) ⇒ Object

try to put value to TVar’s value If the value is not “empty”, it will not retry and only return false. If it succeed to put, it returns true.

Parameters:

  • new_value (Object)

    neet to be shareable



97
98
99
100
101
102
# File 'lib/ractor/tmvar.rb', line 97

def try_put(new_value)
  return false if @tvar.value != EMPTY

  @tvar.value = new_value
  true
end

#try_readObject

read the value like read but it does not retry.

Returns:

  • (Object)

    value of internal TVar.



73
74
75
76
# File 'lib/ractor/tmvar.rb', line 73

def try_read
  v = @tvar.value
  v == EMPTY ? nil : v
end

#try_takeObject

try to get the value. If the value is “empty”, it returns nil.

Returns:

  • (Object)

    value of internal TVar, only if exists.



46
47
48
49
50
51
52
# File 'lib/ractor/tmvar.rb', line 46

def try_take
  v = @tvar.value
  return nil if v == EMPTY

  @tvar.value = EMPTY
  v
end