ractor-tmvar 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/Gemfile.lock +1 -1
- data/lib/ractor/tmvar.rb +91 -18
- data/lib/ractor/tmvar/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61f2c5af774133ccaca92fd2ab9d5926d1faf7d229d60f62c7928e82c8e06cef
|
4
|
+
data.tar.gz: d5e176ee4886ca2f5feea7f20c9b3989e33778b3a433e7f550e281b43c9bfb72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 605a25ac148a512f7d2a8c18d98e6c73cae522fb69963e7a9b5357b4165efeb044a8b0e425a2172f2ad81278a1a632a434bfeb4d22a8047e4cd44e99630a49c9
|
7
|
+
data.tar.gz: 9523eb78bbef48de3b809df3a442eaa2be9df55573e2ea1e0d4a7b3692977a062ba36ca6de18182800dea1c46b0120aa50e28316d21136816af09fc4d65e5522
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/lib/ractor/tmvar.rb
CHANGED
@@ -6,48 +6,121 @@ class Ractor
|
|
6
6
|
# TMVar for Ractor inspired by Haskell's TMVar based on Ractor::TVar.
|
7
7
|
class TMVar
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# represents "empty" value for TMVar
|
10
10
|
#
|
11
|
-
|
11
|
+
EMPTY = :RACTOR_TMVAR_EMPTY
|
12
12
|
|
13
13
|
#
|
14
|
-
#
|
14
|
+
# initialize TMVar
|
15
15
|
#
|
16
|
-
# @param [Object]
|
16
|
+
# @param [Object] value Value to set TVar. It needs to be shareable.
|
17
17
|
#
|
18
18
|
# @return [TMVar]
|
19
19
|
#
|
20
|
-
def initialize(
|
21
|
-
@tvar = Ractor::TVar.new(
|
20
|
+
def initialize(value = nil)
|
21
|
+
@tvar = Ractor::TVar.new(value)
|
22
22
|
end
|
23
23
|
|
24
24
|
#
|
25
|
-
#
|
26
|
-
# If the value is already "
|
25
|
+
# get the value and leave the value to "empty"
|
26
|
+
# If the value is already "empty", it will retry the transaction.
|
27
|
+
# @note You need to wrap it by Ractor.atomically even if you only call +take+
|
28
|
+
# because +TVar#value=+ needs atomically.
|
27
29
|
#
|
28
30
|
# @return [Object] value of internal TVar.
|
29
31
|
#
|
30
|
-
def
|
32
|
+
def take
|
31
33
|
v = @tvar.value
|
32
|
-
raise Ractor::RetryTransaction if v ==
|
34
|
+
raise Ractor::RetryTransaction if v == EMPTY
|
33
35
|
|
34
|
-
|
35
|
-
Ractor.atomically do
|
36
|
-
@tvar.value = BLANK
|
37
|
-
end
|
36
|
+
@tvar.value = EMPTY
|
38
37
|
v
|
39
38
|
end
|
40
39
|
|
41
40
|
#
|
42
|
-
#
|
43
|
-
# If the value is
|
41
|
+
# try to get the value.
|
42
|
+
# If the value is "empty", it returns nil.
|
43
|
+
#
|
44
|
+
# @return [Object] value of internal TVar, only if exists.
|
45
|
+
#
|
46
|
+
def try_take
|
47
|
+
v = @tvar.value
|
48
|
+
return nil if v == EMPTY
|
49
|
+
|
50
|
+
@tvar.value = EMPTY
|
51
|
+
v
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# get the value like +take+.
|
56
|
+
# The difference between +take+ and +read+ is +take+ leaves the value blank,
|
57
|
+
# but +read+ not change the value to blank.
|
58
|
+
#
|
59
|
+
# @return [Object] value of internal TVar.
|
60
|
+
#
|
61
|
+
def read
|
62
|
+
v = @tvar.value
|
63
|
+
raise Ractor::RetryTransaction if v == EMPTY
|
64
|
+
|
65
|
+
v
|
66
|
+
end
|
67
|
+
|
68
|
+
#
|
69
|
+
# read the value like +read+ but it does not retry.
|
70
|
+
#
|
71
|
+
# @return [Object] value of internal TVar.
|
72
|
+
#
|
73
|
+
def try_read
|
74
|
+
v = @tvar.value
|
75
|
+
v == EMPTY ? nil : v
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# write the given value.
|
80
|
+
# If the current value is not "empty", it retries the transaction.
|
81
|
+
#
|
82
|
+
# @param [Object] new_value neet to be shareable
|
83
|
+
#
|
84
|
+
def put(new_value)
|
85
|
+
raise Ractor::RetryTransaction if @tvar.value != EMPTY
|
86
|
+
|
87
|
+
@tvar.value = new_value
|
88
|
+
end
|
89
|
+
|
90
|
+
#
|
91
|
+
# try to put value to TVar's value
|
92
|
+
# If the value is not "empty", it will not retry and only return false.
|
93
|
+
# If it succeed to put, it returns true.
|
44
94
|
#
|
45
95
|
# @param [Object] new_value neet to be shareable
|
46
96
|
#
|
47
|
-
def
|
48
|
-
|
97
|
+
def try_put(new_value)
|
98
|
+
return false if @tvar.value != EMPTY
|
49
99
|
|
50
100
|
@tvar.value = new_value
|
101
|
+
true
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# return the value is "empty" or not.
|
106
|
+
#
|
107
|
+
# @param [Boolean]
|
108
|
+
#
|
109
|
+
def empty?
|
110
|
+
@tvar.value == EMPTY
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# get the the value like +get+, and replace the value to the given value if the current value is not "empty"
|
115
|
+
#
|
116
|
+
# @param [Object] new_value neet to be shareable
|
117
|
+
#
|
118
|
+
def swap(new)
|
119
|
+
v = @tvar.value
|
120
|
+
raise Ractor::RetryTransaction if v == EMPTY
|
121
|
+
|
122
|
+
@tvar.value = new
|
123
|
+
v
|
51
124
|
end
|
52
125
|
end
|
53
126
|
end
|
data/lib/ractor/tmvar/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ractor-tmvar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshitsugu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ractor-tvar
|