cdpt 0.1.0

An automatic, safe, and concurrent garbage collector for Rust
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! The global epoch

use std::sync::atomic::{AtomicUsize, Ordering};

/// An epoch that can be marked as pinned or unpinned.
///
/// Internally, the epoch is represented as an integer that wraps around at some unspecified point
/// and a flag that represents whether it is pinned or unpinned.
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub(crate) struct Epoch {
    /// The bits hold the following (from least to most significant):
    /// 1. pinned (1 bit): set if pinned.
    /// 2. phase number (2 bits): refers to the current phase (e.g., normal, tracing).
    /// 3. white color (1 bit): flips after a successful completion of a collection cycle.
    /// 4. timestamp (rest): increases on every phase transition.
    data: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Phase {
    /// Normal phase.
    N = 0b00,
    /// Root tracing phase.
    RT = 0b01,
    /// Completion tracing phase.
    CT = 0b10,
}

impl From<usize> for Phase {
    fn from(value: usize) -> Self {
        match value {
            0b00 => Self::N,
            0b01 => Self::RT,
            0b10 => Self::CT,
            _ => panic!("Invalid phase number"),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Color {
    C0 = 0,
    C1 = 1,
}

impl From<usize> for Color {
    fn from(value: usize) -> Self {
        if value == 0 { Self::C0 } else { Self::C1 }
    }
}

impl Color {
    pub fn flip(self) -> Self {
        match self {
            Color::C0 => Color::C1,
            Color::C1 => Color::C0,
        }
    }
}

impl Epoch {
    const PINNED_POS: usize = 0;
    const PHASE_NUMBER_POS: usize = Self::PINNED_POS + 1;
    const COLOR_POS: usize = Self::PHASE_NUMBER_POS + 2;
    const TIMESTAMP_POS: usize = Self::COLOR_POS + 1;

    const PINNED_BIT: usize = 0b1 << Self::PINNED_POS;
    const PHASE_NUMBER_BITS: usize = 0b11 << Self::PHASE_NUMBER_POS;
    const COLOR_BIT: usize = 0b1 << Self::COLOR_POS;
    const TIMESTAMP_BITS: usize = usize::MAX << Self::TIMESTAMP_POS;

    /// Returns the starting epoch in unpinned state.
    #[inline]
    pub(crate) fn starting() -> Self {
        Self::default()
    }

    /// Returns `true` if the epoch is marked as pinned.
    #[inline]
    pub(crate) fn is_pinned(self) -> bool {
        (self.data & Self::PINNED_BIT) > 0
    }

    /// Returns the same epoch, but marked as pinned.
    #[inline]
    pub(crate) fn pinned(self) -> Self {
        Self {
            data: self.data | Self::PINNED_BIT,
        }
    }

    /// Returns the same epoch, but marked as unpinned.
    #[inline]
    #[allow(unused)]
    pub(crate) fn unpinned(self) -> Self {
        Self {
            data: self.data & !Self::PINNED_BIT,
        }
    }

    #[inline]
    pub(crate) fn phase(self) -> Phase {
        ((self.data & Self::PHASE_NUMBER_BITS) >> Self::PHASE_NUMBER_POS).into()
    }

    #[inline]
    pub(crate) fn with_phase(self, phase: Phase) -> Self {
        Self {
            data: (self.data & !Self::PHASE_NUMBER_BITS)
                | ((phase as usize) << Self::PHASE_NUMBER_POS),
        }
    }

    #[inline]
    pub(crate) fn color(self) -> Color {
        ((self.data & Self::COLOR_BIT) >> Self::COLOR_POS).into()
    }

    #[inline]
    pub(crate) fn with_color(self, color: Color) -> Self {
        Self {
            data: (self.data & !Self::COLOR_BIT) | ((color as usize) << Self::COLOR_POS),
        }
    }

    #[inline]
    pub(crate) fn timestamp(self) -> usize {
        (self.data & Self::TIMESTAMP_BITS) >> Self::TIMESTAMP_POS
    }

    #[inline]
    pub(crate) fn with_timestamp(self, value: usize) -> Self {
        Self {
            data: (self.data & !Self::TIMESTAMP_BITS) | (value << Self::TIMESTAMP_POS),
        }
    }
}

/// An atomic value that holds an `Epoch`.
#[derive(Default, Debug)]
pub(crate) struct AtomicEpoch {
    /// Since `Epoch` is just a wrapper around `usize`, an `AtomicEpoch` is similarly represented
    /// using an `AtomicUsize`.
    data: AtomicUsize,
}

impl AtomicEpoch {
    /// Creates a new atomic epoch.
    #[inline]
    pub(crate) fn new(epoch: Epoch) -> Self {
        let data = AtomicUsize::new(epoch.data);
        Self { data }
    }

    /// Loads a value from the atomic epoch.
    #[inline]
    pub(crate) fn load(&self, ord: Ordering) -> Epoch {
        Epoch {
            data: self.data.load(ord),
        }
    }

    /// Stores a value into the atomic epoch.
    #[inline]
    pub(crate) fn store(&self, epoch: Epoch, ord: Ordering) {
        self.data.store(epoch.data, ord);
    }

    /// Stores a value into the atomic epoch if the current value is the same as `current`.
    ///
    /// The return value is a result indicating whether the new value was written and containing
    /// the previous value. On success this value is guaranteed to be equal to `current`.
    ///
    /// This method takes two `Ordering` arguments to describe the memory
    /// ordering of this operation. `success` describes the required ordering for the
    /// read-modify-write operation that takes place if the comparison with `current` succeeds.
    /// `failure` describes the required ordering for the load operation that takes place when
    /// the comparison fails. Using `Acquire` as success ordering makes the store part
    /// of this operation `Relaxed`, and using `Release` makes the successful load
    /// `Relaxed`. The failure ordering can only be `SeqCst`, `Acquire` or `Relaxed`
    /// and must be equivalent to or weaker than the success ordering.
    #[inline]
    #[allow(unused)]
    pub(crate) fn compare_exchange(
        &self,
        current: Epoch,
        new: Epoch,
        success: Ordering,
        failure: Ordering,
    ) -> Result<Epoch, Epoch> {
        match self
            .data
            .compare_exchange(current.data, new.data, success, failure)
        {
            Ok(data) => Ok(Epoch { data }),
            Err(data) => Err(Epoch { data }),
        }
    }

    /// Loads a value "non-atomically" from the atomic epoch.
    ///
    /// # Safety
    ///
    /// There must be no interleaving writes on this variable.
    ///
    /// Note that read-read races, where one access is atomic and one is not, are not UB.
    /// * https://doc.rust-lang.org/nightly/std/sync/atomic/index.html#memory-model-for-atomic-accesses
    #[inline]
    pub(crate) unsafe fn load_non_atomic(&self) -> Epoch {
        Epoch {
            data: unsafe { *self.data.as_ptr() },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ---- Color tests ----

    #[test]
    fn color_flip() {
        assert_eq!(Color::C0.flip(), Color::C1);
        assert_eq!(Color::C1.flip(), Color::C0);
    }

    #[test]
    fn color_flip_roundtrip() {
        assert_eq!(Color::C0.flip().flip(), Color::C0);
        assert_eq!(Color::C1.flip().flip(), Color::C1);
    }

    #[test]
    fn color_from_usize() {
        assert_eq!(Color::from(0), Color::C0);
        assert_eq!(Color::from(1), Color::C1);
        // Non-zero values map to C1.
        assert_eq!(Color::from(42), Color::C1);
    }

    #[test]
    fn color_equality() {
        assert_eq!(Color::C0, Color::C0);
        assert_eq!(Color::C1, Color::C1);
        assert_ne!(Color::C0, Color::C1);
    }

    // ---- Phase tests ----

    #[test]
    fn phase_from_usize() {
        assert_eq!(Phase::from(0b00), Phase::N);
        assert_eq!(Phase::from(0b01), Phase::RT);
        assert_eq!(Phase::from(0b10), Phase::CT);
    }

    #[test]
    #[should_panic(expected = "Invalid phase number")]
    fn phase_from_invalid() {
        let _ = Phase::from(0b11);
    }

    #[test]
    fn phase_values() {
        assert_eq!(Phase::N as usize, 0b00);
        assert_eq!(Phase::RT as usize, 0b01);
        assert_eq!(Phase::CT as usize, 0b10);
    }

    // ---- Epoch tests ----

    #[test]
    fn epoch_starting_is_unpinned() {
        let e = Epoch::starting();
        assert!(!e.is_pinned());
        assert_eq!(e.phase(), Phase::N);
        assert_eq!(e.color(), Color::C0);
        assert_eq!(e.timestamp(), 0);
    }

    #[test]
    fn epoch_pinned_unpinned() {
        let e = Epoch::starting();
        assert!(!e.is_pinned());

        let pinned = e.pinned();
        assert!(pinned.is_pinned());

        let unpinned = pinned.unpinned();
        assert!(!unpinned.is_pinned());
    }

    #[test]
    fn epoch_pinned_preserves_fields() {
        let e = Epoch::starting()
            .with_phase(Phase::RT)
            .with_color(Color::C1)
            .with_timestamp(42);

        let pinned = e.pinned();
        assert!(pinned.is_pinned());
        assert_eq!(pinned.phase(), Phase::RT);
        assert_eq!(pinned.color(), Color::C1);
        assert_eq!(pinned.timestamp(), 42);
    }

    #[test]
    fn epoch_phase_transitions() {
        let e = Epoch::starting();
        assert_eq!(e.phase(), Phase::N);

        let rt = e.with_phase(Phase::RT);
        assert_eq!(rt.phase(), Phase::RT);

        let ct = rt.with_phase(Phase::CT);
        assert_eq!(ct.phase(), Phase::CT);

        let n = ct.with_phase(Phase::N);
        assert_eq!(n.phase(), Phase::N);
    }

    #[test]
    fn epoch_phase_preserves_other_fields() {
        let e = Epoch::starting()
            .pinned()
            .with_color(Color::C1)
            .with_timestamp(7);

        let rt = e.with_phase(Phase::RT);
        assert!(rt.is_pinned());
        assert_eq!(rt.color(), Color::C1);
        assert_eq!(rt.timestamp(), 7);
    }

    #[test]
    fn epoch_color() {
        let e = Epoch::starting();
        assert_eq!(e.color(), Color::C0);

        let c1 = e.with_color(Color::C1);
        assert_eq!(c1.color(), Color::C1);

        let c0 = c1.with_color(Color::C0);
        assert_eq!(c0.color(), Color::C0);
    }

    #[test]
    fn epoch_color_preserves_other_fields() {
        let e = Epoch::starting()
            .pinned()
            .with_phase(Phase::CT)
            .with_timestamp(99);

        let flipped = e.with_color(Color::C1);
        assert!(flipped.is_pinned());
        assert_eq!(flipped.phase(), Phase::CT);
        assert_eq!(flipped.timestamp(), 99);
    }

    #[test]
    fn epoch_timestamp() {
        let e = Epoch::starting();
        assert_eq!(e.timestamp(), 0);

        let t1 = e.with_timestamp(1);
        assert_eq!(t1.timestamp(), 1);

        let t100 = e.with_timestamp(100);
        assert_eq!(t100.timestamp(), 100);
    }

    #[test]
    fn epoch_timestamp_preserves_other_fields() {
        let e = Epoch::starting()
            .pinned()
            .with_phase(Phase::RT)
            .with_color(Color::C1);

        let ts = e.with_timestamp(55);
        assert!(ts.is_pinned());
        assert_eq!(ts.phase(), Phase::RT);
        assert_eq!(ts.color(), Color::C1);
    }

    #[test]
    fn epoch_all_fields_combined() {
        let e = Epoch::starting()
            .with_phase(Phase::CT)
            .with_color(Color::C1)
            .with_timestamp(12345)
            .pinned();

        assert!(e.is_pinned());
        assert_eq!(e.phase(), Phase::CT);
        assert_eq!(e.color(), Color::C1);
        assert_eq!(e.timestamp(), 12345);

        // Change each field independently and verify.
        let e2 = e.unpinned();
        assert!(!e2.is_pinned());
        assert_eq!(e2.phase(), Phase::CT);
        assert_eq!(e2.color(), Color::C1);
        assert_eq!(e2.timestamp(), 12345);
    }

    #[test]
    fn epoch_equality() {
        let a = Epoch::starting().with_phase(Phase::RT).with_timestamp(5);
        let b = Epoch::starting().with_phase(Phase::RT).with_timestamp(5);
        assert_eq!(a, b);

        let c = a.with_timestamp(6);
        assert_ne!(a, c);
    }

    // ---- AtomicEpoch tests ----

    #[test]
    fn atomic_epoch_load_store() {
        let ae = AtomicEpoch::default();
        let e = ae.load(Ordering::SeqCst);
        assert_eq!(e, Epoch::starting());

        let new = Epoch::starting()
            .with_phase(Phase::RT)
            .with_color(Color::C1)
            .with_timestamp(3)
            .pinned();
        ae.store(new, Ordering::SeqCst);

        let loaded = ae.load(Ordering::SeqCst);
        assert_eq!(loaded, new);
    }

    #[test]
    fn atomic_epoch_compare_exchange_success() {
        let initial = Epoch::starting().with_timestamp(1);
        let ae = AtomicEpoch::new(initial);

        let new = initial.with_phase(Phase::RT);
        let result = ae.compare_exchange(initial, new, Ordering::SeqCst, Ordering::SeqCst);
        assert!(result.is_ok());
        assert_eq!(ae.load(Ordering::SeqCst), new);
    }

    #[test]
    fn atomic_epoch_compare_exchange_failure() {
        let initial = Epoch::starting().with_timestamp(1);
        let ae = AtomicEpoch::new(initial);

        // Change the value first.
        let updated = initial.with_phase(Phase::CT);
        ae.store(updated, Ordering::SeqCst);

        // CAS with stale value should fail.
        let new = initial.with_phase(Phase::RT);
        let result = ae.compare_exchange(initial, new, Ordering::SeqCst, Ordering::SeqCst);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), updated);
    }

    #[test]
    fn atomic_epoch_load_non_atomic() {
        let e = Epoch::starting().with_phase(Phase::CT).with_timestamp(77);
        let ae = AtomicEpoch::new(e);

        // Safety: no concurrent writes in this single-threaded test.
        let loaded = unsafe { ae.load_non_atomic() };
        assert_eq!(loaded, e);
    }
}