parking_lot/
raw_fair_mutex.rs

1// Copyright 2016 Amanieu d'Antras
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::raw_mutex::RawMutex;
9use lock_api::RawMutexFair;
10
11/// Raw fair mutex type backed by the parking lot.
12pub struct RawFairMutex(RawMutex);
13
14unsafe impl lock_api::RawMutex for RawFairMutex {
15    const INIT: Self = RawFairMutex(<RawMutex as lock_api::RawMutex>::INIT);
16
17    type GuardMarker = <RawMutex as lock_api::RawMutex>::GuardMarker;
18
19    #[inline]
20    fn lock(&self) {
21        self.0.lock()
22    }
23
24    #[inline]
25    fn try_lock(&self) -> bool {
26        self.0.try_lock()
27    }
28
29    #[inline]
30    unsafe fn unlock(&self) {
31        self.unlock_fair()
32    }
33
34    #[inline]
35    fn is_locked(&self) -> bool {
36        self.0.is_locked()
37    }
38}
39
40unsafe impl lock_api::RawMutexFair for RawFairMutex {
41    #[inline]
42    unsafe fn unlock_fair(&self) {
43        self.0.unlock_fair()
44    }
45
46    #[inline]
47    unsafe fn bump(&self) {
48        self.0.bump()
49    }
50}
51
52unsafe impl lock_api::RawMutexTimed for RawFairMutex {
53    type Duration = <RawMutex as lock_api::RawMutexTimed>::Duration;
54    type Instant = <RawMutex as lock_api::RawMutexTimed>::Instant;
55
56    #[inline]
57    fn try_lock_until(&self, timeout: Self::Instant) -> bool {
58        self.0.try_lock_until(timeout)
59    }
60
61    #[inline]
62    fn try_lock_for(&self, timeout: Self::Duration) -> bool {
63        self.0.try_lock_for(timeout)
64    }
65}