ratomic 0.3.0 → 0.3.1
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +1 -3
- data/ext/ratomic/src/lib.rs +14 -7
- data/lib/ratomic/counter.rb +10 -10
- data/lib/ratomic/map.rb +63 -49
- data/lib/ratomic/pool.rb +8 -8
- data/lib/ratomic/queue.rb +13 -13
- data/lib/ratomic/undefined.rb +3 -1
- data/lib/ratomic/version.rb +2 -6
- data/lib/ratomic.rb +10 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bdf323b8b7b61fa10e96f376cf5cf5001b5df0c5155e0a2ab815a86561c2245d
|
|
4
|
+
data.tar.gz: c5d6536e0ef372bde684f19ca4af1174d1f7ab6a2143270bb884c674ab76fba2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 268606f63d6868b2f1c02421ad27e09d8344c2b404653d2e2e43376db814f9deaafdd608bce146129addd72ba43fc4b534a4a1aca17851c9fec35aad8a781400
|
|
7
|
+
data.tar.gz: fc604b15381e5ef3bd9a48c4294eefb4dcdfc5c2d6dbdf526ecfe621ce9adcad26db861eba41a33cf1d1dd7388696ea6d2f959dd2992cb77e7f13e108e6f9de0
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.3.1] - 2026-06-05
|
|
4
|
+
|
|
5
|
+
- Realign `Counter`, `Map`, and `Queue` return semantics with Ruby conventions.
|
|
6
|
+
- Expand YARD comments across the public API to keep full documentation coverage.
|
|
7
|
+
- Move the top-level module docs to the gem entrypoint for a cleaner load path.
|
|
8
|
+
|
|
3
9
|
## [0.3.0] - 2026-06-05
|
|
4
10
|
|
|
5
11
|
- Promote the DashMap-backed `Ratomic::Map` API as the primary concurrent Hash primitive.
|
data/README.md
CHANGED
|
@@ -5,9 +5,7 @@
|
|
|
5
5
|
[](https://www.ruby-lang.org/en/)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
|
-
Ratomic provides mutable data structures for Ruby Ractors. Its primitives are backed by
|
|
9
|
-
native Rust concurrency libraries so Ruby code can share useful state across Ractors
|
|
10
|
-
without falling back to one global lock.
|
|
8
|
+
Ratomic provides mutable data structures for Ruby Ractors. Its primitives are backed by native Rust concurrency libraries so Ruby code can share useful state across Ractors without falling back to one global lock. `Pool` uses Ruby Ractor ownership-transfer primitives instead of the native Rust path.
|
|
11
9
|
|
|
12
10
|
## Project Direction
|
|
13
11
|
|
data/ext/ratomic/src/lib.rs
CHANGED
|
@@ -46,12 +46,14 @@ impl Counter {
|
|
|
46
46
|
make_shareable(ruby, value)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
fn increment(&self, amt: u64) {
|
|
49
|
+
fn increment(&self, amt: u64) -> u64 {
|
|
50
50
|
self.0.inc(amt);
|
|
51
|
+
self.0.read()
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
fn decrement(&self, amt: u64) {
|
|
54
|
+
fn decrement(&self, amt: u64) -> u64 {
|
|
54
55
|
self.0.dec(amt);
|
|
56
|
+
self.0.read()
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
fn read(&self) -> u64 {
|
|
@@ -100,8 +102,9 @@ impl HashMap {
|
|
|
100
102
|
self.0.contains_key(value_to_raw(key))
|
|
101
103
|
}
|
|
102
104
|
|
|
103
|
-
fn set(&self, key: Value, value: Value) {
|
|
105
|
+
fn set(&self, key: Value, value: Value) -> Value {
|
|
104
106
|
self.0.set(value_to_raw(key), value_to_raw(value));
|
|
107
|
+
value
|
|
105
108
|
}
|
|
106
109
|
|
|
107
110
|
fn delete(ruby: &Ruby, rb_self: &Self, key: Value) -> Value {
|
|
@@ -109,8 +112,10 @@ impl HashMap {
|
|
|
109
112
|
unsafe { value_from_raw(raw) }.into_value_with(ruby)
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
fn clear(&
|
|
113
|
-
|
|
115
|
+
fn clear(_ruby: &Ruby, value: Value) -> Result<Value, Error> {
|
|
116
|
+
let rb_self: &Self = TryConvert::try_convert(value)?;
|
|
117
|
+
rb_self.0.clear();
|
|
118
|
+
Ok(value)
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
fn size(&self) -> usize {
|
|
@@ -307,9 +312,10 @@ impl Queue {
|
|
|
307
312
|
make_shareable(ruby, value)
|
|
308
313
|
}
|
|
309
314
|
|
|
310
|
-
fn push(&
|
|
315
|
+
fn push(ruby: &Ruby, rb_self: Value, item: Value) -> Result<Value, Error> {
|
|
316
|
+
let queue: &Self = TryConvert::try_convert(rb_self)?;
|
|
311
317
|
let mut payload = PushPayload {
|
|
312
|
-
queue: &
|
|
318
|
+
queue: &queue.0,
|
|
313
319
|
item: value_to_raw(item),
|
|
314
320
|
};
|
|
315
321
|
unsafe {
|
|
@@ -320,6 +326,7 @@ impl Queue {
|
|
|
320
326
|
std::ptr::null_mut(),
|
|
321
327
|
);
|
|
322
328
|
}
|
|
329
|
+
Ok(rb_self.into_value_with(ruby))
|
|
323
330
|
}
|
|
324
331
|
|
|
325
332
|
fn pop(&self) -> Value {
|
data/lib/ratomic/counter.rb
CHANGED
|
@@ -8,48 +8,48 @@ module Ratomic
|
|
|
8
8
|
#
|
|
9
9
|
# @example Count work across Ractors
|
|
10
10
|
# counter = Ratomic::Counter.new
|
|
11
|
-
# counter.increment(1)
|
|
11
|
+
# counter.increment(1) # => 1
|
|
12
12
|
# counter.read # => 1
|
|
13
13
|
#
|
|
14
14
|
# @!method self.new
|
|
15
15
|
# Create a counter initialized to zero.
|
|
16
16
|
#
|
|
17
|
-
# @return [Ratomic::Counter]
|
|
17
|
+
# @return [Ratomic::Counter] a new shareable counter
|
|
18
18
|
#
|
|
19
19
|
# @!method read
|
|
20
20
|
# Read the current counter value.
|
|
21
21
|
#
|
|
22
|
-
# @return [Integer]
|
|
22
|
+
# @return [Integer] the current counter value
|
|
23
23
|
#
|
|
24
24
|
# @!method increment(amt)
|
|
25
25
|
# Increment the counter by +amt+.
|
|
26
26
|
#
|
|
27
|
-
# @param amt [Integer]
|
|
28
|
-
# @return [
|
|
27
|
+
# @param amt [Integer] amount to add to the counter
|
|
28
|
+
# @return [Integer] the updated counter value
|
|
29
29
|
#
|
|
30
30
|
# @!method decrement(amt)
|
|
31
31
|
# Decrement the counter by +amt+.
|
|
32
32
|
#
|
|
33
|
-
# @param amt [Integer]
|
|
34
|
-
# @return [
|
|
33
|
+
# @param amt [Integer] amount to subtract from the counter
|
|
34
|
+
# @return [Integer] the updated counter value
|
|
35
35
|
class Counter
|
|
36
36
|
# Read the current counter value.
|
|
37
37
|
#
|
|
38
|
-
# @return [Integer]
|
|
38
|
+
# @return [Integer] the current counter value
|
|
39
39
|
def value
|
|
40
40
|
read
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
# Coerce the counter to an Integer snapshot.
|
|
44
44
|
#
|
|
45
|
-
# @return [Integer]
|
|
45
|
+
# @return [Integer] the current counter value
|
|
46
46
|
def to_i
|
|
47
47
|
read
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
# Check whether the current counter value is zero.
|
|
51
51
|
#
|
|
52
|
-
# @return [Boolean]
|
|
52
|
+
# @return [Boolean] true when the counter currently reads zero
|
|
53
53
|
def zero?
|
|
54
54
|
read.zero?
|
|
55
55
|
end
|
data/lib/ratomic/map.rb
CHANGED
|
@@ -22,15 +22,27 @@ module Ratomic
|
|
|
22
22
|
# Missing keys return nil, so use #key? or #fetch when stored nil values
|
|
23
23
|
# need to be distinguished from missing entries.
|
|
24
24
|
#
|
|
25
|
-
# @param key [Object]
|
|
26
|
-
# @return [Object, nil]
|
|
25
|
+
# @param key [Object] lookup key
|
|
26
|
+
# @return [Object, nil] the stored value, or nil when the key is missing
|
|
27
27
|
#
|
|
28
28
|
# @!method set(key, value)
|
|
29
29
|
# Set a value for +key+.
|
|
30
30
|
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
31
|
+
# This is the method behind `#[]=` and follows Ruby setter semantics by
|
|
32
|
+
# returning the assigned value.
|
|
33
|
+
#
|
|
34
|
+
# @param key [Object] key to write
|
|
35
|
+
# @param value [Object] value to store
|
|
36
|
+
# @return [Object] the assigned value
|
|
37
|
+
#
|
|
38
|
+
# @!method []=(key, value)
|
|
39
|
+
# Set a value for +key+.
|
|
40
|
+
#
|
|
41
|
+
# In assignment form, Ruby returns the value assigned to the expression.
|
|
42
|
+
#
|
|
43
|
+
# @param key [Object] key to write
|
|
44
|
+
# @param value [Object] value to store
|
|
45
|
+
# @return [Object] the assigned value
|
|
34
46
|
#
|
|
35
47
|
# @!method key?(key)
|
|
36
48
|
# Check whether +key+ currently exists in the map.
|
|
@@ -38,8 +50,8 @@ module Ratomic
|
|
|
38
50
|
# Unlike #get and #[], this distinguishes missing keys from stored nil
|
|
39
51
|
# values.
|
|
40
52
|
#
|
|
41
|
-
# @param key [Object]
|
|
42
|
-
# @return [Boolean]
|
|
53
|
+
# @param key [Object] lookup key
|
|
54
|
+
# @return [Boolean] true when the key currently exists
|
|
43
55
|
#
|
|
44
56
|
# @!method delete(key)
|
|
45
57
|
# Remove +key+ and return its previous value.
|
|
@@ -47,20 +59,20 @@ module Ratomic
|
|
|
47
59
|
# Missing keys return nil. Stored nil values also return nil; use #key?
|
|
48
60
|
# before deleting if that distinction matters.
|
|
49
61
|
#
|
|
50
|
-
# @param key [Object]
|
|
51
|
-
# @return [Object, nil]
|
|
62
|
+
# @param key [Object] key to remove
|
|
63
|
+
# @return [Object, nil] the previous value, or nil when the key was missing
|
|
52
64
|
#
|
|
53
65
|
# @!method clear
|
|
54
66
|
# Remove all entries from the map.
|
|
55
67
|
#
|
|
56
|
-
# @return [
|
|
68
|
+
# @return [Ratomic::Map] self
|
|
57
69
|
#
|
|
58
70
|
# @!method size
|
|
59
71
|
# Return the current number of entries.
|
|
60
72
|
#
|
|
61
73
|
# Since this is a concurrent map, the value is a moment-in-time observation.
|
|
62
74
|
#
|
|
63
|
-
# @return [Integer]
|
|
75
|
+
# @return [Integer] the current number of entries
|
|
64
76
|
#
|
|
65
77
|
# @!method fetch_and_modify(key)
|
|
66
78
|
# Replace the existing value for +key+ with the block return value.
|
|
@@ -73,9 +85,9 @@ module Ratomic
|
|
|
73
85
|
# Ractor-hot loops or calling back into the same map from inside the block.
|
|
74
86
|
# If the block raises, the previous value is preserved.
|
|
75
87
|
#
|
|
76
|
-
# @param key [Object]
|
|
77
|
-
# @yieldparam value [Object] current value
|
|
78
|
-
# @return [void]
|
|
88
|
+
# @param key [Object] key to modify in place
|
|
89
|
+
# @yieldparam value [Object] the current stored value
|
|
90
|
+
# @return [void] nothing useful is returned
|
|
79
91
|
# @raise [LocalJumpError] if no block is given
|
|
80
92
|
# @raise [Exception] any exception raised by the block
|
|
81
93
|
#
|
|
@@ -93,8 +105,8 @@ module Ratomic
|
|
|
93
105
|
# If the block raises, the previous value is preserved. If the key was
|
|
94
106
|
# missing, no entry is inserted.
|
|
95
107
|
#
|
|
96
|
-
# @param key [Object]
|
|
97
|
-
# @yieldparam value [Object, nil] current value, or nil
|
|
108
|
+
# @param key [Object] key to compute
|
|
109
|
+
# @yieldparam value [Object, nil] the current stored value, or nil when missing
|
|
98
110
|
# @return [Object] the newly stored value
|
|
99
111
|
# @raise [LocalJumpError] if no block is given
|
|
100
112
|
# @raise [Exception] any exception raised by the block
|
|
@@ -112,7 +124,7 @@ module Ratomic
|
|
|
112
124
|
#
|
|
113
125
|
# If the block raises, no entry is inserted.
|
|
114
126
|
#
|
|
115
|
-
# @param key [Object]
|
|
127
|
+
# @param key [Object] key to read or initialize
|
|
116
128
|
# @return [Object] the existing or newly stored value
|
|
117
129
|
# @raise [LocalJumpError] if no block is given
|
|
118
130
|
# @raise [Exception] any exception raised by the block
|
|
@@ -131,9 +143,9 @@ module Ratomic
|
|
|
131
143
|
#
|
|
132
144
|
# If the block raises, the previous value is preserved.
|
|
133
145
|
#
|
|
134
|
-
# @param key [Object]
|
|
135
|
-
# @param initial [Object]
|
|
136
|
-
# @yieldparam value [Object, nil] current value
|
|
146
|
+
# @param key [Object] key to update
|
|
147
|
+
# @param initial [Object] value to use when the key is missing
|
|
148
|
+
# @yieldparam value [Object, nil] the current stored value, or nil when missing
|
|
137
149
|
# @return [Object] the inserted or newly stored value
|
|
138
150
|
# @raise [LocalJumpError] if no block is given
|
|
139
151
|
# @raise [Exception] any exception raised by the block
|
|
@@ -145,8 +157,8 @@ module Ratomic
|
|
|
145
157
|
# and are left unchanged. This uses a native update path and is the preferred
|
|
146
158
|
# counter primitive for Ractor-heavy workloads.
|
|
147
159
|
#
|
|
148
|
-
# @param key [Object]
|
|
149
|
-
# @param by [Numeric]
|
|
160
|
+
# @param key [Object] counter key to increment
|
|
161
|
+
# @param by [Numeric] amount to add
|
|
150
162
|
# @return [Numeric] the newly stored value
|
|
151
163
|
# @raise [TypeError] if +by+ or the existing value is not numeric
|
|
152
164
|
#
|
|
@@ -155,8 +167,8 @@ module Ratomic
|
|
|
155
167
|
#
|
|
156
168
|
# Missing keys start at zero.
|
|
157
169
|
#
|
|
158
|
-
# @param key [Object]
|
|
159
|
-
# @param by [Numeric]
|
|
170
|
+
# @param key [Object] counter key to decrement
|
|
171
|
+
# @param by [Numeric] amount to subtract
|
|
160
172
|
# @return [Numeric] the newly stored value
|
|
161
173
|
# @raise [TypeError] if +by+ or the existing value is not numeric
|
|
162
174
|
#
|
|
@@ -165,8 +177,8 @@ module Ratomic
|
|
|
165
177
|
#
|
|
166
178
|
# The stored Array is replaced rather than mutated in place.
|
|
167
179
|
#
|
|
168
|
-
# @param key [Object]
|
|
169
|
-
# @param value [Object]
|
|
180
|
+
# @param key [Object] bucket key to append into
|
|
181
|
+
# @param value [Object] value to append
|
|
170
182
|
# @return [Array] the newly stored frozen Array
|
|
171
183
|
# @raise [TypeError] if the existing value is not an Array
|
|
172
184
|
#
|
|
@@ -175,16 +187,18 @@ module Ratomic
|
|
|
175
187
|
#
|
|
176
188
|
# The stored Set is replaced rather than mutated in place.
|
|
177
189
|
#
|
|
178
|
-
# @param key [Object]
|
|
179
|
-
# @param value [Object]
|
|
190
|
+
# @param key [Object] bucket key to update
|
|
191
|
+
# @param value [Object] value to add to the set
|
|
180
192
|
# @return [Set] the newly stored frozen Set
|
|
181
193
|
# @raise [TypeError] if the existing value is not a Set
|
|
182
194
|
class Map
|
|
183
195
|
# Set a value for +key+.
|
|
184
196
|
#
|
|
185
|
-
#
|
|
186
|
-
#
|
|
187
|
-
# @
|
|
197
|
+
# In assignment form, Ruby returns the assigned value.
|
|
198
|
+
#
|
|
199
|
+
# @param key [Object] key to write
|
|
200
|
+
# @param value [Object] value to store
|
|
201
|
+
# @return [Object] the assigned value
|
|
188
202
|
def []=(key, value)
|
|
189
203
|
set(key, value)
|
|
190
204
|
end
|
|
@@ -193,8 +207,8 @@ module Ratomic
|
|
|
193
207
|
#
|
|
194
208
|
# Missing keys currently return nil, so storing nil is ambiguous.
|
|
195
209
|
#
|
|
196
|
-
# @param key [Object]
|
|
197
|
-
# @return [Object, nil]
|
|
210
|
+
# @param key [Object] lookup key
|
|
211
|
+
# @return [Object, nil] the stored value, or nil when the key is missing
|
|
198
212
|
def [](key)
|
|
199
213
|
get(key)
|
|
200
214
|
end
|
|
@@ -203,10 +217,10 @@ module Ratomic
|
|
|
203
217
|
#
|
|
204
218
|
# Unlike #[], this distinguishes missing keys from explicit nil values.
|
|
205
219
|
#
|
|
206
|
-
# @param key [Object]
|
|
207
|
-
# @param default [Object]
|
|
208
|
-
# @yieldparam key [Object]
|
|
209
|
-
# @return [Object]
|
|
220
|
+
# @param key [Object] lookup key
|
|
221
|
+
# @param default [Object] fallback value to return when the key is missing
|
|
222
|
+
# @yieldparam key [Object] the missing key
|
|
223
|
+
# @return [Object] the found value, default, or block result
|
|
210
224
|
# @raise [KeyError] if +key+ is missing and no default or block is provided
|
|
211
225
|
def fetch(key, default = UNDEFINED)
|
|
212
226
|
return get(key) if key?(key)
|
|
@@ -222,8 +236,8 @@ module Ratomic
|
|
|
222
236
|
# and are left unchanged. This uses a native update path and is the preferred
|
|
223
237
|
# counter primitive for Ractor-heavy workloads.
|
|
224
238
|
#
|
|
225
|
-
# @param key [Object]
|
|
226
|
-
# @param by [Numeric]
|
|
239
|
+
# @param key [Object] counter key to increment
|
|
240
|
+
# @param by [Numeric] amount to add
|
|
227
241
|
# @return [Numeric] the newly stored value
|
|
228
242
|
# @raise [TypeError] if +by+ or the existing value is not numeric
|
|
229
243
|
def increment(key, by = 1)
|
|
@@ -236,8 +250,8 @@ module Ratomic
|
|
|
236
250
|
#
|
|
237
251
|
# Missing keys start at zero.
|
|
238
252
|
#
|
|
239
|
-
# @param key [Object]
|
|
240
|
-
# @param by [Numeric]
|
|
253
|
+
# @param key [Object] counter key to decrement
|
|
254
|
+
# @param by [Numeric] amount to subtract
|
|
241
255
|
# @return [Numeric] the newly stored value
|
|
242
256
|
# @raise [TypeError] if +by+ or the existing value is not numeric
|
|
243
257
|
def decrement(key, by = 1)
|
|
@@ -250,8 +264,8 @@ module Ratomic
|
|
|
250
264
|
#
|
|
251
265
|
# The stored Array is replaced rather than mutated in place.
|
|
252
266
|
#
|
|
253
|
-
# @param key [Object]
|
|
254
|
-
# @param value [Object]
|
|
267
|
+
# @param key [Object] bucket key to append into
|
|
268
|
+
# @param value [Object] value to append
|
|
255
269
|
# @return [Array] the newly stored frozen Array
|
|
256
270
|
# @raise [TypeError] if the existing value is not an Array
|
|
257
271
|
def append(key, value)
|
|
@@ -274,8 +288,8 @@ module Ratomic
|
|
|
274
288
|
#
|
|
275
289
|
# The stored Set is replaced rather than mutated in place.
|
|
276
290
|
#
|
|
277
|
-
# @param key [Object]
|
|
278
|
-
# @param value [Object]
|
|
291
|
+
# @param key [Object] bucket key to update
|
|
292
|
+
# @param value [Object] value to add to the set
|
|
279
293
|
# @return [Set] the newly stored frozen Set
|
|
280
294
|
# @raise [TypeError] if the existing value is not a Set
|
|
281
295
|
def add_to_set(key, value)
|
|
@@ -296,22 +310,22 @@ module Ratomic
|
|
|
296
310
|
|
|
297
311
|
# Alias for #size.
|
|
298
312
|
#
|
|
299
|
-
# @return [Integer]
|
|
313
|
+
# @return [Integer] the current number of entries
|
|
300
314
|
def length
|
|
301
315
|
size
|
|
302
316
|
end
|
|
303
317
|
|
|
304
318
|
# Check whether the map currently has no entries.
|
|
305
319
|
#
|
|
306
|
-
# @return [Boolean]
|
|
320
|
+
# @return [Boolean] true when the map currently has no entries
|
|
307
321
|
def empty?
|
|
308
322
|
size.zero?
|
|
309
323
|
end
|
|
310
324
|
|
|
311
325
|
# Alias for #key?.
|
|
312
326
|
#
|
|
313
|
-
# @param key [Object]
|
|
314
|
-
# @return [Boolean]
|
|
327
|
+
# @param key [Object] lookup key
|
|
328
|
+
# @return [Boolean] true when the key currently exists
|
|
315
329
|
def include?(key)
|
|
316
330
|
key?(key)
|
|
317
331
|
end
|
data/lib/ratomic/pool.rb
CHANGED
|
@@ -28,9 +28,9 @@ module Ratomic
|
|
|
28
28
|
class Pool
|
|
29
29
|
# Create a pool and seed it with +size+ objects from the factory block.
|
|
30
30
|
#
|
|
31
|
-
# @param size [Integer] number of pooled objects
|
|
31
|
+
# @param size [Integer] number of pooled objects to create up front
|
|
32
32
|
# @param timeout [Numeric, nil] checkout timeout in seconds, or nil to wait indefinitely
|
|
33
|
-
# @yieldreturn [Object] mutable object to
|
|
33
|
+
# @yieldreturn [Object] a mutable object to place into the pool
|
|
34
34
|
# @raise [ArgumentError] if +size+ is not positive
|
|
35
35
|
# @raise [LocalJumpError] if no factory block is given
|
|
36
36
|
def initialize(size = 5, timeout = 1.0)
|
|
@@ -49,7 +49,7 @@ module Ratomic
|
|
|
49
49
|
# The returned object has been moved from the pool to the caller. The caller
|
|
50
50
|
# owns it until it is passed to #checkin.
|
|
51
51
|
#
|
|
52
|
-
# @return [Object, nil]
|
|
52
|
+
# @return [Object, nil] the checked-out object, or nil if the timeout expires
|
|
53
53
|
def checkout
|
|
54
54
|
reply = Ractor::Port.new
|
|
55
55
|
request_id = reply.object_id
|
|
@@ -68,8 +68,8 @@ module Ratomic
|
|
|
68
68
|
# use the object after calling this method; Ruby raises Ractor::MovedError
|
|
69
69
|
# for stale references.
|
|
70
70
|
#
|
|
71
|
-
# @param object [Object] previously checked
|
|
72
|
-
# @return [nil]
|
|
71
|
+
# @param object [Object] the object previously checked out from the pool
|
|
72
|
+
# @return [nil] nothing useful is returned
|
|
73
73
|
def checkin(object)
|
|
74
74
|
@control.send([:checkin, object], move: true)
|
|
75
75
|
nil
|
|
@@ -80,7 +80,7 @@ module Ratomic
|
|
|
80
80
|
# This is primarily useful for tests and short-lived scripts. A closed pool
|
|
81
81
|
# should not be used for further checkout/checkin operations.
|
|
82
82
|
#
|
|
83
|
-
# @return [nil]
|
|
83
|
+
# @return [nil] nothing useful is returned
|
|
84
84
|
def close
|
|
85
85
|
@control << [:shutdown]
|
|
86
86
|
@control.value
|
|
@@ -94,9 +94,9 @@ module Ratomic
|
|
|
94
94
|
# This is the preferred API because it guarantees checkin through an ensure
|
|
95
95
|
# block. If checkout times out, raises Ratomic::Error and does not yield.
|
|
96
96
|
#
|
|
97
|
-
# @yieldparam object [Object] checked
|
|
97
|
+
# @yieldparam object [Object] the object checked out from the pool
|
|
98
98
|
# @raise [Ratomic::Error] if checkout times out
|
|
99
|
-
# @return [Object] block return value
|
|
99
|
+
# @return [Object] the block return value
|
|
100
100
|
def with
|
|
101
101
|
object = checkout
|
|
102
102
|
raise Ratomic::Error, "pool checkout timeout" if object.nil?
|
data/lib/ratomic/queue.rb
CHANGED
|
@@ -14,21 +14,21 @@ module Ratomic
|
|
|
14
14
|
# @!method self.new(capacity)
|
|
15
15
|
# Create a queue with a fixed capacity.
|
|
16
16
|
#
|
|
17
|
-
# @param capacity [Integer]
|
|
18
|
-
# @return [Ratomic::Queue]
|
|
19
|
-
# @raise [ArgumentError] if capacity is outside the supported range
|
|
20
|
-
# @raise [TypeError] if capacity
|
|
17
|
+
# @param capacity [Integer] maximum number of items the queue can hold
|
|
18
|
+
# @return [Ratomic::Queue] a new shareable queue
|
|
19
|
+
# @raise [ArgumentError] if +capacity+ is outside the supported range
|
|
20
|
+
# @raise [TypeError] if +capacity+ cannot be converted to an Integer
|
|
21
21
|
#
|
|
22
22
|
# @!method push(item)
|
|
23
23
|
# Push an item, blocking until space is available.
|
|
24
24
|
#
|
|
25
|
-
# @param item [Object]
|
|
26
|
-
# @return [
|
|
25
|
+
# @param item [Object] the item to append to the queue
|
|
26
|
+
# @return [Ratomic::Queue] self
|
|
27
27
|
#
|
|
28
28
|
# @!method pop
|
|
29
29
|
# Pop an item, blocking until one is available.
|
|
30
30
|
#
|
|
31
|
-
# @return [Object]
|
|
31
|
+
# @return [Object] the next queued item
|
|
32
32
|
#
|
|
33
33
|
# @!method peek
|
|
34
34
|
# Return the next item without removing it.
|
|
@@ -36,7 +36,7 @@ module Ratomic
|
|
|
36
36
|
# Since this is a concurrent queue, the value is a moment-in-time
|
|
37
37
|
# observation.
|
|
38
38
|
#
|
|
39
|
-
# @return [Object, nil]
|
|
39
|
+
# @return [Object, nil] the next item, or nil if the queue is empty
|
|
40
40
|
#
|
|
41
41
|
# @!method empty?
|
|
42
42
|
# Check whether the queue currently appears empty.
|
|
@@ -44,7 +44,7 @@ module Ratomic
|
|
|
44
44
|
# Since this is a concurrent queue, the value is a moment-in-time
|
|
45
45
|
# observation.
|
|
46
46
|
#
|
|
47
|
-
# @return [Boolean]
|
|
47
|
+
# @return [Boolean] true when the queue currently has no items
|
|
48
48
|
#
|
|
49
49
|
# @!method size
|
|
50
50
|
# Return the current queue size.
|
|
@@ -52,17 +52,17 @@ module Ratomic
|
|
|
52
52
|
# Since this is a concurrent queue, the value is a moment-in-time
|
|
53
53
|
# observation.
|
|
54
54
|
#
|
|
55
|
-
# @return [Integer]
|
|
55
|
+
# @return [Integer] the current number of queued items
|
|
56
56
|
#
|
|
57
57
|
# @!method length
|
|
58
58
|
# Alias for #size.
|
|
59
59
|
#
|
|
60
|
-
# @return [Integer]
|
|
60
|
+
# @return [Integer] the current number of queued items
|
|
61
61
|
class Queue
|
|
62
62
|
# Push an item and return the queue for chaining.
|
|
63
63
|
#
|
|
64
|
-
# @param item [Object]
|
|
65
|
-
# @return [Ratomic::Queue]
|
|
64
|
+
# @param item [Object] the item to append to the queue
|
|
65
|
+
# @return [Ratomic::Queue] self
|
|
66
66
|
def <<(item)
|
|
67
67
|
push(item)
|
|
68
68
|
self
|
data/lib/ratomic/undefined.rb
CHANGED
|
@@ -4,7 +4,9 @@ module Ratomic
|
|
|
4
4
|
# Internal sentinel object for future Hash-like APIs that need to distinguish
|
|
5
5
|
# missing keys from explicit nil values.
|
|
6
6
|
class Undefined
|
|
7
|
-
#
|
|
7
|
+
# Return the sentinel's stable inspection string.
|
|
8
|
+
#
|
|
9
|
+
# @return [String] a human-readable sentinel marker
|
|
8
10
|
def inspect
|
|
9
11
|
"#<Undefined>"
|
|
10
12
|
end
|
data/lib/ratomic/version.rb
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Ratomic provides Ractor-friendly mutable data structures backed by native
|
|
4
|
-
# Rust concurrency primitives.
|
|
5
|
-
#
|
|
6
|
-
# The public API currently includes {Counter}, {Map}, {Queue}, and {Pool}.
|
|
7
3
|
module Ratomic
|
|
8
|
-
# Current gem version.
|
|
9
|
-
VERSION = "0.3.
|
|
4
|
+
# Current gem version string.
|
|
5
|
+
VERSION = "0.3.1"
|
|
10
6
|
end
|
data/lib/ratomic.rb
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
# Ratomic provides mutable data structures for Ruby Ractors. Its primitives
|
|
4
|
+
# are backed by native Rust concurrency libraries so Ruby code can share useful
|
|
5
|
+
# state across Ractors without falling back to one global lock. Pool uses Ruby
|
|
6
|
+
# Ractor ownership-transfer primitives instead of the native Rust path.
|
|
7
|
+
#
|
|
8
|
+
# The public API currently includes {Counter}, {Map}, {Queue}, and {Pool}.
|
|
6
9
|
module Ratomic
|
|
10
|
+
# Base error class for Ratomic-specific failures.
|
|
7
11
|
class Error < StandardError; end
|
|
8
12
|
end
|
|
9
13
|
|
|
14
|
+
require "ratomic/ratomic"
|
|
15
|
+
require "ratomic/version"
|
|
16
|
+
|
|
10
17
|
require "ratomic/undefined"
|
|
11
18
|
require "ratomic/counter"
|
|
12
19
|
require "ratomic/map"
|