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 
8 use crate::raw_mutex::RawMutex;
9 use lock_api::RawMutexFair;
10 
11 /// Raw fair mutex type backed by the parking lot.
12 pub struct RawFairMutex(RawMutex);
13 
14 unsafe 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]
lock(&self)20     fn lock(&self) {
21         self.0.lock()
22     }
23 
24     #[inline]
try_lock(&self) -> bool25     fn try_lock(&self) -> bool {
26         self.0.try_lock()
27     }
28 
29     #[inline]
unlock(&self)30     unsafe fn unlock(&self) {
31         self.unlock_fair()
32     }
33 
34     #[inline]
is_locked(&self) -> bool35     fn is_locked(&self) -> bool {
36         self.0.is_locked()
37     }
38 }
39 
40 unsafe impl lock_api::RawMutexFair for RawFairMutex {
41     #[inline]
unlock_fair(&self)42     unsafe fn unlock_fair(&self) {
43         self.0.unlock_fair()
44     }
45 
46     #[inline]
bump(&self)47     unsafe fn bump(&self) {
48         self.0.bump()
49     }
50 }
51 
52 unsafe 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]
try_lock_until(&self, timeout: Self::Instant) -> bool57     fn try_lock_until(&self, timeout: Self::Instant) -> bool {
58         self.0.try_lock_until(timeout)
59     }
60 
61     #[inline]
try_lock_for(&self, timeout: Self::Duration) -> bool62     fn try_lock_for(&self, timeout: Self::Duration) -> bool {
63         self.0.try_lock_for(timeout)
64     }
65 }
66