genref 0.8.0

Vale's generational references in 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
//! # Sharable generation counting
//!
//! This module implements the same functionality as the base module, but
//! implemented with locks and atomics. It is to `Arc` what base genrefs are to
//! `Rc`.

use std::{
    mem, ptr,
    sync::atomic::{AtomicU32, Ordering},
};

use lock_api::{RawRwLock, RawRwLockRecursive, RawRwLockUpgrade};
use parking_lot::Mutex;

use lazy_static::lazy_static;

#[repr(transparent)]
#[derive(Clone, Copy)]
struct Generation(&'static AtomicU32);

impl Generation
{
    fn new() -> Self { FreeList::unfree().unwrap_or_else(FreshList::fresh) }
    fn get(&self) -> u32 { self.0.load(Ordering::Relaxed) }

    fn free(this: Self)
    {
        let c = this.0.fetch_add(1, Ordering::Relaxed);

        if c != u32::MAX {
            FreeList::free(this);
        }
    }
}

lazy_static! {
    static ref FREELIST: Mutex<FreeList> = Mutex::new(FreeList::new());
    static ref FRESHLIST: Mutex<FreshList> = Mutex::new(FreshList::new());
    static ref LOCK: Lock = Lock::new();
    static ref DROPQUEUE: Mutex<DropQueue> = Mutex::new(DropQueue::new());
}

struct Lock(parking_lot::RawRwLock);

/// Non-exclusive lock (ZST)
///
/// Used to create shared references to underlying objects,
/// its existence defers dropping of allocated objects.
#[derive(Debug)]
pub struct Reading(());
pub fn reading() -> Reading { Lock::reading() }
pub fn try_reading() -> Option<Reading> { Lock::try_reading() }

/// Exclusive lock (ZST)
///
/// Used to create mutable references to underlying objects,
/// its existence defers dropping of allocated objects.
///
/// USAGE IS HIGHLY ILL ADVISED
#[derive(Debug)]
pub struct Writing(());
pub fn writing() -> Writing { Lock::writing() }
pub fn try_writing() -> Option<Writing> { Lock::try_writing() }

impl Lock
{
    fn new() -> Self { Self(lock_api::RawRwLock::INIT) }

    fn reading() -> Reading
    {
        LOCK.0.lock_shared_recursive();
        Reading(())
    }

    fn writing() -> Writing
    {
        LOCK.0.lock_exclusive();
        Writing(())
    }

    fn try_reading() -> Option<Reading>
    {
        if LOCK.0.try_lock_shared_recursive() {
            Some(Reading(()))
        } else {
            None
        }
    }

    fn try_writing() -> Option<Writing>
    {
        if LOCK.0.try_lock_exclusive() {
            Some(Writing(()))
        } else {
            None
        }
    }
}

impl Reading
{
    fn try_upgrade(self) -> Result<Writing, Self>
    {
        LOCK.0.lock_upgradable();
        unsafe { LOCK.0.unlock_shared() }
        if unsafe { LOCK.0.try_upgrade() } {
            mem::forget(self);
            Ok(Writing(()))
        } else {
            LOCK.0.lock_shared_recursive();
            unsafe { LOCK.0.unlock_upgradable() }
            Err(self)
        }
    }
}

impl Drop for Reading
{
    fn drop(&mut self)
    {
        let this = unsafe { ptr::read(self as *const Reading) };

        let dq;
        match this.try_upgrade() {
            Ok(mut wl) => dq = DropQueue::clear(&mut wl),
            Err(rl) => mem::forget(rl),
        }

        unsafe { LOCK.0.unlock_shared() }
    }
}

impl Clone for Reading
{
    fn clone(&self) -> Self { reading() }
}

impl Drop for Writing
{
    fn drop(&mut self)
    {
        let q = DropQueue::clear(self);
        unsafe { LOCK.0.unlock_shared() }
        mem::drop(q);
    }
}

struct FreeList(Vec<Generation>);
struct FreshList(usize, &'static [AtomicU32]);

impl FreeList
{
    fn new() -> Self { Self(Vec::with_capacity(32)) }

    fn free_(&mut self, gen: Generation) { self.0.push(gen) }
    fn free(gen: Generation) { FREELIST.lock().free_(gen) }

    fn unfree_(&mut self) -> Option<Generation> { self.0.pop() }
    fn unfree() -> Option<Generation> { FREELIST.lock().unfree_() }
}

impl FreshList
{
    const INIT: u32 = 1;
    fn new() -> Self { Self(0, Self::more(32)) }

    fn fresh_(&mut self) -> Generation
    {
        if self.0 == self.1.len() {
            self.refresh()
        }
        self.0 += 1;
        Generation(&self.1[self.0 - 1])
    }

    fn fresh() -> Generation { FRESHLIST.lock().fresh_() }

    fn refresh(&mut self)
    {
        self.1 = Self::more(self.0 + self.0 / 2);
        self.0 = 0;
    }

    fn more(n: usize) -> &'static [AtomicU32]
    {
        let mut v = Vec::with_capacity(n);
        for _ in 0..n {
            v.push(AtomicU32::new(Self::INIT))
        }
        Vec::leak(v)
    }
}
trait DropLater: Send + Sync {}
impl<T: Send + Sync> DropLater for T {}
struct DropQueue(Vec<Box<dyn DropLater>>);

impl DropQueue
{
    fn new() -> Self { Self(Vec::with_capacity(32)) }

    fn clear_(&mut self, _wl: &mut Writing) -> impl Drop
    {
        let re = Vec::with_capacity(self.0.len());
        mem::replace(&mut self.0, re)
    }

    fn clear(wl: &mut Writing) -> impl Drop { DROPQUEUE.lock().clear_(wl) }

    fn defer_(&mut self, val: Box<dyn DropLater>) { self.0.push(val) }
    fn defer(val: Box<dyn DropLater>) { DROPQUEUE.lock().defer_(val) }
}

use std::{mem::ManuallyDrop, ptr::NonNull};

/// Strong reference
///
/// Owns its underlying allocation.
///
/// The generation counter is allocated separately, since it must persist for
/// the entire lifetime of all `Weak` references.
pub struct Strong<T: Sync + Send + 'static>
{
    gen: Generation,
    ptr: ManuallyDrop<Box<T>>,
}

/// Weak reference
///
/// Stores its reference generation locally and cross-checks it everytime an
/// access is made.
pub struct Weak<T: Sync + Send + 'static>
{
    genref: u32,
    gen: Generation,
    ptr: NonNull<T>,
}

impl<T: Sync + Send + 'static> Drop for Strong<T>
{
    fn drop(&mut self)
    {
        Generation::free(self.gen);
        if let Some(wl) = Lock::try_writing() {
            let d = unsafe { ManuallyDrop::take(&mut self.ptr) };
            mem::drop(wl);
            mem::drop(d);
        } else {
            DropQueue::defer(unsafe { ManuallyDrop::take(&mut self.ptr) } as Box<dyn DropLater>);
        }
    }
}

impl<T: Sync + Send + 'static> Strong<T>
{
    pub fn new(t: T) -> Self { Self::from(Box::new(t)) }

    pub fn alias(&self) -> Weak<T>
    {
        Weak {
            genref: self.gen.get(),
            gen: self.gen,
            ptr: NonNull::from((*self.ptr).as_ref()),
        }
    }

    pub fn take(mut self, _wl: &mut Writing) -> Box<T>
    {
        Generation::free(self.gen);
        let b = unsafe { ManuallyDrop::take(&mut self.ptr) };
        mem::forget(self);
        b
    }

    pub fn as_ref(&self, _rl: &Reading) -> &T { &self.ptr }
    pub fn as_mut(&mut self, _wl: &mut Writing) -> &mut T { &mut self.ptr }

    pub fn map<F, U>(&self, rl: &Reading, f: F) -> Weak<U>
    where
        for<'a> F: Fn(&'a T) -> &'a U,
        U: Sync + Send + 'static,
    {
        Weak {
            genref: self.gen.get(),
            gen: self.gen,
            ptr: NonNull::from(f(self.as_ref(rl))),
        }
    }
}

impl<T: Sync + Send + 'static> From<Box<T>> for Strong<T>
{
    fn from(b: Box<T>) -> Self
    {
        Self {
            gen: Generation::new(),
            ptr: ManuallyDrop::new(b),
        }
    }
}

impl<T: Sync + Send + 'static> Weak<T>
{
    pub fn dangling() -> Self
    {
        static ZERO: AtomicU32 = AtomicU32::new(0);
        Weak {
            genref: u32::MAX,
            gen: Generation(&ZERO),
            ptr: NonNull::dangling(),
        }
    }

    pub fn is_valid(&self) -> bool { self.genref == self.gen.get() }

    pub fn try_ref(&self, _rl: &Reading) -> Option<&T>
    {
        if self.is_valid() {
            Some(unsafe { self.ptr.as_ref() })
        } else {
            None
        }
    }

    pub fn try_mut(&mut self, _wl: &mut Writing) -> Option<&mut T>
    {
        if self.is_valid() {
            Some(unsafe { self.ptr.as_mut() })
        } else {
            None
        }
    }

    pub fn try_map<F, U>(&self, rl: &Reading, f: F) -> Option<Weak<U>>
    where
        for<'a> F: Fn(&'a T) -> &'a U,
        U: Sync + Send + 'static,
    {
        if let Some(a) = self.try_ref(rl) {
            Some(Weak {
                genref: self.genref,
                gen: self.gen,
                ptr: NonNull::from(f(a)),
            })
        } else {
            None
        }
    }
}

impl<T: Sync + Send + 'static> Clone for Weak<T>
{
    fn clone(&self) -> Self { *self }
}

impl<T: Sync + Send + 'static> Copy for Weak<T> {}

pub enum Ref<T: Sync + Send + 'static>
{
    Strong(Strong<T>),
    Weak(Weak<T>),
}

impl<T: Sync + Send + 'static> Ref<T>
{
    /// New strong reference
    pub fn new(t: T) -> Self { Self::Strong(Strong::new(t)) }

    pub fn try_as_ref(&self, rl: &Reading) -> Option<&T>
    {
        match self {
            Ref::Strong(s) => Some(s.as_ref(rl)),
            Ref::Weak(w) => w.try_ref(rl),
        }
    }

    pub fn try_mut(&mut self, wl: &mut Writing) -> Option<&mut T>
    {
        match self {
            Ref::Strong(s) => Some(s.as_mut(wl)),
            Ref::Weak(w) => w.try_mut(wl),
        }
    }

    pub fn try_map<F, U>(&self, rl: &Reading, f: F) -> Option<Ref<U>>
    where
        for<'a> F: Fn(&'a T) -> &'a U,
        U: Sync + Send + 'static,
    {
        match self {
            Ref::Strong(s) => Some(Ref::Weak(s.map(rl, f))),
            Ref::Weak(w) => w.try_map(rl, f).map(Ref::Weak),
        }
    }

    pub fn is_weak(&self) -> bool
    {
        match self {
            Ref::Strong(_) => false,
            Ref::Weak(_) => true,
        }
    }

    pub fn is_strong(&self) -> bool
    {
        match self {
            Ref::Strong(_) => false,
            Ref::Weak(_) => true,
        }
    }

    pub fn is_valid(&self) -> bool
    {
        match self {
            Ref::Strong(_) => true,
            Ref::Weak(w) => w.is_valid(),
        }
    }
}

impl<T: Sync + Send + 'static> Clone for Ref<T>
{
    fn clone(&self) -> Self
    {
        match self {
            Self::Strong(s) => Self::Weak(s.alias()),
            Self::Weak(w) => Self::Weak(*w),
        }
    }
}

impl<T: Sync + Send + 'static> From<Weak<T>> for Ref<T>
{
    fn from(w: Weak<T>) -> Self { Ref::Weak(w) }
}

impl<T: Sync + Send + 'static> From<Strong<T>> for Ref<T>
{
    fn from(s: Strong<T>) -> Self { Ref::Strong(s) }
}