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
/// Basic tests module
/// Tests core functionality correctness
use crate::SwmrCell;
use std::prelude::v1::*;
use std::thread;
use std::thread::JoinHandle;

use std::format;
use std::string::ToString;
use std::vec;

/// Test 1: Create SwmrCell and basic usage
#[test]
fn test_create_swmr_cell_and_basic_usage() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

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

/// Test 2: local pin/drop cycle
#[test]
fn test_reader_pin_drop_cycle() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    // First pin
    {
        let _guard = local.pin();
        // guard is active here
    }
    // guard dropped, unpinned

    // Second pin
    {
        let _guard = local.pin();
        // guard active again
    }
}

/// Test 3: Writer store new value
#[test]
fn test_writer_store() {
    let mut cell = SwmrCell::new(10i32);
    let local = cell.local_reader();

    // Initial value
    {
        let guard = local.pin();
        assert_eq!(*guard, 10);
    }

    // Writer stores new value
    cell.store(20);

    // Read new value
    {
        let guard = local.pin();
        assert_eq!(*guard, 20);
    }
}

/// Test 4: Writer manual collect
#[test]
fn test_writer_collect() {
    // Use builder to set a high threshold to avoid auto-collect
    let mut cell = SwmrCell::builder()
        .auto_reclaim_threshold(Some(1000))
        .build(0i32);

    // Retire some data
    cell.store(100);
    cell.store(200);

    // We can't check garbage count directly as it's private.
    // But we can call collect.
    cell.collect();

    // If it doesn't panic, it's good.
}

/// Test 5: Nested pins (Reentrancy)
#[test]
fn test_nested_pins() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    // Verify we can pin multiple times (reentrant pinning)
    let guard1 = local.pin();
    let guard2 = local.pin();
    let guard3 = local.pin(); // Reentrant

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

    // All guards should work
    drop(guard3);
    drop(guard2);
    drop(guard1);
}

/// Test 6: Multiple Locals
#[test]
fn test_multiple_locals() {
    let cell = SwmrCell::new(42i32);

    let reader1 = cell.local_reader();
    let reader2 = cell.local_reader();

    // Both readers should work
    let guard1 = reader1.pin();
    let guard2 = reader2.pin();

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

/// Test 7: String type
#[test]
fn test_swmr_with_string() {
    let cell = SwmrCell::new(String::from("hello"));
    let local = cell.local_reader();

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

/// Test 8: Struct type
#[test]
fn test_swmr_with_struct() {
    #[derive(Debug, PartialEq)]
    struct Point {
        x: i32,
        y: i32,
    }

    let cell = SwmrCell::new(Point { x: 10, y: 20 });
    let local = cell.local_reader();

    {
        let guard = local.pin();
        assert_eq!(guard.x, 10);
        assert_eq!(guard.y, 20);
    }
}

/// Test 9: SwmrCell Drop
#[test]
fn test_swmr_drop() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();
    drop(local);
    drop(cell);
    // Memory should be freed. We rely on Miri or ASAN to catch leaks.
}

/// Test 10: Multiple SwmrCell instances
#[test]
fn test_multiple_swmr_instances() {
    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 11: Thread safety
#[test]
fn test_thread_safety() {
    let cell = SwmrCell::new(0i32);

    let mut handles = vec![];

    // Start 5 local threads
    for _ in 0..5 {
        let local = cell.local_reader();

        handles.push(thread::spawn(move || {
            let guard = local.pin();
            *guard // Return value
        }));
    }

    let results: Vec<i32> = handles
        .into_iter()
        .map(|h: JoinHandle<i32>| h.join().unwrap())
        .collect();

    assert_eq!(results.len(), 5);
    for &result in &results {
        assert_eq!(result, 0);
    }
}

/// Test 12: previous() returns None initially
#[test]
fn test_previous_none_initially() {
    let cell = SwmrCell::new(42i32);
    assert!(cell.previous().is_none());
}

/// Test 13: previous() returns the old value after store
#[test]
fn test_previous_after_store() {
    let mut cell = SwmrCell::new(1i32);
    assert!(cell.previous().is_none());

    cell.store(2);
    assert_eq!(cell.previous(), Some(&1));

    cell.store(3);
    assert_eq!(cell.previous(), Some(&2));

    cell.store(4);
    assert_eq!(cell.previous(), Some(&3));
}

/// Test 14: previous() survives garbage collection
#[test]
fn test_previous_survives_gc() {
    let mut cell = SwmrCell::builder()
        .auto_reclaim_threshold(None) // Disable auto-reclaim
        .build(0i32);

    // Store multiple values to create garbage
    for i in 1..=10 {
        cell.store(i);
    }

    // Manual collect
    cell.collect();

    // previous() should still return the last retired value (9)
    // because safety_limit = current_version - 2 preserves it
    assert_eq!(cell.previous(), Some(&9));
}

/// Test 15: previous() with complex type
#[test]
fn test_previous_with_struct() {
    #[derive(Debug, PartialEq)]
    struct Data {
        value: i32,
        name: String,
    }

    let mut cell = SwmrCell::new(Data {
        value: 1,
        name: "first".to_string(),
    });
    assert!(cell.previous().is_none());

    cell.store(Data {
        value: 2,
        name: "second".to_string(),
    });

    let prev = cell.previous().unwrap();
    assert_eq!(prev.value, 1);
    assert_eq!(prev.name, "first");
}

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

/// Test 16: get() returns current value
#[test]
fn test_get_returns_current_value() {
    let mut cell = SwmrCell::new(42i32);
    assert_eq!(*cell.get(), 42);

    cell.store(100);
    assert_eq!(*cell.get(), 100);
}

/// Test 17: update() with closure
#[test]
fn test_update_with_closure() {
    let mut cell = SwmrCell::new(10i32);

    cell.update(|v| v + 5);
    assert_eq!(*cell.get(), 15);

    cell.update(|v| v * 2);
    assert_eq!(*cell.get(), 30);
}

/// Test 22: garbage_count() tracks retired objects
#[test]
fn test_garbage_count_tracks_retired_objects() {
    let mut cell = SwmrCell::builder()
        .auto_reclaim_threshold(None) // Disable auto-reclaim
        .build(0i32);

    assert_eq!(cell.garbage_count(), 0);

    cell.store(1);
    assert_eq!(cell.garbage_count(), 1);

    cell.store(2);
    assert_eq!(cell.garbage_count(), 2);

    cell.store(3);
    assert_eq!(cell.garbage_count(), 3);
}

/// Test 23: LocalReader::is_pinned()
#[test]
fn test_local_reader_is_pinned() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();

    assert!(!local.is_pinned());

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

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

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

    assert_eq!(local.version(), 0);

    cell.store(1);
    assert_eq!(local.version(), 1);

    cell.store(2);
    assert_eq!(local.version(), 2);
}

/// Test 25: PinGuard::version()
#[test]
fn test_pin_guard_version() {
    let mut cell = SwmrCell::new(0i32);
    let local = cell.local_reader();

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

    cell.store(1);
    let guard = local.pin();
    assert_eq!(guard.version(), 1);
}

/// Test 26: PinGuard::as_ref()
#[test]
fn test_pin_guard_as_ref() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();
    let guard = local.pin();

    let value: &i32 = guard.as_ref();
    assert_eq!(*value, 42);
}

/// Test 27: Default trait
#[test]
fn test_default_trait() {
    let cell: SwmrCell<i32> = SwmrCell::default();
    assert_eq!(*cell.get(), 0);

    let cell: SwmrCell<String> = SwmrCell::default();
    assert_eq!(*cell.get(), "");

    let cell: SwmrCell<Vec<i32>> = SwmrCell::default();
    assert!(cell.get().is_empty());
}

/// Test 28: From<T> trait
#[test]
fn test_from_trait() {
    let cell: SwmrCell<i32> = SwmrCell::from(42);
    assert_eq!(*cell.get(), 42);

    let cell: SwmrCell<String> = SwmrCell::from(String::from("hello"));
    assert_eq!(*cell.get(), "hello");
}

/// Test 29: Debug trait for SwmrCell
#[test]
fn test_debug_trait_swmr_cell() {
    let cell = SwmrCell::new(42i32);
    let debug_str = format!("{:?}", cell);
    assert!(debug_str.contains("SwmrCell"));
    assert!(debug_str.contains("42"));
}

/// Test 30: Debug trait for LocalReader
#[test]
fn test_debug_trait_local_reader() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();
    let debug_str = format!("{:?}", local);
    assert!(debug_str.contains("LocalReader"));
}

/// Test 31: Debug trait for PinGuard
#[test]
fn test_debug_trait_pin_guard() {
    let cell = SwmrCell::new(42i32);
    let local = cell.local_reader();
    let guard = local.pin();
    let debug_str = format!("{:?}", guard);
    assert!(debug_str.contains("PinGuard"));
    assert!(debug_str.contains("42"));
}