lighthouse/beacon_node/beacon_chain/src/timeout_rw_lock.rs
Paul Hauner f04c55075e
Add timeouts to canonical head rwlock (#759)
* Add TimeoutRwLock to BeaconChain

* Update network crate

* Update rest api

* Fix beacon chain tests

* Fix rest api tests

* Set test back to !debug_assertions
2020-01-06 17:30:37 +11:00

21 lines
638 B
Rust

use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::time::Duration;
/// A simple wrapper around `parking_lot::RwLock` that only permits read/write access with a
/// time-out (i.e., no indefinitely-blocking operations).
pub struct TimeoutRwLock<T>(RwLock<T>);
impl<T> TimeoutRwLock<T> {
pub fn new(inner: T) -> Self {
Self(RwLock::new(inner))
}
pub fn try_read_for(&self, timeout: Duration) -> Option<RwLockReadGuard<T>> {
self.0.try_read_for(timeout)
}
pub fn try_write_for(&self, timeout: Duration) -> Option<RwLockWriteGuard<T>> {
self.0.try_write_for(timeout)
}
}