swmr-cell 0.3.1

A thread-safe single-writer multi-reader cell with wait-free reads and version-based garbage collection
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
/// Lifecycle and memory safety tests
use crate::SwmrCell;
use std::prelude::v1::*;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

use std::format;
use std::vec;

/// Test 1: local guard lifetime constraint
#[test]
fn test_guard_lifetime_constraint() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();
    let guard = local.pin();
    assert_eq!(*guard, 42);
    // value lifetime bound to guard
}

/// Test 2: Multiple guards simultaneously active
#[test]
fn test_multiple_guards_simultaneously_active() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    let guard1 = local.pin();
    let guard2 = local.pin();

    assert_eq!(*guard1, 42);
    assert_eq!(*guard2, 42);
}

/// Test 3: Guard nested scopes
#[test]
fn test_guard_nested_scopes() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    {
        let guard1 = local.pin();
        assert_eq!(*guard1, 42);

        {
            let guard2 = local.pin();
            assert_eq!(*guard2, 42);
        }

        let guard1_again = local.pin();
        assert_eq!(*guard1_again, 42);
    }
}

/// Test 4: local isolation across threads
#[test]
fn test_reader_isolation_across_threads() {
    let mut cell = SwmrCell::new(0i32);
    let mut handles = vec![];

    for _ in 0..3 {
        let local = cell.local_reader();
        handles.push(thread::spawn(move || {
            let guard = local.pin();
            assert!(*guard >= 0);
        }));
    }

    cell.store(1);

    for h in handles {
        h.join().unwrap();
    }
}

/// Test 5: Writer single threaded constraint
/// The type system enforces this (SwmrCell is !Sync? No, SwmrCell is Send but maybe !Clone),
/// but we can't easily test compilation failure here.
/// We just assume if we can't clone SwmrCell, it's good.
#[test]
fn test_writer_uniqueness() {
    // SwmrCell does not implement Clone (Single Writer)
    // let cell = SwmrCell::new(0);
    // let c2 = cell.clone(); // Should fail to compile
}

/// Test 6: Garbage collection memory safety
#[test]
fn test_garbage_collection_memory_safety() {
    let mut cell = SwmrCell::new(vec![1, 2, 3]);
    let local = cell.local_reader();

    cell.store(vec![4, 5, 6]);
    cell.store(vec![7, 8, 9]);

    let _guard = local.pin();
    cell.collect();

    let _guard2 = local.pin();
}

/// Test 7: EpochPtr drop implementation
/// Covered by swmr_drop test in basic_tests, but here specifically checks leaks?
#[test]
fn test_swmr_drop_implementation() {
    {
        let _c = SwmrCell::new(String::from("test"));
    }
}

/// Test 8: Multiple SwmrCell independence
#[test]
fn test_multiple_swmr_independence() {
    let c1 = SwmrCell::new(10i32);
    let c2 = SwmrCell::new(20i32);
    let c3 = SwmrCell::new(30i32);

    let r1 = c1.local_reader();
    let r2 = c2.local_reader();
    let r3 = c3.local_reader();

    let g1 = r1.pin();
    let g2 = r2.pin();
    let g3 = r3.pin();

    assert_eq!(*g1, 10);
    assert_eq!(*g2, 20);
    assert_eq!(*g3, 30);
}

/// Test 9: Domain clone safety (local creation safety)
#[test]
fn test_reader_creation_safety() {
    let cell = SwmrCell::new(0i32);
    let r1 = cell.local_reader();
    let r2 = cell.local_reader();
    let r3 = cell.local_reader();

    let _g1 = r1.pin();
    let _g2 = r2.pin();
    let _g3 = r3.pin();
}

/// Test 10: Epoch advancement correctness
#[test]
fn test_epoch_advancement_correctness() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

    {
        let guard = local.pin();
        assert_eq!(*guard, 0);
    }

    cell.store(1);

    {
        let guard = local.pin();
        assert_eq!(*guard, 1);
    }
}

/// Test 11: Concurrent read consistency
#[test]
fn test_concurrent_read_consistency() {
    let cell = SwmrCell::new(42i32);
    let check = Arc::new(AtomicUsize::new(0));
    let mut handles = vec![];

    for _ in 0..10 {
        let local = cell.local_reader();
        let c = check.clone();
        handles.push(thread::spawn(move || {
            for _ in 0..100 {
                let guard = local.pin();
                if *guard == 42 {
                    c.fetch_add(1, Ordering::Relaxed);
                }
            }
        }));
    }

    for h in handles {
        h.join().unwrap();
    }

    assert_eq!(check.load(Ordering::Relaxed), 1000);
}

/// Test 12: local exit cleanup
#[test]
fn test_reader_exit_cleanup() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

    let t = thread::spawn(move || {
        let _guard = local.pin();
    });
    t.join().unwrap();
    cell.collect();
}

/// Test 13: Large garbage safe reclamation
#[test]
fn test_large_garbage_safe_reclamation() {
    let mut cell = SwmrCell::new(0i32);
    for i in 0..1000 {
        cell.store(i);
    }
    cell.collect();
}

/// Test 14: Complex type lifetime management
#[test]
fn test_complex_type_lifetime_management() {
    #[derive(Debug)]
    struct ComplexData {
        id: usize,
        values: Vec<i32>,
        name: String,
    }

    let data = ComplexData {
        id: 1,
        values: vec![1, 2, 3, 4, 5],
        name: String::from("test"),
    };

    let cell = SwmrCell::new(data);
    let local = cell.local_reader();

    let guard = local.pin();
    assert_eq!(guard.id, 1);
    assert_eq!(guard.values.len(), 5);
    assert_eq!(guard.name, "test");
}

/// Test 15: Data visibility across epochs
#[test]
fn test_data_visibility_across_epochs() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

    let g1 = local.pin();

    assert_eq!(*g1, 0);

    cell.store(1);

    let g2 = local.pin();
    assert_eq!(*g2, 1);

    // Original reader still sees old data if it holds guard
    assert_eq!(*g1, 0);

    // But if it pin again?
    let g1_new = local.pin();
    assert_eq!(*g1_new, 1);
}

/// Test 17: Rapid local switching
#[test]
fn test_rapid_reader_switching() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    for _ in 0..100 {
        let guard = local.pin();
        assert_eq!(*guard, 42);
        drop(guard);

        let guard = local.pin();
        assert_eq!(*guard, 42);
    }
}

/// Test 18: Writer garbage management
#[test]
fn test_writer_garbage_management() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

    {
        let _guard = local.pin();
        for i in 0..50 {
            cell.store(i);
        }
        // Garbage retained
    }
    // local inactive
    cell.collect();
    // Garbage collected
}

/// Test 19: Multiple readers garbage protection
#[test]
fn test_multiple_readers_garbage_protection() {
    let mut cell = SwmrCell::new(0i32);
    let r1 = cell.local_reader();
    let r2 = cell.local_reader();
    let r3 = cell.local_reader();

    let _g1 = r1.pin();
    let _g2 = r2.pin();
    let _g3 = r3.pin();

    for i in 0..100 {
        cell.store(i);
    }

    // Garbage protected
    cell.collect();
}

/// Test 20: Complete lifecycle scenario
#[test]
fn test_complete_lifecycle_scenario() {
    let mut cell = SwmrCell::new(String::from("initial"));
    let mut readers = vec![];
    for _ in 0..5 {
        readers.push(cell.local_reader());
    }

    for round in 0..3 {
        let guards: Vec<_> = readers
            .iter()
            .map(|r: &crate::LocalReader<String>| r.pin())
            .collect();

        for guard in &guards {
            assert!(!(*guard).is_empty());
        }

        cell.store(format!("round_{}", round));

        for i in 0..50 {
            cell.store(format!("garbage_{}", i));
        }

        cell.collect();
    }
}

// ============================================================================
// New API Lifecycle Tests
// ============================================================================

/// Test 21: get() lifetime does not outlive cell
#[test]
fn test_get_lifetime_bound_to_cell() {
    let cell = SwmrCell::new(42i32);
    let value_ref = cell.get();
    assert_eq!(*value_ref, 42);
    // value_ref is valid as long as cell is alive
}

/// Test 22: update() preserves garbage for previous()
#[test]
fn test_update_preserves_previous() {
    let mut cell = SwmrCell::new(1i32);

    cell.update(|v| v + 1);
    assert_eq!(cell.previous(), Some(&1));

    cell.update(|v| v * 2);
    assert_eq!(cell.previous(), Some(&2));
}

/// Test 24: version consistency throughout lifecycle
#[test]
fn test_version_consistency_lifecycle() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

    for i in 0..10 {
        assert_eq!(cell.version(), i);
        assert_eq!(local.version(), i);

        let guard = local.pin();
        assert_eq!(guard.version(), i);
        drop(guard);

        cell.store(i as i32 + 1);
    }
}

/// Test 26: is_pinned lifecycle with clone
#[test]
fn test_is_pinned_lifecycle_with_clone() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    assert!(!local.is_pinned());

    let guard1 = local.pin();
    assert!(local.is_pinned());

    let guard2 = guard1.clone();
    assert!(local.is_pinned());

    drop(guard1);
    assert!(local.is_pinned()); // guard2 still holds

    drop(guard2);
    assert!(!local.is_pinned());
}

/// Test 28: PinGuard version preserved across writes
#[test]
fn test_pin_guard_version_preserved() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

    let guard = local.pin();
    let initial_version = guard.version();

    // Write multiple times
    for i in 1..=10 {
        cell.store(i);
    }

    // Guard version should not change
    assert_eq!(guard.version(), initial_version);

    // But value seen by guard is the snapshot
    assert_eq!(*guard, 0);
}

/// Test 29: Default trait with complex lifecycle
#[test]
fn test_default_trait_lifecycle() {
    let mut cell: SwmrCell<Vec<i32>> = SwmrCell::default();
    let local = cell.local_reader();

    assert!(cell.get().is_empty());
    assert_eq!(cell.version(), 0);

    cell.update(|v: &Vec<i32>| {
        let mut new_v = v.clone();
        new_v.push(1);
        new_v
    });

    assert_eq!(*cell.get(), vec![1]);
    assert_eq!(cell.version(), 1);

    let guard = local.pin();
    assert_eq!(*guard, vec![1]);
}