tari_utilities/
locks.rs

1// Copyright 2019, The Tari Project
2//
3// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4// following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7// disclaimer.
8//
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10// following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13// products derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23//! Macros for RwLock.
24
25/// Recovers a poisoned lock by returning the value before the lock was poisoned
26#[macro_export]
27macro_rules! recover_lock {
28    ($e:expr) => {
29        match $e {
30            Ok(lock) => lock,
31            Err(poisoned) => {
32                log::warn!(target: "tari_util", "Lock has been POISONED and will be silently recovered");
33                poisoned.into_inner()
34            },
35        }
36    };
37}
38
39/// This macro returns a Mutex or RwLock guard without returning a `PoisonError`.
40/// If the lock is poisoned (i.e. a panic before a MutexGuard / RwLockGuard is dropped), the last value before the panic
41/// occurred is used. The semantics of this macro are similar to a database transaction rollback on failure.
42#[macro_export]
43macro_rules! acquire_lock {
44    ($e:expr, $m:ident) => {
45        $crate::recover_lock!($e.$m())
46    };
47    ($e:expr) => {
48        $crate::acquire_lock!($e, lock)
49    };
50}
51
52/// Acquire a write lock on a RwLock, silently recovering the lock if it is poisoned
53#[macro_export]
54macro_rules! acquire_write_lock {
55    ($e:expr) => {
56        $crate::acquire_lock!($e, write)
57    };
58}
59
60/// Acquire a read lock on a RwLock, silently recovering the lock if it is poisoned
61#[macro_export]
62macro_rules! acquire_read_lock {
63    ($e:expr) => {
64        $crate::acquire_lock!($e, read)
65    };
66}