arbiter_machines 0.1.5 → 0.3.4
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 +50 -0
- data/Cargo.lock +307 -0
- data/Cargo.toml +2 -0
- data/README.md +81 -69
- data/ext/arbiter_machines_native/core/CHANGELOG.md +29 -0
- data/ext/arbiter_machines_native/core/Cargo.toml +4 -2
- data/ext/arbiter_machines_native/core/README.md +34 -1
- data/ext/arbiter_machines_native/core/src/lib.rs +214 -63
- data/ext/arbiter_machines_native/ffi/Cargo.toml +2 -1
- data/ext/arbiter_machines_native/ffi/src/arbiter.rs +244 -118
- data/ext/arbiter_machines_native/ffi/src/lib.rs +0 -17
- data/lib/arbiter_machines/version.rb +1 -1
- data/lib/arbiter_machines.rb +25 -0
- data/sig/arbiter_machines.rbs +51 -4
- metadata +5 -3
|
@@ -1,182 +1,308 @@
|
|
|
1
1
|
//! Erase exact task/resource const-generic counts without adding participants.
|
|
2
2
|
|
|
3
|
-
use
|
|
4
|
-
use arbiter_machines::{Acquisition, Arbiter, Protocol};
|
|
3
|
+
use arbiter_machines::{Acquisition, Arbiter, Cancelling, Protocol, Refusal};
|
|
5
4
|
use magnus::{
|
|
6
|
-
Error,
|
|
7
|
-
prelude::*,
|
|
8
|
-
scan_args::{get_kwargs, scan_args},
|
|
9
|
-
typed_data::Obj,
|
|
5
|
+
Error, IntoValue, RModule, Ruby, Value, function, method, prelude::*, typed_data::Obj,
|
|
10
6
|
};
|
|
7
|
+
use matryona_machines::ruby::{Class, Registry, Symbols, exact_array, integer, keep, symbol};
|
|
8
|
+
use std::any::Any;
|
|
11
9
|
use std::cell::RefCell;
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
/// The most tasks, and the most resources, an arbiter built from Ruby has:
|
|
12
|
+
/// every count in `0..=CAPACITY` is compiled.
|
|
13
|
+
const CAPACITY: usize = 8;
|
|
14
|
+
|
|
15
|
+
trait Decisions: Send + Any {
|
|
16
|
+
/// A copy to decide on: kept only once the answer exists.
|
|
17
|
+
fn boxed(&self) -> Box<dyn Decisions>;
|
|
18
|
+
/// Whether `other` is this very state (same counts, same everything).
|
|
19
|
+
fn same(&self, other: &dyn Decisions) -> bool;
|
|
20
|
+
fn as_any(&self) -> &dyn Any;
|
|
21
|
+
fn acquire(&mut self, task: usize, resource: usize) -> Result<Acquisition, Refusal>;
|
|
22
|
+
fn release(&mut self, task: usize, resource: usize) -> Result<(), Refusal>;
|
|
23
|
+
fn cancel_wait(&mut self, task: usize) -> Result<Cancelling, Refusal>;
|
|
17
24
|
fn effective_priorities(&self) -> Vec<u32>;
|
|
25
|
+
fn owner(&self, resource: usize) -> Option<usize>;
|
|
26
|
+
fn waiting_on(&self, task: usize) -> Option<usize>;
|
|
27
|
+
fn protocol(&self) -> Protocol;
|
|
28
|
+
fn task_count(&self) -> usize;
|
|
29
|
+
fn resource_count(&self) -> usize;
|
|
18
30
|
}
|
|
19
31
|
|
|
20
32
|
impl<const T: usize, const R: usize> Decisions for Arbiter<T, R> {
|
|
21
|
-
fn
|
|
33
|
+
fn boxed(&self) -> Box<dyn Decisions> {
|
|
34
|
+
Box::new(self.clone())
|
|
35
|
+
}
|
|
36
|
+
fn same(&self, other: &dyn Decisions) -> bool {
|
|
37
|
+
other.as_any().downcast_ref::<Self>() == Some(self)
|
|
38
|
+
}
|
|
39
|
+
fn as_any(&self) -> &dyn Any {
|
|
40
|
+
self
|
|
41
|
+
}
|
|
42
|
+
fn acquire(&mut self, task: usize, resource: usize) -> Result<Acquisition, Refusal> {
|
|
22
43
|
self.acquire(task, resource)
|
|
23
44
|
}
|
|
24
|
-
fn release(&mut self, task: usize, resource: usize) ->
|
|
45
|
+
fn release(&mut self, task: usize, resource: usize) -> Result<(), Refusal> {
|
|
25
46
|
self.release(task, resource)
|
|
26
47
|
}
|
|
27
|
-
fn cancel_wait(&mut self, task: usize) ->
|
|
48
|
+
fn cancel_wait(&mut self, task: usize) -> Result<Cancelling, Refusal> {
|
|
28
49
|
self.cancel_wait(task)
|
|
29
50
|
}
|
|
30
51
|
fn effective_priorities(&self) -> Vec<u32> {
|
|
31
52
|
self.effective_priorities().to_vec()
|
|
32
53
|
}
|
|
54
|
+
fn owner(&self, resource: usize) -> Option<usize> {
|
|
55
|
+
self.owner(resource)
|
|
56
|
+
}
|
|
57
|
+
fn waiting_on(&self, task: usize) -> Option<usize> {
|
|
58
|
+
self.waiting_on(task)
|
|
59
|
+
}
|
|
60
|
+
fn protocol(&self) -> Protocol {
|
|
61
|
+
self.protocol()
|
|
62
|
+
}
|
|
63
|
+
fn task_count(&self) -> usize {
|
|
64
|
+
self.task_count()
|
|
65
|
+
}
|
|
66
|
+
fn resource_count(&self) -> usize {
|
|
67
|
+
self.resource_count()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// An arbiter of any compiled counts, as clone-then-keep holds it: a copy to
|
|
72
|
+
/// decide on, and equality with the state it was copied from.
|
|
73
|
+
struct Core(Box<dyn Decisions>);
|
|
74
|
+
|
|
75
|
+
impl Clone for Core {
|
|
76
|
+
fn clone(&self) -> Self {
|
|
77
|
+
Self(self.0.boxed())
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
impl PartialEq for Core {
|
|
82
|
+
fn eq(&self, other: &Self) -> bool {
|
|
83
|
+
self.0.same(other.0.as_ref())
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fn range_error(ruby: &Ruby, message: &'static str) -> Error {
|
|
88
|
+
Error::new(ruby.exception_range_error(), message)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Runs `$body` with `$n` bound to `$len` as a const, for every count in
|
|
92
|
+
/// `0..=CAPACITY`; `$over` when `$len` is larger.
|
|
93
|
+
macro_rules! by_count {
|
|
94
|
+
($len:expr, $n:ident => $body:expr, $over:expr) => {
|
|
95
|
+
by_count!(@ $len, $n => $body, $over; 0 1 2 3 4 5 6 7 8)
|
|
96
|
+
};
|
|
97
|
+
(@ $len:expr, $n:ident => $body:expr, $over:expr; $($k:literal)*) => {
|
|
98
|
+
match $len {
|
|
99
|
+
$($k => {
|
|
100
|
+
const $n: usize = $k;
|
|
101
|
+
$body
|
|
102
|
+
})*
|
|
103
|
+
_ => $over,
|
|
104
|
+
}
|
|
105
|
+
};
|
|
33
106
|
}
|
|
34
107
|
|
|
35
|
-
fn
|
|
108
|
+
fn sized<const T: usize>(
|
|
36
109
|
ruby: &Ruby,
|
|
37
110
|
base: [u32; T],
|
|
38
111
|
ceilings: &[u32],
|
|
39
112
|
protocol: Protocol,
|
|
40
113
|
) -> Result<Box<dyn Decisions>, Error> {
|
|
41
|
-
|
|
42
|
-
(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
114
|
+
by_count!(
|
|
115
|
+
ceilings.len(),
|
|
116
|
+
N => Ok(Box::new(Arbiter::<T, N>::new(
|
|
117
|
+
base,
|
|
118
|
+
std::array::from_fn(|i| ceilings[i]),
|
|
119
|
+
protocol,
|
|
120
|
+
)) as Box<dyn Decisions>),
|
|
121
|
+
Err(range_error(ruby, "at most 8 resources (ceilings)"))
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/// Every element of `value`, exactly an Array (no subclass), as a priority;
|
|
126
|
+
/// `name` is the argument.
|
|
127
|
+
fn priorities(ruby: &Ruby, value: Value, name: &'static str) -> Result<Vec<u32>, Error> {
|
|
128
|
+
let message = if name == "base" {
|
|
129
|
+
"base must be an Array of Integers"
|
|
130
|
+
} else {
|
|
131
|
+
"ceilings must be an Array of Integers"
|
|
132
|
+
};
|
|
133
|
+
exact_array(ruby, value, message)?
|
|
59
134
|
.into_iter()
|
|
60
|
-
.map(|v|
|
|
135
|
+
.map(|v| integer(v, name))
|
|
61
136
|
.collect()
|
|
62
137
|
}
|
|
63
138
|
|
|
139
|
+
matryona_machines::symbols! {
|
|
140
|
+
/// The core's protocols as Symbols.
|
|
141
|
+
Protocols for Protocol as "protocol" {
|
|
142
|
+
Inheritance => "inheritance",
|
|
143
|
+
ImmediateCeiling => "immediate_ceiling",
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
matryona_machines::symbols! {
|
|
148
|
+
/// The core's refusals as Symbols: the snake case of each variant.
|
|
149
|
+
Refusals for Refusal as "a refusal" {
|
|
150
|
+
UnknownTask => "unknown_task",
|
|
151
|
+
UnknownResource => "unknown_resource",
|
|
152
|
+
AboveCeiling => "above_ceiling",
|
|
153
|
+
Deadlock => "deadlock",
|
|
154
|
+
Unowned => "unowned",
|
|
155
|
+
WrongOwner => "wrong_owner",
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
matryona_machines::symbols! {
|
|
160
|
+
/// The core's honoured `cancel_wait` answers as Symbols.
|
|
161
|
+
Cancellings for Cancelling as "a cancellation" {
|
|
162
|
+
Cancelled => "cancelled",
|
|
163
|
+
NotWaiting => "not_waiting",
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Ruby symbol of a refusal: the snake case of the core variant.
|
|
168
|
+
fn refusal(ruby: &Ruby, refused: Refusal) -> Value {
|
|
169
|
+
Refusals::symbol(ruby, refused).as_value()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// `ArbiterMachines::Blocked`, resolved once at init: swapping the constant
|
|
173
|
+
/// later changes nothing the arbiter builds.
|
|
174
|
+
struct Classes {
|
|
175
|
+
blocked: Class,
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static CLASSES: Registry<Classes> = Registry::new("arbiter_machines");
|
|
179
|
+
|
|
180
|
+
/// What `Blocked.new` stores: the resource index, validated as the core's `usize`.
|
|
181
|
+
fn checked_blocked(resource: Value) -> Result<usize, Error> {
|
|
182
|
+
integer(resource, "resource")
|
|
183
|
+
}
|
|
184
|
+
|
|
64
185
|
#[magnus::wrap(class = "ArbiterMachines::Arbiter", free_immediately, size)]
|
|
65
186
|
struct Wrapper {
|
|
66
|
-
core: RefCell<
|
|
67
|
-
tasks: usize,
|
|
68
|
-
resources: usize,
|
|
187
|
+
core: RefCell<Core>,
|
|
69
188
|
}
|
|
70
189
|
|
|
71
190
|
impl Wrapper {
|
|
72
|
-
fn new(ruby: &Ruby,
|
|
73
|
-
let
|
|
74
|
-
let
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"inheritance" => Protocol::Inheritance,
|
|
82
|
-
"immediate_ceiling" => Protocol::ImmediateCeiling,
|
|
83
|
-
_ => {
|
|
84
|
-
return Err(Error::new(
|
|
85
|
-
ruby.exception_arg_error(),
|
|
86
|
-
"protocol must be :inheritance or :immediate_ceiling",
|
|
87
|
-
));
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
let base = counts(priorities)?;
|
|
91
|
-
let ceilings = counts(ceilings)?;
|
|
92
|
-
macro_rules! sized {
|
|
93
|
-
($($n:literal),*) => {
|
|
94
|
-
match base.len() {
|
|
95
|
-
$($n => resources::<$n>(
|
|
96
|
-
ruby,
|
|
97
|
-
std::array::from_fn(|i| base[i]),
|
|
98
|
-
&ceilings,
|
|
99
|
-
protocol,
|
|
100
|
-
)?,)*
|
|
101
|
-
_ => {
|
|
102
|
-
let message = "task count must be 1..8";
|
|
103
|
-
return Err(Error::new(ruby.exception_arg_error(), message));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
let core = sized!(1, 2, 3, 4, 5, 6, 7, 8);
|
|
191
|
+
fn new(ruby: &Ruby, base: Value, ceilings: Value, protocol: Value) -> Result<Self, Error> {
|
|
192
|
+
let base = priorities(ruby, base, "base")?;
|
|
193
|
+
let ceilings = priorities(ruby, ceilings, "ceilings")?;
|
|
194
|
+
let protocol = Protocols::parse(ruby, protocol)?;
|
|
195
|
+
let core = by_count!(
|
|
196
|
+
base.len(),
|
|
197
|
+
N => sized::<N>(ruby, std::array::from_fn(|i| base[i]), &ceilings, protocol)?,
|
|
198
|
+
return Err(range_error(ruby, "at most 8 tasks (base priorities)"))
|
|
199
|
+
);
|
|
109
200
|
Ok(Self {
|
|
110
|
-
core: RefCell::new(core),
|
|
111
|
-
tasks: base.len(),
|
|
112
|
-
resources: ceilings.len(),
|
|
201
|
+
core: RefCell::new(Core(core)),
|
|
113
202
|
})
|
|
114
203
|
}
|
|
115
204
|
|
|
116
|
-
fn indices(args: &[Value]) -> Result<(usize, usize), Error> {
|
|
117
|
-
let args = scan_args::<(), (), (), (), RHash, ()>(args)?;
|
|
118
|
-
let (task, resource) =
|
|
119
|
-
get_kwargs::<_, (Count, Count), (), ()>(args.keywords, &["task", "resource"], &[])?
|
|
120
|
-
.required;
|
|
121
|
-
Ok((task.0 as usize, resource.0 as usize))
|
|
122
|
-
}
|
|
123
|
-
|
|
124
205
|
fn acquire(
|
|
125
206
|
ruby: &Ruby,
|
|
126
207
|
rb_self: Obj<Self>,
|
|
127
|
-
|
|
128
|
-
|
|
208
|
+
task: Value,
|
|
209
|
+
resource: Value,
|
|
210
|
+
) -> Result<Value, Error> {
|
|
211
|
+
// Frozen first, then the arguments: everything that can raise
|
|
212
|
+
// happens before the core decides.
|
|
129
213
|
rb_self.check_frozen()?;
|
|
130
|
-
let (task, resource) =
|
|
131
|
-
let
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
214
|
+
let (task, resource) = (integer(task, "task")?, integer(resource, "resource")?);
|
|
215
|
+
let blocked = CLASSES.get(ruby)?.blocked.get(ruby);
|
|
216
|
+
// Clone-then-keep: the core decides on a copy, under no borrow while
|
|
217
|
+
// Ruby runs, and the copy is stored only once the answer exists.
|
|
218
|
+
keep(
|
|
219
|
+
ruby,
|
|
220
|
+
rb_self,
|
|
221
|
+
&rb_self.core,
|
|
222
|
+
|core| core.0.acquire(task, resource),
|
|
223
|
+
|acquired| match acquired {
|
|
224
|
+
Ok(Acquisition::Granted) => Ok(symbol(ruby, "granted").as_value()),
|
|
225
|
+
Ok(Acquisition::AlreadyOwned) => Ok(symbol(ruby, "already_owned").as_value()),
|
|
226
|
+
Ok(Acquisition::Blocked(index)) => blocked.funcall("new", (index,)),
|
|
227
|
+
Err(refused) => Ok(refusal(ruby, refused)),
|
|
228
|
+
},
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
fn release(
|
|
233
|
+
ruby: &Ruby,
|
|
234
|
+
rb_self: Obj<Self>,
|
|
235
|
+
task: Value,
|
|
236
|
+
resource: Value,
|
|
237
|
+
) -> Result<Value, Error> {
|
|
144
238
|
rb_self.check_frozen()?;
|
|
145
|
-
let (task, resource) =
|
|
146
|
-
let released = rb_self.core.borrow_mut().release(task, resource);
|
|
147
|
-
Ok(released
|
|
239
|
+
let (task, resource) = (integer(task, "task")?, integer(resource, "resource")?);
|
|
240
|
+
let released = rb_self.core.borrow_mut().0.release(task, resource);
|
|
241
|
+
Ok(match released {
|
|
242
|
+
Ok(()) => symbol(ruby, "released").as_value(),
|
|
243
|
+
Err(refused) => refusal(ruby, refused),
|
|
244
|
+
})
|
|
148
245
|
}
|
|
149
246
|
|
|
150
|
-
fn cancel_wait(rb_self: Obj<Self>,
|
|
247
|
+
fn cancel_wait(ruby: &Ruby, rb_self: Obj<Self>, task: Value) -> Result<Value, Error> {
|
|
151
248
|
rb_self.check_frozen()?;
|
|
152
|
-
let
|
|
153
|
-
let
|
|
154
|
-
|
|
155
|
-
|
|
249
|
+
let task = integer(task, "task")?;
|
|
250
|
+
let cancelled = rb_self.core.borrow_mut().0.cancel_wait(task);
|
|
251
|
+
Ok(match cancelled {
|
|
252
|
+
Ok(cancelling) => Cancellings::symbol(ruby, cancelling).as_value(),
|
|
253
|
+
Err(refused) => refusal(ruby, refused),
|
|
254
|
+
})
|
|
156
255
|
}
|
|
157
256
|
|
|
158
257
|
fn effective_priorities(&self) -> Vec<u32> {
|
|
159
|
-
self.core.borrow().effective_priorities()
|
|
258
|
+
self.core.borrow().0.effective_priorities()
|
|
160
259
|
}
|
|
161
260
|
|
|
162
|
-
|
|
163
|
-
|
|
261
|
+
fn owner(&self, resource: Value) -> Result<Option<usize>, Error> {
|
|
262
|
+
let resource = integer(resource, "resource")?;
|
|
263
|
+
Ok(self.core.borrow().0.owner(resource))
|
|
164
264
|
}
|
|
165
|
-
|
|
166
|
-
|
|
265
|
+
|
|
266
|
+
fn waiting_on(&self, task: Value) -> Result<Option<usize>, Error> {
|
|
267
|
+
let task = integer(task, "task")?;
|
|
268
|
+
Ok(self.core.borrow().0.waiting_on(task))
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
fn protocol(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
272
|
+
let protocol = rb_self.core.borrow().0.protocol();
|
|
273
|
+
Protocols::symbol(ruby, protocol).into_value_with(ruby)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
fn task_count(&self) -> usize {
|
|
277
|
+
self.core.borrow().0.task_count()
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
fn resource_count(&self) -> usize {
|
|
281
|
+
self.core.borrow().0.resource_count()
|
|
167
282
|
}
|
|
168
283
|
}
|
|
169
284
|
|
|
170
285
|
pub fn define(ruby: &Ruby, module: RModule) -> Result<(), Error> {
|
|
286
|
+
// Held for good: removing the constant must not let the GC take it.
|
|
287
|
+
let blocked = Class::resolve(ruby, module, "Blocked")?;
|
|
288
|
+
CLASSES.set(ruby, Classes { blocked })?;
|
|
289
|
+
module.define_singleton_method("__blocked", function!(checked_blocked, 1))?;
|
|
171
290
|
let class = module.define_class("Arbiter", ruby.class_object())?;
|
|
172
|
-
class.
|
|
173
|
-
class.
|
|
174
|
-
class.
|
|
175
|
-
class.define_method("
|
|
291
|
+
class.const_set("TASK_CAPACITY", CAPACITY)?;
|
|
292
|
+
class.const_set("RESOURCE_CAPACITY", CAPACITY)?;
|
|
293
|
+
class.define_singleton_method("new", function!(Wrapper::new, 3))?;
|
|
294
|
+
class.define_method("acquire", method!(Wrapper::acquire, 2))?;
|
|
295
|
+
class.define_method("release", method!(Wrapper::release, 2))?;
|
|
296
|
+
class.define_method("cancel_wait", method!(Wrapper::cancel_wait, 1))?;
|
|
176
297
|
class.define_method(
|
|
177
298
|
"effective_priorities",
|
|
178
299
|
method!(Wrapper::effective_priorities, 0),
|
|
179
300
|
)?;
|
|
301
|
+
class.define_method("owner", method!(Wrapper::owner, 1))?;
|
|
302
|
+
class.define_method("waiting_on", method!(Wrapper::waiting_on, 1))?;
|
|
303
|
+
class.define_method("protocol", method!(Wrapper::protocol, 0))?;
|
|
180
304
|
class.define_method("task_count", method!(Wrapper::task_count, 0))?;
|
|
181
|
-
class.define_method("resource_count", method!(Wrapper::resource_count, 0))
|
|
305
|
+
class.define_method("resource_count", method!(Wrapper::resource_count, 0))?;
|
|
306
|
+
// `new` answers a Wrapper: fix its class now, so building one runs no Ruby.
|
|
307
|
+
Class::pin_wrapped::<Wrapper>(ruby, class).map(drop)
|
|
182
308
|
}
|
|
@@ -2,20 +2,3 @@
|
|
|
2
2
|
|
|
3
3
|
mod arbiter;
|
|
4
4
|
mod init;
|
|
5
|
-
|
|
6
|
-
use magnus::{Error, Integer, Ruby, TryConvert, Value};
|
|
7
|
-
|
|
8
|
-
#[derive(Clone, Copy)]
|
|
9
|
-
pub(crate) struct Count(pub(crate) u32);
|
|
10
|
-
|
|
11
|
-
impl TryConvert for Count {
|
|
12
|
-
fn try_convert(value: Value) -> Result<Self, Error> {
|
|
13
|
-
if Integer::from_value(value).is_none() {
|
|
14
|
-
return Err(Error::new(
|
|
15
|
-
Ruby::get_with(value).exception_type_error(),
|
|
16
|
-
"expected an Integer",
|
|
17
|
-
));
|
|
18
|
-
}
|
|
19
|
-
u32::try_convert(value).map(Self)
|
|
20
|
-
}
|
|
21
|
-
}
|
data/lib/arbiter_machines.rb
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'arbiter_machines/version'
|
|
4
|
+
|
|
5
|
+
module ArbiterMachines
|
|
6
|
+
# The core's `Acquisition::Blocked`: the task must wait, and its wait on
|
|
7
|
+
# `resource` (a resource index) is now recorded. Like every value the core
|
|
8
|
+
# can answer, `resource` is a non-negative Integer that fits a usize: anything
|
|
9
|
+
# else raises RangeError (TypeError for a non-Integer).
|
|
10
|
+
Blocked = Data.define(:resource) do
|
|
11
|
+
def initialize(resource:)
|
|
12
|
+
resource = ArbiterMachines.__send__(:__blocked, resource)
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Native is required: there is no pure-Ruby fallback. It validates and builds
|
|
19
|
+
# the Data value above.
|
|
4
20
|
require 'arbiter_machines_native/arbiter_machines_native'
|
|
21
|
+
|
|
22
|
+
module ArbiterMachines
|
|
23
|
+
private_class_method :__blocked
|
|
24
|
+
|
|
25
|
+
# Sealed: no subclass can skip the check by overriding `initialize`, and no
|
|
26
|
+
# bare `allocate` builds a value that never ran it.
|
|
27
|
+
Blocked.singleton_class.undef_method(:allocate)
|
|
28
|
+
Blocked.define_singleton_method(:inherited) { |_| raise TypeError, "#{Blocked} cannot be subclassed" }
|
|
29
|
+
end
|
data/sig/arbiter_machines.rbs
CHANGED
|
@@ -1,12 +1,59 @@
|
|
|
1
1
|
module ArbiterMachines
|
|
2
2
|
VERSION: String
|
|
3
|
+
|
|
4
|
+
# Refusals: the call was not honoured and changed nothing. Every reason has
|
|
5
|
+
# its own symbol; there is no catch-all.
|
|
6
|
+
type refusal = :unknown_task | :unknown_resource | :above_ceiling | :deadlock | :unowned | :wrong_owner
|
|
7
|
+
|
|
8
|
+
type protocol = :inheritance | :immediate_ceiling
|
|
9
|
+
|
|
10
|
+
# The task must wait; its wait on `resource` is recorded. `resource` is a
|
|
11
|
+
# non-negative Integer that fits a usize: RangeError otherwise (TypeError for
|
|
12
|
+
# a non-Integer), whether the arbiter built it or you did. Sealed: subclassing
|
|
13
|
+
# raises TypeError and `allocate` is undefined.
|
|
14
|
+
class Blocked < Data
|
|
15
|
+
attr_reader resource: Integer
|
|
16
|
+
def self.new: (Integer resource) -> Blocked
|
|
17
|
+
| (resource: Integer) -> Blocked
|
|
18
|
+
end
|
|
19
|
+
|
|
3
20
|
class Arbiter
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
21
|
+
# Every count of tasks and of resources in 0..8 is compiled in.
|
|
22
|
+
TASK_CAPACITY: Integer
|
|
23
|
+
RESOURCE_CAPACITY: Integer
|
|
24
|
+
|
|
25
|
+
# Positional, like `Arbiter::new(base, ceilings, protocol)`: one base
|
|
26
|
+
# priority per task and one ceiling per resource, Integers in 0..2**32.
|
|
27
|
+
# TypeError for a non-Array, a non-Integer or a non-Symbol; RangeError for
|
|
28
|
+
# a value out of range, more than 8 tasks or resources, or an unknown protocol.
|
|
29
|
+
def self.new: (Array[Integer] base, Array[Integer] ceilings, protocol protocol) -> Arbiter
|
|
30
|
+
# Honoured: :granted, :already_owned (a repeat, changes nothing), Blocked
|
|
31
|
+
# (the wait is recorded; asked again while the same resource blocks, the
|
|
32
|
+
# same answer and nothing changes). Refused: :unknown_task,
|
|
33
|
+
# :unknown_resource, :above_ceiling, :deadlock. Not applied when building
|
|
34
|
+
# the Blocked raises (that exception), freezes the arbiter (FrozenError) or
|
|
35
|
+
# changes it (RuntimeError).
|
|
36
|
+
def acquire: (Integer task, Integer resource) -> (:granted | :already_owned | Blocked | :unknown_task | :unknown_resource | :above_ceiling | :deadlock)
|
|
37
|
+
# Honoured: :released (every wait on the resource is cleared). Refused:
|
|
38
|
+
# :unknown_task, :unknown_resource, :unowned (a repeated release included:
|
|
39
|
+
# no history is kept), :wrong_owner.
|
|
40
|
+
def release: (Integer task, Integer resource) -> (:released | :unknown_task | :unknown_resource | :unowned | :wrong_owner)
|
|
41
|
+
# Honoured: :cancelled, :not_waiting (a repeat, changes nothing).
|
|
42
|
+
# Refused: :unknown_task.
|
|
43
|
+
def cancel_wait: (Integer task) -> (:cancelled | :not_waiting | :unknown_task)
|
|
44
|
+
# A fresh Array, one Integer per task.
|
|
8
45
|
def effective_priorities: () -> Array[Integer]
|
|
46
|
+
# The task owning `resource`; nil when it is free or there is no such resource.
|
|
47
|
+
def owner: (Integer resource) -> Integer?
|
|
48
|
+
# The resource `task` waits on; nil when it is not waiting or there is no such task.
|
|
49
|
+
def waiting_on: (Integer task) -> Integer?
|
|
50
|
+
def protocol: () -> protocol
|
|
9
51
|
def task_count: () -> Integer
|
|
10
52
|
def resource_count: () -> Integer
|
|
11
53
|
end
|
|
54
|
+
|
|
55
|
+
# Native check behind `Blocked#initialize`: the resource back when it is a
|
|
56
|
+
# non-negative Integer that fits a usize, RangeError (TypeError for a
|
|
57
|
+
# non-Integer) otherwise.
|
|
58
|
+
private def self.__blocked: (Integer resource) -> Integer
|
|
12
59
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: arbiter_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -29,8 +29,9 @@ dependencies:
|
|
|
29
29
|
- - ">="
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
31
|
version: 0.9.130
|
|
32
|
-
description: Thin Ruby adapter to the standalone allocation-free arbiter-machines
|
|
33
|
-
Rust core
|
|
32
|
+
description: 'Thin Ruby adapter to the standalone allocation-free arbiter-machines
|
|
33
|
+
Rust core: who may take a resource, who waits, who inherits priority, and who would
|
|
34
|
+
deadlock. The caller takes and releases the actual locks.'
|
|
34
35
|
email:
|
|
35
36
|
- oss@seuros.com
|
|
36
37
|
executables: []
|
|
@@ -39,6 +40,7 @@ extensions:
|
|
|
39
40
|
extra_rdoc_files: []
|
|
40
41
|
files:
|
|
41
42
|
- CHANGELOG.md
|
|
43
|
+
- Cargo.lock
|
|
42
44
|
- Cargo.toml
|
|
43
45
|
- LICENSE.txt
|
|
44
46
|
- README.md
|