File size: 1,966 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{
    ops::{Deref, DerefMut},
    sync::{
        RwLock, RwLockReadGuard, RwLockWriteGuard,
        atomic::{AtomicBool, Ordering},
    },
};

use crate::store::Store;

pub struct StoreContainer {
    store: RwLock<StoreWithGeneration>,
    want_to_read: AtomicBool,
}

struct StoreWithGeneration {
    store: Store,
    generation: usize,
}

impl StoreContainer {
    pub fn new() -> Self {
        Self {
            store: RwLock::new(StoreWithGeneration {
                store: Store::new(),
                generation: 0,
            }),
            want_to_read: AtomicBool::new(false),
        }
    }

    pub fn read(&self) -> StoreReadGuard<'_> {
        if let Ok(guard) = self.store.try_read() {
            return StoreReadGuard { guard };
        }
        self.want_to_read.store(true, Ordering::Relaxed);
        let guard = StoreReadGuard {
            guard: self.store.read().unwrap(),
        };
        self.want_to_read.store(false, Ordering::Relaxed);
        guard
    }

    pub fn write(&self) -> StoreWriteGuard<'_> {
        let mut guard = self.store.write().unwrap();
        guard.generation += 1;
        StoreWriteGuard { guard }
    }

    pub fn want_to_read(&self) -> bool {
        self.want_to_read.load(Ordering::Relaxed)
    }
}

pub struct StoreReadGuard<'a> {
    guard: RwLockReadGuard<'a, StoreWithGeneration>,
}

impl StoreReadGuard<'_> {
    pub fn generation(&self) -> usize {
        self.guard.generation
    }
}

impl Deref for StoreReadGuard<'_> {
    type Target = Store;

    fn deref(&self) -> &Self::Target {
        &self.guard.store
    }
}

pub struct StoreWriteGuard<'a> {
    guard: RwLockWriteGuard<'a, StoreWithGeneration>,
}

impl Deref for StoreWriteGuard<'_> {
    type Target = Store;

    fn deref(&self) -> &Self::Target {
        &self.guard.store
    }
}

impl DerefMut for StoreWriteGuard<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.guard.store
    }
}