ratomic 0.4.1 → 0.4.3
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 +12 -0
- data/ext/ratomic/src/hashmap.rs +25 -3
- data/ext/ratomic/src/lib.rs +26 -5
- data/lib/ratomic/local_pool.rb +2 -0
- data/lib/ratomic/pool.rb +4 -0
- data/lib/ratomic/version.rb +1 -1
- data/sig/ratomic.rbs +4 -0
- 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: 73c8a9e8067cd54768d471f72fec834fbc4a865b70cb77ca784a2d4addace65a
|
|
4
|
+
data.tar.gz: 7e294ec44ad2e110f731aca962a5fe37f532e6f8d6f5d144e00fae575c347aa4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: abf7be9f9209c28b97e2bce3d224cd9c269d2cd7675a4d987f5d3121240648799f29910a704e17fb01081152945acadfe7be4d08cdd3db7247d8ddaf1211372d
|
|
7
|
+
data.tar.gz: 04eb14597dbc79dfef40fa74f56440edb61028f6de591ccdc0d1a0abc0fb4a8072edc18547c3b69ced8a3df60ede050e503ed2f732d41ed37578b66d966851a6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## 0.4.3
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Expose `Pool#size` and `LocalPool#size` and add `shutdown` aliases for connection pool compatibility.
|
|
8
|
+
|
|
9
|
+
## 0.4.2
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- Prevent `Map#compute` from deadlocking when multiple Ruby threads contend for the same key.
|
|
14
|
+
|
|
3
15
|
## 0.4.1
|
|
4
16
|
|
|
5
17
|
### Documentation
|
data/ext/ratomic/src/hashmap.rs
CHANGED
|
@@ -54,6 +54,21 @@ impl MapStore {
|
|
|
54
54
|
self.map.len()
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
fn with_entry<F, R, W>(&self, key: VALUE, mut on_locked: W, f: F) -> R
|
|
58
|
+
where
|
|
59
|
+
F: FnOnce(Entry<'_, RubyHashEql, VALUE>) -> R,
|
|
60
|
+
W: FnMut(),
|
|
61
|
+
{
|
|
62
|
+
let mut f = Some(f);
|
|
63
|
+
loop {
|
|
64
|
+
if let Some(entry) = self.map.try_entry(RubyHashEql(key)) {
|
|
65
|
+
return f.take().expect("entry closure already used")(entry);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
on_locked();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
57
72
|
pub fn fetch_and_modify<F>(&self, key: VALUE, f: F)
|
|
58
73
|
where
|
|
59
74
|
F: FnOnce(VALUE) -> VALUE,
|
|
@@ -61,11 +76,18 @@ impl MapStore {
|
|
|
61
76
|
self.map.alter(&RubyHashEql(key), |_, value| f(value));
|
|
62
77
|
}
|
|
63
78
|
|
|
64
|
-
pub fn compute<F, E
|
|
79
|
+
pub fn compute<F, E, W>(
|
|
80
|
+
&self,
|
|
81
|
+
key: VALUE,
|
|
82
|
+
missing: VALUE,
|
|
83
|
+
on_locked: W,
|
|
84
|
+
f: F,
|
|
85
|
+
) -> Result<VALUE, E>
|
|
65
86
|
where
|
|
66
87
|
F: FnOnce(VALUE) -> Result<VALUE, E>,
|
|
88
|
+
W: FnMut(),
|
|
67
89
|
{
|
|
68
|
-
|
|
90
|
+
self.with_entry(key, on_locked, |entry| match entry {
|
|
69
91
|
Entry::Occupied(mut entry) => {
|
|
70
92
|
let new_value = f(*entry.get())?;
|
|
71
93
|
entry.insert(new_value);
|
|
@@ -76,7 +98,7 @@ impl MapStore {
|
|
|
76
98
|
entry.insert(new_value);
|
|
77
99
|
Ok(new_value)
|
|
78
100
|
}
|
|
79
|
-
}
|
|
101
|
+
})
|
|
80
102
|
}
|
|
81
103
|
|
|
82
104
|
pub fn update<F, E>(&self, key: VALUE, f: F) -> Result<VALUE, E>
|
data/ext/ratomic/src/lib.rs
CHANGED
|
@@ -18,7 +18,7 @@ use magnus::{
|
|
|
18
18
|
use mpmc_queue::MpmcQueue;
|
|
19
19
|
use parking_lot::Mutex;
|
|
20
20
|
use rb_sys::{rb_ext_ractor_safe, rb_thread_call_without_gvl, ruby_special_consts, VALUE};
|
|
21
|
-
use std::{ffi::c_void, mem::transmute};
|
|
21
|
+
use std::{ffi::c_void, mem::transmute, time::Duration};
|
|
22
22
|
|
|
23
23
|
fn value_to_raw(value: Value) -> VALUE {
|
|
24
24
|
unsafe { transmute::<Value, VALUE>(value) }
|
|
@@ -38,6 +38,22 @@ fn make_shareable(ruby: &Ruby, value: Value) -> Result<Value, Error> {
|
|
|
38
38
|
ractor.funcall("make_shareable", (value,))
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
unsafe extern "C" fn sleep_briefly_without_gvl(_: *mut c_void) -> *mut c_void {
|
|
42
|
+
std::thread::sleep(Duration::from_millis(1));
|
|
43
|
+
std::ptr::null_mut()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fn wait_for_entry_without_gvl() {
|
|
47
|
+
unsafe {
|
|
48
|
+
rb_thread_call_without_gvl(
|
|
49
|
+
Some(sleep_briefly_without_gvl),
|
|
50
|
+
std::ptr::null_mut(),
|
|
51
|
+
None,
|
|
52
|
+
std::ptr::null_mut(),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
41
57
|
struct Counter(AtomicCounter);
|
|
42
58
|
|
|
43
59
|
impl Counter {
|
|
@@ -158,10 +174,15 @@ impl HashMap {
|
|
|
158
174
|
}
|
|
159
175
|
|
|
160
176
|
let proc = ruby.block_proc()?;
|
|
161
|
-
let raw = rb_self.0.compute(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
177
|
+
let raw = rb_self.0.compute(
|
|
178
|
+
value_to_raw(key),
|
|
179
|
+
qnil_raw(),
|
|
180
|
+
wait_for_entry_without_gvl,
|
|
181
|
+
|value| {
|
|
182
|
+
proc.call::<_, Value>((unsafe { value_from_raw(value) },))
|
|
183
|
+
.map(value_to_raw)
|
|
184
|
+
},
|
|
185
|
+
)?;
|
|
165
186
|
|
|
166
187
|
Ok(unsafe { value_from_raw(raw) }.into_value_with(ruby))
|
|
167
188
|
}
|
data/lib/ratomic/local_pool.rb
CHANGED
|
@@ -69,6 +69,7 @@ module Ratomic
|
|
|
69
69
|
# @see Pool Use Pool for plain mutable Ruby values where ownership transfer
|
|
70
70
|
# is the desired safety model.
|
|
71
71
|
class LocalPool
|
|
72
|
+
attr_reader :size
|
|
72
73
|
# Create a per-Ractor local pool facade.
|
|
73
74
|
#
|
|
74
75
|
# @param size [Integer] maximum number of resources in each Ractor-local pool
|
|
@@ -127,6 +128,7 @@ module Ratomic
|
|
|
127
128
|
pool.close
|
|
128
129
|
nil
|
|
129
130
|
end
|
|
131
|
+
alias_method :shutdown, :close
|
|
130
132
|
|
|
131
133
|
# Minimal thread-safe resource pool used inside exactly one Ractor.
|
|
132
134
|
class ResourcePool
|
data/lib/ratomic/pool.rb
CHANGED
|
@@ -26,6 +26,7 @@ module Ratomic
|
|
|
26
26
|
# buffer << :change
|
|
27
27
|
# end
|
|
28
28
|
class Pool
|
|
29
|
+
attr_reader :size
|
|
29
30
|
# Create a pool and seed it with +size+ objects from the factory block.
|
|
30
31
|
#
|
|
31
32
|
# @param size [Integer] number of pooled objects to create up front
|
|
@@ -37,6 +38,7 @@ module Ratomic
|
|
|
37
38
|
raise ArgumentError, "pool size must be positive" if size <= 0
|
|
38
39
|
raise LocalJumpError, "no block given" unless block_given?
|
|
39
40
|
|
|
41
|
+
@size = size
|
|
40
42
|
@timeout = timeout&.to_f
|
|
41
43
|
@control = self.class.send(:new_control_ractor)
|
|
42
44
|
size.times { @control.send([:checkin, yield], move: true) }
|
|
@@ -88,6 +90,8 @@ module Ratomic
|
|
|
88
90
|
rescue Ractor::ClosedError, Ractor::Error
|
|
89
91
|
nil
|
|
90
92
|
end
|
|
93
|
+
# backwards compatibility with ConnectionPool
|
|
94
|
+
alias_method :shutdown, :close
|
|
91
95
|
|
|
92
96
|
# Checkout an object, yield it, then move it back to the pool.
|
|
93
97
|
#
|
data/lib/ratomic/version.rb
CHANGED
data/sig/ratomic.rbs
CHANGED
|
@@ -68,6 +68,8 @@ module Ratomic
|
|
|
68
68
|
|
|
69
69
|
def with: () { (untyped object) -> untyped } -> untyped
|
|
70
70
|
def close: () -> nil
|
|
71
|
+
def shutdown: () -> nil
|
|
72
|
+
def size: () -> Integer
|
|
71
73
|
end
|
|
72
74
|
|
|
73
75
|
class Pool
|
|
@@ -76,6 +78,8 @@ module Ratomic
|
|
|
76
78
|
def checkout: () -> untyped
|
|
77
79
|
def checkin: (untyped object) -> nil
|
|
78
80
|
def close: () -> nil
|
|
81
|
+
def shutdown: () -> nil
|
|
82
|
+
def size: () -> Integer
|
|
79
83
|
def with: () { (untyped object) -> untyped } -> untyped
|
|
80
84
|
end
|
|
81
85
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ratomic
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mike Perham
|
|
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
82
|
version: '0'
|
|
83
83
|
requirements: []
|
|
84
|
-
rubygems_version: 4.0.
|
|
84
|
+
rubygems_version: 4.0.16
|
|
85
85
|
specification_version: 4
|
|
86
86
|
summary: Ractor-safe concurrent data structures for Ruby
|
|
87
87
|
test_files: []
|