ractor-tmvar 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 452e6a7f21ca12c8163f6b2c9a90169705b92e28bcbb0e5a2a41fb66c0d6b258
4
- data.tar.gz: 33e776a975119f6d61593870cb317bdd8a8b45693178613098ef359d4fefb2f5
3
+ metadata.gz: 61f2c5af774133ccaca92fd2ab9d5926d1faf7d229d60f62c7928e82c8e06cef
4
+ data.tar.gz: d5e176ee4886ca2f5feea7f20c9b3989e33778b3a433e7f550e281b43c9bfb72
5
5
  SHA512:
6
- metadata.gz: 57c2cdd4d7918a3869120ca8565a67de62accad9f1e46fa5e4dced91d4f36eb584122ec5fe0923fd263f2b39b64116509ece1a85b41cf82df9b8182506ba618e
7
- data.tar.gz: 26cfe85a897f01216cb68a783e259d03a7563905cf70eacde9a157795db0555cf7cb78417124196fddad46a8f574b7cc490a471593d512d778d0dce6889da326
6
+ metadata.gz: 605a25ac148a512f7d2a8c18d98e6c73cae522fb69963e7a9b5357b4165efeb044a8b0e425a2172f2ad81278a1a632a434bfeb4d22a8047e4cd44e99630a49c9
7
+ data.tar.gz: 9523eb78bbef48de3b809df3a442eaa2be9df55573e2ea1e0d4a7b3692977a062ba36ca6de18182800dea1c46b0120aa50e28316d21136816af09fc4d65e5522
@@ -12,3 +12,8 @@ Style/StringLiteralsInInterpolation:
12
12
 
13
13
  Layout/LineLength:
14
14
  Max: 120
15
+
16
+ Metrics/ClassLength:
17
+ Enabled: true
18
+ Exclude:
19
+ - "test/**/*"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ractor-tmvar (0.1.0)
4
+ ractor-tmvar (0.2.0)
5
5
  ractor-tvar (~> 0.3.0)
6
6
 
7
7
  GEM
@@ -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
- # Represents "blank" value for TMVar
9
+ # represents "empty" value for TMVar
10
10
  #
11
- BLANK = :RACTOR_TMVAR_BLANK
11
+ EMPTY = :RACTOR_TMVAR_EMPTY
12
12
 
13
13
  #
14
- # TMVar.new(value) to initialize TMVar
14
+ # initialize TMVar
15
15
  #
16
- # @param [Object] tvar neet to be shareable
16
+ # @param [Object] value Value to set TVar. It needs to be shareable.
17
17
  #
18
18
  # @return [TMVar]
19
19
  #
20
- def initialize(tvar = nil)
21
- @tvar = Ractor::TVar.new(tvar)
20
+ def initialize(value = nil)
21
+ @tvar = Ractor::TVar.new(value)
22
22
  end
23
23
 
24
24
  #
25
- # Get TVar's value and leave the value to "blank".
26
- # If the value is already "blank", it will retry the transaction.
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 value
32
+ def take
31
33
  v = @tvar.value
32
- raise Ractor::RetryTransaction if v == BLANK
34
+ raise Ractor::RetryTransaction if v == EMPTY
33
35
 
34
- # NOTE: TVar cannot be set without `Ractor.atomically`.
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
- # Put value to TVar's value.
43
- # If the value is not "blank", it will retry the transaction.
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 value=(new_value)
48
- raise Ractor::RetryTransaction if @tvar.value != BLANK
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Ractor
4
4
  class TMVar
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
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.1.2
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-30 00:00:00.000000000 Z
11
+ date: 2020-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ractor-tvar