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
use std::fmt::{ Display, Formatter, Error as FmtError };

/// The type of lock that can be acquired for a `GLock`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LockType {
    /// Before a `Shared` lock is acquired for a child `GLock`, `IntentionShared` locks must be
    /// acquired for each of its ancestors.
    ///
    /// # Compatibility
    ///
    /// `IntentionShared`: Yes
    ///
    /// `IntentionExclusive`: Yes
    ///
    /// `Shared`: Yes
    ///
    /// `SharedIntentionExclusive`: Yes
    ///
    /// `Exclusive`: No
    ///
    IntentionShared,

    /// Before an `Exclusive` or `SharedIntentionExclusive` lock is acquired for a child `GLock`,
    /// `IntentionExclusive` locks must be acquired for each of its ancestors.
    ///
    /// # Compatibility
    ///
    /// `IntentionShared`: Yes
    ///
    /// `IntentionExclusive`: Yes
    ///
    /// `Shared`: No
    ///
    /// `SharedIntentionExclusive`: No
    ///
    /// `Exclusive`: No
    ///
    IntentionExclusive,

    /// A `Shared` lock grants read access to its protected data. Before acquiring a `Shared` lock for a
    /// child `GLock`, `IntentionShared` (or more restrictive) locks must be acquired for all its ancestors.
    ///
    /// # Compatibility
    ///
    /// `IntentionShared`: Yes
    ///
    /// `IntentionExclusive`: No
    ///
    /// `Shared`: Yes
    ///
    /// `SharedIntentionExclusive`: No
    ///
    /// `Exclusive`: No
    ///
    Shared,

    /// A `SharedIntentionExclusive` lock - as the name implies - is similar to holding both a
    /// `Shared` lock and an `IntentionExclusive` lock at the same time. Before acquiring a
    /// `SharedIntentionExclusive` lock for a child `GLock`, `IntentionExclusive` (or more
    /// restrictive) locks must be acquired for all its ancestors.
    ///
    /// # Compatibility
    ///
    /// `IntentionShared`: Yes
    ///
    /// `IntentionExclusive`: No
    ///
    /// `Shared`: No
    ///
    /// `SharedIntentionExclusive`: No
    ///
    /// `Exclusive`: No
    ///
    SharedIntentionExclusive,

    /// An `Exclusive` lock grants write access to its protected data. Before acquiring an
    /// `Exclusive` lock for a child `GLock`, `IntentionExclusive` (or more restrictive) locks
    /// must be acquired for all its ancestors.
    ///
    /// # Compatibility
    ///
    /// `IntentionShared`: No
    ///
    /// `IntentionExclusive`: No
    ///
    /// `Shared`: No
    ///
    /// `SharedIntentionExclusive`: No
    ///
    /// `Exclusive`: No
    ///
    Exclusive,
}

pub const LOCK_TYPE_COUNT: usize = 5;

pub const LOCK_EMPTY_COUNTS: [usize; LOCK_TYPE_COUNT] = [0, 0, 0, 0, 0];

const LOCK_TYPES: [LockType; LOCK_TYPE_COUNT] = [
    LockType::IntentionShared,
    LockType::IntentionExclusive,
    LockType::Shared,
    LockType::SharedIntentionExclusive,
    LockType::Exclusive,
];

const LOCK_TYPE_IMPLICIT_PARENT_TYPE: [LockType; LOCK_TYPE_COUNT] = [
    LockType::IntentionShared,
    LockType::IntentionExclusive,
    LockType::IntentionShared,
    LockType::IntentionExclusive,
    LockType::IntentionExclusive
];

const LOCK_TYPE_COMPATIBLE_WITH: [[bool; LOCK_TYPE_COUNT]; LOCK_TYPE_COUNT] = [
    [true,  true,  true,  true,  false],
    [true,  true,  false, false, false],
    [true,  false, true,  false, false],
    [true,  false, false, false, false],
    [false, false, false, false, false],
];

const LOCK_TYPE_UPGRADABLE_TO: [[bool; LOCK_TYPE_COUNT]; LOCK_TYPE_COUNT] = [
    [true,  true,  true,  true,  true],
    [false, true,  false, true,  true],
    [false, false, true,  true,  true],
    [false, false, false, true,  true],
    [false, false, false, false, true],
];

const LOCK_TYPE_SUPPORTS_CHILDREN: [[bool; LOCK_TYPE_COUNT]; LOCK_TYPE_COUNT] = [
    [true,  false, true,  false, false],
    [true,  true,  true,  true,  true],
    [true,  false, true,  false, false],
    [true,  true,  true,  true,  true],
    [true,  true,  true,  true,  true],
];

impl LockType {

    pub fn lock_types() -> &'static [LockType] { &LOCK_TYPES }

    /// Returns the numeric index corresponding to this lock type.
    pub fn index(self) -> usize {
        match self {
            LockType::IntentionShared           => 0,
            LockType::IntentionExclusive        => 1,
            LockType::Shared                    => 2,
            LockType::SharedIntentionExclusive  => 3,
            LockType::Exclusive                 => 4,
        }
    }

    /// Returns the implicit parent lock type for this lock type. This means that, before acquiring
    /// this type of lock for a child `GLock`, locks of the implicit parent type must be acquired
    /// for all its ancestor `GLock`s.
    pub fn implicit_parent_type(self) -> LockType { LOCK_TYPE_IMPLICIT_PARENT_TYPE[self.index()] }

    /// Returns `true` if the lock type is compatible with the specified lock type, `false` otherwise.
    pub fn compatible_with(self, other_type: LockType) -> bool { LOCK_TYPE_COMPATIBLE_WITH[self.index()][other_type.index()] }

    /// Returns `true` if the lock type is upgradable to the specified lock type, `false` otherwise.
    pub fn upgradable_to(self, other_type: LockType) -> bool { LOCK_TYPE_UPGRADABLE_TO[self.index()][other_type.index()] }

    /// Returns `true` if the lock type can support child locks of the specified type, `false` otherwise.
    /// If `true`, this means that if a lock of this type is acquired for a parent `GLock`, a lock
    /// of the specified type can be acquired for a child `GLock`.
    pub fn supports_children(self, other_type: LockType) -> bool { LOCK_TYPE_SUPPORTS_CHILDREN[self.index()][other_type.index()] }

    /// Returns the least restrictive lock type that this lock type can be upgraded to, that is at
    /// least as restrictive as the specified type.
    pub fn min_upgradable(self, other_type: LockType) -> LockType {
        for lt in LOCK_TYPES.iter() {
            if self.upgradable_to(*lt) && other_type.upgradable_to(*lt) {
                return *lt;
            }
        }

        return LockType::Exclusive;
    }
}

impl Display for LockType {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match *self {
            LockType::IntentionShared           => { write!(f, "IntentionShared") },
            LockType::IntentionExclusive        => { write!(f, "IntentionExclusive") },
            LockType::Shared                    => { write!(f, "Shared") },
            LockType::SharedIntentionExclusive  => { write!(f, "SharedIntentionExclusive") },
            LockType::Exclusive                 => { write!(f, "Exclusive") },
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn implicit_parent_type() {
        assert_eq!(LockType::IntentionShared.implicit_parent_type(), LockType::IntentionShared);
        assert_eq!(LockType::IntentionExclusive.implicit_parent_type(), LockType::IntentionExclusive);
        assert_eq!(LockType::Shared.implicit_parent_type(), LockType::IntentionShared);
        assert_eq!(LockType::SharedIntentionExclusive.implicit_parent_type(), LockType::IntentionExclusive);
        assert_eq!(LockType::Exclusive.implicit_parent_type(), LockType::IntentionExclusive);
    }

    #[test]
    fn compatible_with() {
        assert_eq!(LockType::IntentionShared.compatible_with(LockType::IntentionShared), true);
        assert_eq!(LockType::IntentionShared.compatible_with(LockType::IntentionExclusive), true);
        assert_eq!(LockType::IntentionShared.compatible_with(LockType::Shared), true);
        assert_eq!(LockType::IntentionShared.compatible_with(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::IntentionShared.compatible_with(LockType::Exclusive), false);

        assert_eq!(LockType::IntentionExclusive.compatible_with(LockType::IntentionShared), true);
        assert_eq!(LockType::IntentionExclusive.compatible_with(LockType::IntentionExclusive), true);
        assert_eq!(LockType::IntentionExclusive.compatible_with(LockType::Shared), false);
        assert_eq!(LockType::IntentionExclusive.compatible_with(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::IntentionExclusive.compatible_with(LockType::Exclusive), false);

        assert_eq!(LockType::Shared.compatible_with(LockType::IntentionShared), true);
        assert_eq!(LockType::Shared.compatible_with(LockType::IntentionExclusive), false);
        assert_eq!(LockType::Shared.compatible_with(LockType::Shared), true);
        assert_eq!(LockType::Shared.compatible_with(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::Shared.compatible_with(LockType::Exclusive), false);

        assert_eq!(LockType::SharedIntentionExclusive.compatible_with(LockType::IntentionShared), true);
        assert_eq!(LockType::SharedIntentionExclusive.compatible_with(LockType::IntentionExclusive), false);
        assert_eq!(LockType::SharedIntentionExclusive.compatible_with(LockType::Shared), false);
        assert_eq!(LockType::SharedIntentionExclusive.compatible_with(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::SharedIntentionExclusive.compatible_with(LockType::Exclusive), false);

        assert_eq!(LockType::Exclusive.compatible_with(LockType::IntentionShared), false);
        assert_eq!(LockType::Exclusive.compatible_with(LockType::IntentionExclusive), false);
        assert_eq!(LockType::Exclusive.compatible_with(LockType::Shared), false);
        assert_eq!(LockType::Exclusive.compatible_with(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::Exclusive.compatible_with(LockType::Exclusive), false);
    }

    #[test]
    fn upgradable_to() {
        assert_eq!(LockType::IntentionShared.upgradable_to(LockType::IntentionShared), true);
        assert_eq!(LockType::IntentionShared.upgradable_to(LockType::IntentionExclusive), true);
        assert_eq!(LockType::IntentionShared.upgradable_to(LockType::Shared), true);
        assert_eq!(LockType::IntentionShared.upgradable_to(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::IntentionShared.upgradable_to(LockType::Exclusive), true);

        assert_eq!(LockType::IntentionExclusive.upgradable_to(LockType::IntentionShared), false);
        assert_eq!(LockType::IntentionExclusive.upgradable_to(LockType::IntentionExclusive), true);
        assert_eq!(LockType::IntentionExclusive.upgradable_to(LockType::Shared), false);
        assert_eq!(LockType::IntentionExclusive.upgradable_to(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::IntentionExclusive.upgradable_to(LockType::Exclusive), true);

        assert_eq!(LockType::Shared.upgradable_to(LockType::IntentionShared), false);
        assert_eq!(LockType::Shared.upgradable_to(LockType::IntentionExclusive), false);
        assert_eq!(LockType::Shared.upgradable_to(LockType::Shared), true);
        assert_eq!(LockType::Shared.upgradable_to(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::Shared.upgradable_to(LockType::Exclusive), true);

        assert_eq!(LockType::SharedIntentionExclusive.upgradable_to(LockType::IntentionShared), false);
        assert_eq!(LockType::SharedIntentionExclusive.upgradable_to(LockType::IntentionExclusive), false);
        assert_eq!(LockType::SharedIntentionExclusive.upgradable_to(LockType::Shared), false);
        assert_eq!(LockType::SharedIntentionExclusive.upgradable_to(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::SharedIntentionExclusive.upgradable_to(LockType::Exclusive), true);

        assert_eq!(LockType::Exclusive.upgradable_to(LockType::IntentionShared), false);
        assert_eq!(LockType::Exclusive.upgradable_to(LockType::IntentionExclusive), false);
        assert_eq!(LockType::Exclusive.upgradable_to(LockType::Shared), false);
        assert_eq!(LockType::Exclusive.upgradable_to(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::Exclusive.upgradable_to(LockType::Exclusive), true);
    }

    #[test]
    fn supports_children() {
        assert_eq!(LockType::IntentionShared.supports_children(LockType::IntentionShared), true);
        assert_eq!(LockType::IntentionShared.supports_children(LockType::IntentionExclusive), false);
        assert_eq!(LockType::IntentionShared.supports_children(LockType::Shared), true);
        assert_eq!(LockType::IntentionShared.supports_children(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::IntentionShared.supports_children(LockType::Exclusive), false);

        assert_eq!(LockType::IntentionExclusive.supports_children(LockType::IntentionShared), true);
        assert_eq!(LockType::IntentionExclusive.supports_children(LockType::IntentionExclusive), true);
        assert_eq!(LockType::IntentionExclusive.supports_children(LockType::Shared), true);
        assert_eq!(LockType::IntentionExclusive.supports_children(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::IntentionExclusive.supports_children(LockType::Exclusive), true);

        assert_eq!(LockType::Shared.supports_children(LockType::IntentionShared), true);
        assert_eq!(LockType::Shared.supports_children(LockType::IntentionExclusive), false);
        assert_eq!(LockType::Shared.supports_children(LockType::Shared), true);
        assert_eq!(LockType::Shared.supports_children(LockType::SharedIntentionExclusive), false);
        assert_eq!(LockType::Shared.supports_children(LockType::Exclusive), false);

        assert_eq!(LockType::SharedIntentionExclusive.supports_children(LockType::IntentionShared), true);
        assert_eq!(LockType::SharedIntentionExclusive.supports_children(LockType::IntentionExclusive), true);
        assert_eq!(LockType::SharedIntentionExclusive.supports_children(LockType::Shared), true);
        assert_eq!(LockType::SharedIntentionExclusive.supports_children(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::SharedIntentionExclusive.supports_children(LockType::Exclusive), true);

        assert_eq!(LockType::Exclusive.supports_children(LockType::IntentionShared), true);
        assert_eq!(LockType::Exclusive.supports_children(LockType::IntentionExclusive), true);
        assert_eq!(LockType::Exclusive.supports_children(LockType::Shared), true);
        assert_eq!(LockType::Exclusive.supports_children(LockType::SharedIntentionExclusive), true);
        assert_eq!(LockType::Exclusive.supports_children(LockType::Exclusive), true);
    }

    #[test]
    fn min_upgradable() {
        assert_eq!(LockType::IntentionShared.min_upgradable(LockType::IntentionShared), LockType::IntentionShared);
        assert_eq!(LockType::IntentionShared.min_upgradable(LockType::IntentionExclusive), LockType::IntentionExclusive);
        assert_eq!(LockType::IntentionShared.min_upgradable(LockType::Shared), LockType::Shared);
        assert_eq!(LockType::IntentionShared.min_upgradable(LockType::SharedIntentionExclusive), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::IntentionShared.min_upgradable(LockType::Exclusive), LockType::Exclusive);

        assert_eq!(LockType::IntentionExclusive.min_upgradable(LockType::IntentionShared), LockType::IntentionExclusive);
        assert_eq!(LockType::IntentionExclusive.min_upgradable(LockType::IntentionExclusive), LockType::IntentionExclusive);
        assert_eq!(LockType::IntentionExclusive.min_upgradable(LockType::Shared), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::IntentionExclusive.min_upgradable(LockType::SharedIntentionExclusive), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::IntentionExclusive.min_upgradable(LockType::Exclusive), LockType::Exclusive);

        assert_eq!(LockType::Shared.min_upgradable(LockType::IntentionShared), LockType::Shared);
        assert_eq!(LockType::Shared.min_upgradable(LockType::IntentionExclusive), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::Shared.min_upgradable(LockType::Shared), LockType::Shared);
        assert_eq!(LockType::Shared.min_upgradable(LockType::SharedIntentionExclusive), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::Shared.min_upgradable(LockType::Exclusive), LockType::Exclusive);

        assert_eq!(LockType::SharedIntentionExclusive.min_upgradable(LockType::IntentionShared), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::SharedIntentionExclusive.min_upgradable(LockType::IntentionExclusive), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::SharedIntentionExclusive.min_upgradable(LockType::Shared), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::SharedIntentionExclusive.min_upgradable(LockType::SharedIntentionExclusive), LockType::SharedIntentionExclusive);
        assert_eq!(LockType::SharedIntentionExclusive.min_upgradable(LockType::Exclusive), LockType::Exclusive);

        assert_eq!(LockType::Exclusive.min_upgradable(LockType::IntentionShared), LockType::Exclusive);
        assert_eq!(LockType::Exclusive.min_upgradable(LockType::IntentionExclusive), LockType::Exclusive);
        assert_eq!(LockType::Exclusive.min_upgradable(LockType::Shared), LockType::Exclusive);
        assert_eq!(LockType::Exclusive.min_upgradable(LockType::SharedIntentionExclusive), LockType::Exclusive);
        assert_eq!(LockType::Exclusive.min_upgradable(LockType::Exclusive), LockType::Exclusive);
    }
}