Class: Ractor::TMVar
- Inherits:
-
Object
- Object
- Ractor::TMVar
- 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
-
#empty? ⇒ Boolean
return the value is “empty” or not.
-
#initialize(value = nil) ⇒ TMVar
constructor
initialize TMVar.
-
#put(new_value) ⇒ Object
write the given value.
-
#read ⇒ Object
get the value like
take
. -
#swap(new) ⇒ Object
get the the value like
get
, and replace the value to the given value if the current value is not “empty”. -
#take ⇒ Object
get the value and leave the value to “empty” If the value is already “empty”, it will retry the transaction.
-
#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.
-
#try_read ⇒ Object
read the value like
read
but it does not retry. -
#try_take ⇒ Object
try to get the value.
Constructor Details
Instance Method Details
#empty? ⇒ Boolean
return the value is “empty” or not.
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.
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 |
#read ⇒ Object
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.
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”
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 |
#take ⇒ Object
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.
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.
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_read ⇒ Object
read the value like read
but it does not retry.
73 74 75 76 |
# File 'lib/ractor/tmvar.rb', line 73 def try_read v = @tvar.value v == EMPTY ? nil : v end |