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
use parking_lot::{Condvar, Mutex, MutexGuard, RawMutex, WaitTimeoutResult};
use std::{
    ops::{Deref, DerefMut},
    time::{Duration, Instant},
};

#[derive(Debug, Default)]
pub struct Monitor<T> {
    mutex: Mutex<T>,
    cv: Condvar,
}

impl<T> Monitor<T> {
    pub fn new(t: T) -> Self {
        Monitor {
            mutex: Mutex::new(t),
            cv: Condvar::new(),
        }
    }

    pub fn lock(&self) -> MonitorGuard<T> {
        MonitorGuard::new(&self.cv, self.mutex.lock())
    }

    pub fn try_lock(&self) -> Option<MonitorGuard<T>> {
        self.mutex
            .try_lock()
            .map(|g| MonitorGuard::new(&self.cv, g))
    }

    pub fn try_lock_for(&self, timeout: Duration) -> Option<MonitorGuard<T>> {
        self.mutex
            .try_lock_for(timeout)
            .map(|g| MonitorGuard::new(&self.cv, g))
    }

    pub fn try_lock_until(&self, timeout: Instant) -> Option<MonitorGuard<T>> {
        self.mutex
            .try_lock_until(timeout)
            .map(|g| MonitorGuard::new(&self.cv, g))
    }

    pub fn with_lock<U, F>(&self, f: F) -> U
    where
        F: FnOnce(MonitorGuard<T>) -> U,
    {
        f(self.lock())
    }

    pub fn try_with_lock<U, F>(&self, f: F) -> Option<U>
    where
        F: FnOnce(MonitorGuard<T>) -> U,
    {
        self.try_lock().map(f)
    }

    pub fn try_with_lock_for<U, F>(&self, timeout: Duration, f: F) -> Option<U>
    where
        F: FnOnce(MonitorGuard<T>) -> U,
    {
        self.try_lock_for(timeout).map(f)
    }

    pub fn try_with_lock_until<U, F>(&self, timeout: Instant, f: F) -> Option<U>
    where
        F: FnOnce(MonitorGuard<T>) -> U,
    {
        self.try_lock_until(timeout).map(f)
    }

    pub fn into_inner(self) -> T {
        self.mutex.into_inner()
    }

    pub fn get_mut(&mut self) -> &mut T {
        self.mutex.get_mut()
    }

    pub unsafe fn raw(&self) -> &RawMutex {
        self.mutex.raw()
    }

    pub unsafe fn force_unlock(&self) {
        self.mutex.force_unlock()
    }

    pub unsafe fn force_unlock_fair(&self) {
        self.mutex.force_unlock_fair()
    }
}

impl<T> From<T> for Monitor<T> {
    fn from(t: T) -> Self {
        Monitor::new(t)
    }
}

pub struct MonitorGuard<'a, T> {
    cv: &'a Condvar,
    guard: MutexGuard<'a, T>,
}

impl<'a, T> MonitorGuard<'a, T> {
    pub fn new(cv: &'a Condvar, guard: MutexGuard<'a, T>) -> Self {
        MonitorGuard { cv, guard }
    }

    pub fn notify_one(&self) {
        self.cv.notify_one();
    }

    pub fn notify_all(&self) {
        self.cv.notify_all();
    }

    pub fn wait(&mut self) {
        self.cv.wait(&mut self.guard);
    }

    pub fn wait_for(&mut self, timeout: Duration) -> WaitTimeoutResult {
        self.cv.wait_for(&mut self.guard, timeout)
    }

    pub fn wait_until(&mut self, timeout: Instant) -> WaitTimeoutResult {
        self.cv.wait_until(&mut self.guard, timeout)
    }
}

impl<T> Deref for MonitorGuard<'_, T> {
    type Target = T;

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

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