o3 0.52.1

shared-nothing primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::{marker, mem, ptr};

use crate::{
    alloc,
    base::marker::ThreadBound,
    depot::{
        self,
        heap::{
            self, indexed,
            indexed::resident::{
                Borrowed, Control, Entry, Min, Owned, Peek, Phase, Slot, Vacant, Value, Values,
                phase,
            },
        },
        index::Initialized,
        queue::links,
    },
};

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Tag {
    Common,
    Control,
    Data,
    Active,
}

#[repr(C)]
pub(super) union Stored<T: Values> {
    scheduled: mem::ManuallyDrop<T::Scheduled>,
    idle: mem::ManuallyDrop<T::Idle>,
    control: mem::ManuallyDrop<T::Control>,
    data: mem::ManuallyDrop<T::Data>,
    active: mem::ManuallyDrop<T::Active>,
}

impl<T: Values> Stored<T> {
    fn new(value: Owned<T>) -> Self {
        match value {
            Value::Scheduled(value) => Self {
                scheduled: mem::ManuallyDrop::new(value),
            },
            Value::Idle(value) => Self {
                idle: mem::ManuallyDrop::new(value),
            },
            Value::Control(value) => Self {
                control: mem::ManuallyDrop::new(value),
            },
            Value::Data(value) => Self {
                data: mem::ManuallyDrop::new(value),
            },
            Value::Active(value) => Self {
                active: mem::ManuallyDrop::new(value),
            },
        }
    }

    // SAFETY: callers retain the exact initialized field selected by the owner.
    unsafe fn get<F>(&self) -> &F {
        unsafe { &*ptr::from_ref(self).cast::<F>() }
    }

    unsafe fn get_mut<F>(&mut self) -> &mut F {
        unsafe { &mut *ptr::from_mut(self).cast::<F>() }
    }

    unsafe fn take<F>(&mut self) -> F {
        unsafe { ptr::read(ptr::from_mut(self).cast::<F>()) }
    }

    unsafe fn drop_field<F>(&mut self) {
        unsafe { ptr::drop_in_place(ptr::from_mut(self).cast::<F>()) };
    }
}

pub(super) struct Node<T: Values> {
    pub(super) common: T::Common,
    hook: links::Hook,
    tag: Tag,
    value: Stored<T>,
}

impl<T: Values> Node<T> {
    pub(super) fn scheduled(index: usize, common: T::Common, value: T::Scheduled) -> Self {
        Self {
            common,
            hook: links::Hook::vacant(index as u32, links::Index::END),
            tag: Tag::Common,
            value: Stored::new(Value::Scheduled(value)),
        }
    }
}

pub(super) struct Core<T: Values> {
    pub(super) nodes: Box<[Node<T>]>,
    pub(super) control: links::Chain,
    pub(super) data: links::Chain,
    pub(super) active: u32,
    _thread: ThreadBound,
}

impl<T: Values> Core<T> {
    pub(super) fn new(nodes: Box<[Node<T>]>) -> Self {
        Self {
            nodes,
            control: links::Chain::new(),
            data: links::Chain::new(),
            active: 0,
            _thread: ThreadBound::NEW,
        }
    }
}

// SAFETY: the fixed original node allocation retains one distinct stable hook per slot.
unsafe impl<T: Values> depot::raw::Hooks for Core<T> {
    unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
        unsafe { &self.nodes.get_unchecked(index.get()).hook }
    }
}

impl<'a, P: Ord, T: Values, H, F: Phase<T>, C: alloc::raw::Extent> Slot<'a, P, T, H, F, C> {
    // SAFETY: both disjoint loans retain the same owner's exact index, heap state and F field.
    unsafe fn new(
        order: &'a mut indexed::Min<P, C>,
        core: &'a mut Core<T>,
        index: links::Index,
        state: H,
    ) -> Self {
        Self {
            order,
            core,
            index,
            state,
            field: marker::PhantomData,
        }
    }

    pub fn index(&self) -> usize {
        self.index.get()
    }

    pub fn get(&self) -> (&T::Common, &F::Item) {
        // SAFETY: the retained slot admits exactly this resident and field.
        let node = unsafe { self.core.nodes.get_unchecked(self.index.get()) };
        (&node.common, unsafe { node.value.get() })
    }

    pub fn get_mut(&mut self) -> (&mut T::Common, &mut F::Item) {
        // SAFETY: the exclusive owner prevents field replacement during this loan.
        let node = unsafe { self.core.nodes.get_unchecked_mut(self.index.get()) };
        (&mut node.common, unsafe { node.value.get_mut() })
    }

    pub fn change<G: Phase<T> + super::Retained<H>>(
        self,
        value: G::Item,
    ) -> (Slot<'a, P, T, H, G, C>, F::Item)
    where
        H: Copy,
    {
        let state = self.state;
        self.transition::<G, _>(state, value)
    }

    fn transition<G: Phase<T>, N>(
        self,
        state: N,
        value: G::Item,
    ) -> (Slot<'a, P, T, N, G, C>, F::Item) {
        let value = Stored::new(G::value(value));
        if F::TAG != G::TAG {
            // SAFETY: F owns this slot's sole membership; the successor cannot share its hook.
            unsafe {
                match F::TAG {
                    Tag::Control => {
                        self.core.control.unlink(self.core, self.index);
                    }
                    Tag::Data => {
                        self.core.data.unlink(self.core, self.index);
                    }
                    Tag::Active => {
                        // SAFETY: this retained Active field contributes one distinct physical slot.
                        self.core.active -= 1;
                    }
                    Tag::Common => {}
                }
                match G::TAG {
                    Tag::Control => self.core.control.link_back(self.core, self.index),
                    Tag::Data => self.core.data.link_back(self.core, self.index),
                    Tag::Active => {
                        // SAFETY: this retained non-Active slot is excluded from active. The complete
                        // compact domain bounds active + 1 by its admitted u32 capacity.
                        self.core.active += 1;
                    }
                    Tag::Common => {}
                }
                self.core.nodes.get_unchecked_mut(self.index.get()).tag = G::TAG;
            }
        }
        // SAFETY: F is the sole initialized field; transition consumes it exactly once.
        let node = unsafe { self.core.nodes.get_unchecked_mut(self.index.get()) };
        let old = unsafe { node.value.take::<F::Item>() };
        node.value = value;
        (
            Slot {
                order: self.order,
                core: self.core,
                index: self.index,
                state,
                field: marker::PhantomData,
            },
            old,
        )
    }
}

impl<'a, P: Ord, T: Values, F: Phase<T>, C: alloc::raw::Extent> Slot<'a, P, T, usize, F, C> {
    pub fn replace(mut self, priority: P) -> (Self, P) {
        // SAFETY: this exclusive slot retains its exact initialized heap position.
        let entry = unsafe { heap::Entry::new(&mut *self.order, self.state, ()) };
        let (order, old) = indexed::Occupied { entry }.replace(priority);
        self.state = order.entry.position;
        (self, old)
    }

    pub fn retire(self, value: T::Idle) -> (Vacant<'a, P, T, phase::Idle, C>, F::Item, P) {
        // SAFETY: this exclusive slot retains its exact initialized heap position.
        let entry = unsafe { heap::Entry::new(&mut *self.order, self.state, ()) };
        let (vacant, priority) = indexed::Occupied { entry }.remove();
        let direct = vacant.entry.value;
        let (slot, old) = self.transition::<phase::Idle, _>(direct, value);
        (slot, old, priority)
    }
}

impl<'a, P: Ord, T: Values, F: Phase<T>, C: alloc::raw::Extent> Slot<'a, P, T, heap::Direct, F, C> {
    pub fn schedule(
        self,
        priority: P,
        value: T::Control,
    ) -> (Slot<'a, P, T, usize, phase::Control, C>, F::Item) {
        // SAFETY: the exclusive absent identity retains spare capacity in this exact heap.
        let vacant = unsafe { heap::Vacancy::new(&mut *self.order, self.state) };
        let entry = indexed::Vacant { entry: vacant }.insert(priority);
        let position = entry.entry.position;
        self.transition::<phase::Control, _>(position, value)
    }
}

pub(super) fn view<P: Ord, T: Values, C: alloc::raw::Extent>(
    owner: &Min<P, T, C>,
    index: usize,
) -> Option<(&T::Common, Borrowed<'_, T>)> {
    use heap::raw::Store as _;
    let node = owner.core.nodes.get(index)?;
    // SAFETY: the single tag and exact heap membership determine the sole initialized field.
    let value = unsafe {
        match node.tag {
            Tag::Common
                if *owner.order.positions().get_unchecked(index)
                    != <u32 as depot::raw::Link>::NONE =>
            {
                Value::Scheduled(node.value.get())
            }
            Tag::Common => Value::Idle(node.value.get()),
            Tag::Control => Value::Control(node.value.get()),
            Tag::Data => Value::Data(node.value.get()),
            Tag::Active => Value::Active(node.value.get()),
        }
    };
    Some((&node.common, value))
}

pub(super) fn peek<P: Ord, T: Values, C: alloc::raw::Extent>(
    owner: &Min<P, T, C>,
) -> Option<Peek<'_, P, T>> {
    let (index, priority) = owner.order.peek()?;
    // SAFETY: the actual heap root retains a represented full resident, whose tag can only
    // be Common/Scheduled or Control. Data and Active transitions keep the heap vacant.
    let node = unsafe { owner.core.nodes.get_unchecked(index) };
    let value = if node.tag == Tag::Control {
        None
    } else {
        Some(unsafe { node.value.get() })
    };
    Some((index, priority, &node.common, value))
}

unsafe fn vacant<'a, P: Ord, T: Values, C: alloc::raw::Extent, F: Phase<T>>(
    order: &'a mut indexed::Min<P, C>,
    core: &'a mut Core<T>,
    index: links::Index,
) -> Vacant<'a, P, T, F, C> {
    // SAFETY: caller retains the exact F field and its vacant heap position in this owner.
    let live: depot::raw::Proof<_, Initialized<indexed::Empty>> =
        unsafe { depot::raw::Proof::new(((order, core), index.position())) };
    let (order, core) = live.entry();
    unsafe { Slot::new(order.entry.owner, core, index, order.entry.value) }
}

pub(super) fn entry<P: Ord, T: Values, C: alloc::raw::Extent>(
    owner: &mut Min<P, T, C>,
    index: usize,
) -> Option<Entry<'_, P, T, C>> {
    let tag = owner.core.nodes.get(index)?.tag;
    // SAFETY: the fixed node allocation admits this exact compact heap position.
    let index = unsafe { links::Index::new_usize(index) };
    let Min { order, core } = owner;
    Some(match tag {
        // SAFETY: only vacant typed transitions can publish Data or Active fields.
        Tag::Data => Entry::Data(unsafe { vacant(order, core, index) }),
        Tag::Active => Entry::Active(unsafe { vacant(order, core, index) }),
        Tag::Common | Tag::Control => {
            // SAFETY: the original node owner retains this exact initialized reverse-map slot.
            let live: depot::raw::Proof<_, Initialized<()>> =
                unsafe { depot::raw::Proof::new(((order, core), index.position())) };
            let (order, core) = live.entry();
            match (tag, order) {
                (Tag::Control, indexed::Entry::Occupied(order)) => {
                    Entry::Control(Control::Full(unsafe {
                        Slot::new(order.entry.owner, core, index, order.entry.position)
                    }))
                }
                (Tag::Control, indexed::Entry::Vacant(order)) => {
                    Entry::Control(Control::Empty(unsafe {
                        Slot::new(order.entry.owner, core, index, order.entry.value)
                    }))
                }
                (_, indexed::Entry::Occupied(order)) => Entry::Scheduled(unsafe {
                    Slot::new(order.entry.owner, core, index, order.entry.position)
                }),
                (_, indexed::Entry::Vacant(order)) => Entry::Idle(unsafe {
                    Slot::new(order.entry.owner, core, index, order.entry.value)
                }),
            }
        }
    })
}

pub(super) fn control<P: Ord, T: Values, C: alloc::raw::Extent>(
    owner: &mut Min<P, T, C>,
) -> Option<Control<'_, P, T, C>> {
    let Min { order, core } = owner;
    let index = core.control.first()?;
    // SAFETY: this exact control queue retains the node's Control field and represented
    // heap position. Both full and empty Control are legitimate scheduled outcomes.
    let live: depot::raw::Proof<_, Initialized<()>> =
        unsafe { depot::raw::Proof::new(((order, core), index.position())) };
    let (order, core) = live.entry();
    Some(match order {
        indexed::Entry::Occupied(order) => Control::Full(unsafe {
            Slot::new(order.entry.owner, core, index, order.entry.position)
        }),
        indexed::Entry::Vacant(order) => {
            Control::Empty(unsafe { Slot::new(order.entry.owner, core, index, order.entry.value) })
        }
    })
}

pub(super) fn data<P: Ord, T: Values, C: alloc::raw::Extent>(
    owner: &mut Min<P, T, C>,
) -> Option<Vacant<'_, P, T, phase::Data, C>> {
    let Min { order, core } = owner;
    let index = core.data.first()?;
    // SAFETY: this exact queue admits only Data fields; their owned transitions retain
    // the same vacant heap slot until this exclusive source loan is consumed.
    Some(unsafe { vacant(order, core, index) })
}

pub(super) fn clear<P: Ord, T: Values, C: alloc::raw::Extent>(
    owner: &mut Min<P, T, C>,
    mut convert: impl FnMut(&mut T::Common, T::Scheduled) -> T::Idle,
) {
    use heap::raw::Store as _;
    for (node, &position) in owner.core.nodes.iter_mut().zip(owner.order.positions()) {
        if position != <u32 as depot::raw::Link>::NONE && node.tag == Tag::Common {
            // SAFETY: Common plus full membership owns the Scheduled field. Control fields
            // remain untouched; the subsequent heap clear changes only their deadline phase.
            let old = unsafe { node.value.take::<T::Scheduled>() };
            node.value = Stored::new(Value::Idle(convert(&mut node.common, old)));
        }
    }
    owner.order.clear();
}

pub(super) fn destroy<P: Ord, T: Values, C: alloc::raw::Extent>(owner: &mut Min<P, T, C>) {
    use heap::raw::Store as _;
    for (node, &position) in owner.core.nodes.iter_mut().zip(owner.order.positions()) {
        // SAFETY: terminal ownership retains the sole field selected by tag and membership;
        // each payload is dropped once, then normal field drop releases Common and storage.
        unsafe {
            match node.tag {
                Tag::Common if position != <u32 as depot::raw::Link>::NONE => {
                    node.value.drop_field::<T::Scheduled>()
                }
                Tag::Common => node.value.drop_field::<T::Idle>(),
                Tag::Control => node.value.drop_field::<T::Control>(),
                Tag::Data => node.value.drop_field::<T::Data>(),
                Tag::Active => node.value.drop_field::<T::Active>(),
            }
        }
    }
}