|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::fmt; |
| 16 | +use std::ops::Deref; |
| 17 | +use std::ops::DerefMut; |
| 18 | +use std::time::Instant; |
| 19 | + |
| 20 | +use log::info; |
| 21 | +use tokio::sync::RwLockReadGuard; |
| 22 | +use tokio::sync::RwLockWriteGuard; |
| 23 | + |
| 24 | +#[derive(Debug)] |
| 25 | +pub struct WriteGuard<'a, T: ?Sized> { |
| 26 | + acquired: Instant, |
| 27 | + purpose: String, |
| 28 | + inner: RwLockWriteGuard<'a, T>, |
| 29 | +} |
| 30 | + |
| 31 | +impl<'a, T> WriteGuard<'a, T> { |
| 32 | + pub fn new(purpose: impl ToString, inner: RwLockWriteGuard<'a, T>) -> Self { |
| 33 | + Self { |
| 34 | + acquired: Instant::now(), |
| 35 | + purpose: purpose.to_string(), |
| 36 | + inner, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<T> Deref for WriteGuard<'_, T> { |
| 42 | + type Target = T; |
| 43 | + |
| 44 | + fn deref(&self) -> &Self::Target { |
| 45 | + self.inner.deref() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<T> DerefMut for WriteGuard<'_, T> { |
| 50 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 51 | + self.inner.deref_mut() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<T> fmt::Display for WriteGuard<'_, T> |
| 56 | +where T: fmt::Display |
| 57 | +{ |
| 58 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 59 | + write!( |
| 60 | + f, |
| 61 | + "StateMachineLock-Write-Guard({}): {}", |
| 62 | + self.purpose, self.inner |
| 63 | + ) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<T: ?Sized> Drop for WriteGuard<'_, T> { |
| 68 | + fn drop(&mut self) { |
| 69 | + let elapsed = self.acquired.elapsed(); |
| 70 | + info!( |
| 71 | + "StateMachineLock-Write-Guard({}) released after {:?}", |
| 72 | + self.purpose, elapsed |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[derive(Debug)] |
| 78 | +pub struct ReadGuard<'a, T: ?Sized> { |
| 79 | + acquired: Instant, |
| 80 | + purpose: String, |
| 81 | + inner: RwLockReadGuard<'a, T>, |
| 82 | +} |
| 83 | + |
| 84 | +impl<'a, T> ReadGuard<'a, T> { |
| 85 | + pub fn new(purpose: impl ToString, inner: RwLockReadGuard<'a, T>) -> Self { |
| 86 | + Self { |
| 87 | + acquired: Instant::now(), |
| 88 | + purpose: purpose.to_string(), |
| 89 | + inner, |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<T> Deref for ReadGuard<'_, T> { |
| 95 | + type Target = T; |
| 96 | + |
| 97 | + fn deref(&self) -> &Self::Target { |
| 98 | + self.inner.deref() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<T> fmt::Display for ReadGuard<'_, T> |
| 103 | +where T: fmt::Display |
| 104 | +{ |
| 105 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 106 | + write!( |
| 107 | + f, |
| 108 | + "StateMachineLock-Read-Guard({}): {}", |
| 109 | + self.purpose, self.inner |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl<T: ?Sized> Drop for ReadGuard<'_, T> { |
| 115 | + fn drop(&mut self) { |
| 116 | + let elapsed = self.acquired.elapsed(); |
| 117 | + info!( |
| 118 | + "StateMachineLock-Read-Guard({}) released after {:?}", |
| 119 | + self.purpose, elapsed |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
0 commit comments