range-mutex 0.1.7

A `Mutex<[T]>`-like type, that allows locking different ranges separately.
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
use std::{ops::Bound, sync::atomic::AtomicBool, time::Duration};

use itertools::Itertools;

use crate::{RangeMutex, RangeMutexGuard};

#[test]
fn basic() {
    let mut data = vec![0u8; 300];
    let mut mutex = RangeMutex::new(&mut data[..]);
    {
        let mut g1 = mutex.try_lock(..100).unwrap();
        let mut g2 = mutex.try_lock(100..200).unwrap();
        let mut g3 = mutex
            .try_lock(40..40)
            .expect("empty range does not overlap with any other range");
        let mut g4 = mutex
            .try_lock(40..40)
            .expect("empty range does not overlap with any other range");
        assert!(mutex.try_lock(99..100).is_none(), "overlaps with g1");
        assert!(mutex.try_lock(100..101).is_none(), "overlaps with g2");
        assert!(mutex.try_lock(99..101).is_none(), "overlaps with g1 and g2");
        g1.fill(1);
        g2.fill(2);
        g3.fill(3);
        g4.fill(4);
        drop(g2);
        assert!(mutex.try_lock(99..100).is_none(), "overlaps with g1");
        assert!(mutex.try_lock(99..101).is_none(), "overlaps with g1");
        assert!(mutex.try_lock(100..101).is_some());
    }
    assert!(mutex.get_mut()[..100].iter().all(|&x| x == 1));
    assert!(mutex.get_mut()[100..200].iter().all(|&x| x == 2));
    assert!(mutex.get_mut()[200..].iter().all(|&x| x == 0));
    assert!(data[..100].iter().all(|&x| x == 1));
    assert!(data[100..200].iter().all(|&x| x == 2));
    assert!(data[200..].iter().all(|&x| x == 0));
}

#[test]
fn basic_park() {
    let mut data = vec![0u8; 300];
    let mut mutex = RangeMutex::new(&mut data[..]);
    // not perfect but meh
    let has_main_dropped_g1: AtomicBool = AtomicBool::new(false);
    let has_main_dropped_g2: AtomicBool = AtomicBool::new(false);
    std::thread::scope(|scope| {
        let mut g1 = mutex.try_lock(..100).unwrap();
        let mut g2 = mutex.try_lock(100..200).unwrap();
        let mut g3 = mutex
            .try_lock(40..40)
            .expect("empty range does not overlap with any other range");
        let mut g4 = mutex
            .try_lock(40..40)
            .expect("empty range does not overlap with any other range");
        scope.spawn(|| {
            let g5 = mutex.lock(99..100);
            assert!(
                has_main_dropped_g1.load(std::sync::atomic::Ordering::Acquire),
                "overlaps with g1"
            );
            assert_eq!(&*g5, [1]);
        });
        scope.spawn(|| {
            let g6 = mutex.lock(100..101);
            assert!(
                has_main_dropped_g2.load(std::sync::atomic::Ordering::Acquire),
                "overlaps with g2"
            );
            assert_eq!(&*g6, [2]);
        });
        scope.spawn(|| {
            let g7 = mutex.lock(99..101);
            assert!(
                has_main_dropped_g1.load(std::sync::atomic::Ordering::Acquire)
                    && has_main_dropped_g2
                        .load(std::sync::atomic::Ordering::Acquire),
                "overlaps with g1 and g2"
            );
            assert_eq!(&*g7, [1, 2]);
        });
        g1.fill(1);
        g2.fill(2);
        g3.fill(3);
        g4.fill(4);
        has_main_dropped_g2.store(true, std::sync::atomic::Ordering::Release);
        drop(g2);
        std::thread::sleep(Duration::from_millis(10));
        has_main_dropped_g1.store(true, std::sync::atomic::Ordering::Release);
        drop(g1);
        std::thread::sleep(Duration::from_millis(10));
    });
    assert!(mutex.get_mut()[..100].iter().all(|&x| x == 1));
    assert!(mutex.get_mut()[100..200].iter().all(|&x| x == 2));
    assert!(mutex.get_mut()[200..].iter().all(|&x| x == 0));
    assert!(data[..100].iter().all(|&x| x == 1));
    assert!(data[100..200].iter().all(|&x| x == 2));
    assert!(data[200..].iter().all(|&x| x == 0));
}

#[test]
fn empty() {
    let data = RangeMutex::new(vec![0_i32; 4]);
    let g1 = data.try_lock(0..0).unwrap();
    let g2 = data.try_lock(0..0).unwrap();
    assert_eq!(g1.len(), 0);
    assert_eq!(g2.len(), 0);
    let (g3, g4) = RangeMutexGuard::split_at(g1, 0);
    assert_eq!(g3.len(), 0);
    assert_eq!(g4.len(), 0);
    let g5 = data.try_lock(4..4).unwrap();
    assert_eq!(g5.len(), 0);
    let g6 = RangeMutexGuard::<i32>::default();
    let g7 = RangeMutexGuard::<i32>::empty();
    assert_eq!(g6.len(), 0);
    assert_eq!(g7.len(), 0);
}

#[test]
fn disjoint_time_overlapping_ranges() {
    let data = RangeMutex::new(vec![0; 128]);
    for i in 0..data.len() {
        data.lock(i..).fill(i);
    }
    itertools::assert_equal(data.into_inner(), 0..128);
}

#[test]
fn overlapping() {
    // Run multiple times for different index orders.
    for indices in [0, 1, 2, 3].into_iter().permutations(4) {
        let mut data = RangeMutex::new(vec![usize::MAX; 5]);
        std::thread::scope(|scope| {
            let data = &data;
            for i in indices {
                scope.spawn(move || {
                    let mut g = data.lock(i..=i + 1);
                    std::thread::sleep(Duration::from_millis(50));
                    g.fill(i);
                });
            }
        });

        // Possible orders:
        // * 0/2, 1/3: [0, 1, 1, 3, 3] (2 seconds)
        // * 1/3, 0/2: [0, 0, 2, 2, 3] (2 seconds)
        // * 0/3, 1, 2: [0, 1, 2, 2, 3] (3 seconds)
        // * 0/3, 2, 1: [0, 1, 1, 2, 3] (3 seconds)

        let data = data.get_mut();
        // Possible orders:
        // * 0/2, 1/3: [0, 1, 1, 3, 3]
        // * 1/3, 0/2: [0, 0, 2, 2, 3]
        // * 0/3, 1, 2: [0, 1, 2, 2, 3]
        // * 0/3, 2, 1: [0, 1, 1, 2, 3]
        assert!(
            data == [0, 1, 1, 3, 3]
                || data == [0, 0, 2, 2, 3]
                || data == [0, 1, 2, 2, 3]
                || data == [0, 1, 1, 2, 3]
        );
    }
}

#[test]
#[ignore = "takes up to 40 seconds"]
fn overlapping_with_sleep() {
    // Run multiple times for different index orders.
    for indices in [0, 1, 2, 3].into_iter().permutations(4) {
        let start = std::time::Instant::now();
        let mut data = RangeMutex::new(vec![usize::MAX; 5]);
        std::thread::scope(|scope| {
            let data = &data;
            for i in indices {
                scope.spawn(move || {
                    let mut g = data.lock(i..=i + 1);
                    std::thread::sleep(Duration::from_secs(1));
                    g.fill(i);
                });
            }
        });

        // Possible orders:
        // * 0/2, 1/3: [0, 1, 1, 3, 3] (2 seconds)
        // * 1/3, 0/2: [0, 0, 2, 2, 3] (2 seconds)
        // * 0/3, 1, 2: [0, 1, 2, 2, 3] (3 seconds)
        // * 0/3, 2, 1: [0, 1, 1, 2, 3] (3 seconds)

        let took = start.elapsed();
        let data = data.get_mut();
        // Possible orders:
        // * 0/2, 1/3: [0, 1, 1, 3, 3]
        // * 1/3, 0/2: [0, 0, 2, 2, 3]
        // * 0/3, 1, 2: [0, 1, 2, 2, 3]
        // * 0/3, 2, 1: [0, 1, 1, 2, 3]
        let seconds = Duration::from_secs;
        assert!(
            data == [0, 1, 1, 3, 3] && (seconds(2)..seconds(3)).contains(&took)
                || data == [0, 0, 2, 2, 3]
                    && (seconds(2)..seconds(3)).contains(&took)
                || data == [0, 1, 2, 2, 3]
                    && (seconds(3)..seconds(4)).contains(&took)
                || data == [0, 1, 1, 2, 3]
                    && (seconds(3)..seconds(4)).contains(&took)
        );
    }
}

#[test]
fn split() {
    let mut values = [0u8; 64];
    let mutex = RangeMutex::new(&mut values);

    let guard = mutex.try_lock(..).unwrap();
    assert_eq!(guard.len(), 64);
    let (g1, g2) = RangeMutexGuard::split_at(guard, 42);
    assert_eq!(g1.len(), 42);
    assert_eq!(g2.len(), 64 - 42);
    drop((g1, g2));

    let guard = mutex.try_lock(..).unwrap();
    assert_eq!(guard.len(), 64);
    let (g1, g2) = RangeMutexGuard::split_at(guard, 64);
    assert_eq!(g1.len(), 64);
    assert_eq!(g2.len(), 0);
    drop((g1, g2));

    let guard = mutex.try_lock(..).unwrap();
    assert_eq!(guard.len(), 64);
    let (g1, g2) = RangeMutexGuard::split_at(guard, 0);
    assert_eq!(g1.len(), 0);
    assert_eq!(g2.len(), 64);
    drop((g1, g2));
}

#[test]
fn slice() {
    let mut values = [0u8; 64];
    let mutex = RangeMutex::new(&mut values);

    let guard = mutex.try_lock(..).unwrap();
    assert_eq!(guard.len(), 64);
    let guard = RangeMutexGuard::slice(guard, 12..42);
    assert_eq!(guard.len(), 42 - 12);
    assert!(mutex.try_lock(..12).is_some());
    assert!(mutex.try_lock(42..).is_some());
}

#[cfg(feature = "async")]
#[tokio::test(start_paused = true)]
async fn async_lock() {
    use rand::seq::SliceRandom;
    use std::sync::Arc;
    // Run multiple times for different index orders.
    for indices in [0, 1, 2, 3].into_iter().permutations(4) {
        let start = tokio::time::Instant::now();
        let mut data = Arc::new(RangeMutex::new(vec![usize::MAX; 5]));
        let mut handles = vec![];
        for i in indices {
            let handle = tokio::spawn({
                let data = data.clone();
                async move {
                    let mut g = data.lock_async(i..=i + 1).await;
                    tokio::time::sleep(Duration::from_secs(1)).await;
                    g.fill(i);
                }
            });
            handles.push(handle);
        }
        handles.shuffle(&mut rand::thread_rng());
        for handle in handles {
            handle.await.unwrap();
        }

        // Possible orders:
        // * 0/2, 1/3: [0, 1, 1, 3, 3] (2 seconds)
        // * 1/3, 0/2: [0, 0, 2, 2, 3] (2 seconds)
        // * 0/3, 1, 2: [0, 1, 2, 2, 3] (3 seconds)
        // * 0/3, 2, 1: [0, 1, 1, 2, 3] (3 seconds)

        let took = start.elapsed();
        let data =
            Arc::get_mut(&mut data).expect("all tasks completed").get_mut();
        // Possible orders:
        // * 0/2, 1/3: [0, 1, 1, 3, 3]
        // * 1/3, 0/2: [0, 0, 2, 2, 3]
        // * 0/3, 1, 2: [0, 1, 2, 2, 3]
        // * 0/3, 2, 1: [0, 1, 1, 2, 3]
        assert!(
            data == [0, 1, 1, 3, 3] && took == Duration::from_secs(2)
                || data == [0, 0, 2, 2, 3] && took == Duration::from_secs(2)
                || data == [0, 1, 2, 2, 3] && took == Duration::from_secs(3)
                || data == [0, 1, 1, 2, 3] && took == Duration::from_secs(3)
        );
    }
}

/// ```rust,compile_fail,E0597
/// use range_mutex::RangeMutex;
/// let data: RangeMutex<&'static str, Vec<&'static str>> =
///     RangeMutex::new(vec!["Hello, world!"]);
/// {
///     let s = String::from("Oh, no!");
///     let mut g = data.lock(..);
///     // s does not live long enough
///     g[0] = &s;
/// }
/// dbg!(data.into_inner());
/// ```
#[allow(dead_code)]
pub struct AssertVarianceIsCorrect;

#[test]
#[should_panic]
fn ridiculous_range_1() {
    let mut values = [(); usize::MAX];
    let range_mutex = RangeMutex::new(&mut values);

    let _ = range_mutex.lock(..=usize::MAX);
}

#[test]
#[should_panic]
fn ridiculous_range_2() {
    let mut values = [(); usize::MAX];
    let range_mutex = RangeMutex::new(&mut values);

    let _ = range_mutex.lock((Bound::Excluded(usize::MAX), Bound::Unbounded));
}

#[test]
#[should_panic]
fn out_of_range_1() {
    let range_mutex = RangeMutex::new(&mut [(); 0]);

    let _ = range_mutex.lock(..1);
}

#[test]
#[should_panic]
fn out_of_range_2() {
    let mut values = [0u8; 4];
    let range_mutex = RangeMutex::new(&mut values);

    let _ = range_mutex.lock(2..1);
}

#[test]
fn into_inner_coverage() {
    // Array
    let range_mutex = RangeMutex::new([0u8; 42]);
    range_mutex.lock(..).fill(42);
    assert_eq!(range_mutex.into_inner(), [42; 42]);

    // &mut Array
    let mut values = [0u8; 42];
    let range_mutex = RangeMutex::new(&mut values);
    range_mutex.lock(..).fill(42);
    assert_eq!(range_mutex.into_inner(), &mut [42; 42]);

    // &mut Slice
    let mut values = [0u8; 42];
    let range_mutex = RangeMutex::new(&mut values[..]);
    range_mutex.lock(..).fill(42);
    assert_eq!(range_mutex.into_inner(), &mut [42; 42]);

    // Vec
    let range_mutex = RangeMutex::new(vec![0u8; 42]);
    range_mutex.lock(..).fill(42);
    assert_eq!(range_mutex.into_inner(), [42; 42]);

    // Boxed slice
    let range_mutex = RangeMutex::new(vec![0u8; 42].into_boxed_slice());
    range_mutex.lock(..).fill(42);
    assert_eq!(*range_mutex.into_inner(), [42; 42]);

    // RangeMutexGuard
    let range_mutex = RangeMutex::new(vec![0u8; 42].into_boxed_slice());
    {
        let range_mutex = RangeMutex::new(range_mutex.lock(..));
        range_mutex.lock(..).fill(42);
        assert_eq!(*range_mutex.into_inner(), [42; 42]);
    }
    assert_eq!(*range_mutex.into_inner(), [42; 42]);
}

#[test]
fn leak() {
    let mut values = [0; 64];
    let mut range_mutex = RangeMutex::new(&mut values);
    let mut g1 = range_mutex.lock(..32);
    g1.fill(42);
    std::mem::forget(g1);
    assert!(range_mutex.try_lock(..).is_none());
    assert!(range_mutex.try_lock(32..).is_some());
    assert_eq!(range_mutex.get_mut()[..32], [42; 32]);
    assert_eq!(range_mutex.get_mut()[32..], [0; 32]);
    assert!(range_mutex.try_lock(..).is_none());
    assert!(range_mutex.try_lock(32..).is_some());
    range_mutex.undo_leak();
    assert!(range_mutex.try_lock(..).is_some());
    assert!(range_mutex.try_lock(32..).is_some());
}

#[test]
fn zero_length() {
    let mut values = [0; 64];
    let range_mutex = RangeMutex::new(&mut values);
    let _g1 = range_mutex.lock(..32);
    assert_eq!(range_mutex.try_lock(..0).unwrap().len(), 0);
    assert_eq!(range_mutex.lock(..0).len(), 0);
    assert_eq!(range_mutex.try_lock(10..10).unwrap().len(), 0);
    assert_eq!(range_mutex.lock(10..10).len(), 0);
}

#[cfg(feature = "async")]
#[tokio::test(start_paused = true)]
async fn async_zero_length() {
    let mut values = [0; 64];
    let range_mutex = RangeMutex::new(&mut values);
    let _g1 = range_mutex.lock_async(..32).await;
    assert_eq!(range_mutex.try_lock(..0).unwrap().len(), 0);
    assert_eq!(range_mutex.lock_async(..0).await.len(), 0);
    assert_eq!(range_mutex.try_lock(10..10).unwrap().len(), 0);
    assert_eq!(range_mutex.lock_async(10..10).await.len(), 0);
}

#[test]
fn as_ref() {
    let mut values = [0; 64];
    let range_mutex = RangeMutex::new(&mut values);
    let mut guard = range_mutex.lock(..);
    guard.fill(42);
    assert_eq!(guard.as_ref(), &[42; 64]);
    assert_eq!(guard.as_mut(), &mut [42; 64]);
}