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
use std::{
    fmt::Display,
    ops::Deref,
    sync::{atomic::Ordering, Arc},
};

use crate::{ref_inner::RefInner, ref_mut::RefMut};

pub struct Ref<T> {
    pub(crate) inner: Arc<RefInner<T>>,
}

unsafe impl<T> Sync for Ref<T> {}
unsafe impl<T> Send for Ref<T> {}

impl<T> Ref<T> {
    pub fn new(data: T) -> Self {
        Self {
            inner: Arc::new(RefInner::new(data)),
        }
    }

    pub fn locked(&self) -> bool {
        self.inner.lock.load(Ordering::Relaxed) > 0
    }

    /// # Dont use!!!
    pub fn lock(&self) {
        while self
            .inner
            .lock
            .compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
            .is_err()
        {
            atomic_wait::wait(&self.inner.lock, 1);
        }
    }

    /// # Dont use!!!
    pub fn unlock(&self) {
        self.inner.lock.store(0, Ordering::Release);
        atomic_wait::wake_one(&self.inner.lock)
    }

    /// Recomand to use `get_mut`
    /// this is not really recomanded
    /// but if you do things in a single thread is more recomanded
    pub fn mut_scope(&self, clasure: impl Fn(&mut T)) {
        self.lock();
        clasure(unsafe { &mut *self.inner.cell.get() });
        self.unlock()
    }

    /// if you doing things in as single thread if you call get_mut 2 times or more the application will be in a loop
    /// Dont `get_mut` more then one time!
    pub fn get_mut(&self) -> RefMut<T> {
        self.lock();

        RefMut {
            inner: self.inner.clone(),
        }
    }
}

impl<T> Clone for Ref<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<T> Deref for Ref<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.inner.cell.get() }
    }
}

impl<T> Display for Ref<T>
where
    T: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.deref().fmt(f)
    }
}

#[cfg(test)]
mod test {
    use super::Ref;
    use std::thread::spawn;

    #[test]
    fn threading_1() {
        let data = Ref::new(0);

        let tmp_data = data.clone();
        let thread1 = spawn(move || {
            let data = tmp_data;
            for _ in 0..5000000 {
                let mut data = data.get_mut();
                *data += 1;
            }
        });

        let tmp_data = data.clone();
        let thread2 = spawn(move || {
            let data = tmp_data;
            for _ in 0..5000000 {
                let mut data = data.get_mut();
                *data += 1;
            }
        });

        let tmp_data = data.clone();
        let thread3 = spawn(move || {
            let data = tmp_data;
            for _ in 0..5000000 {
                let mut data = data.get_mut();
                *data += 1;
            }
        });

        thread1.join().unwrap();
        thread2.join().unwrap();
        thread3.join().unwrap();

        assert_eq!(*data, 15000000)
    }
}