awesomeness

This commit is contained in:
nora 2022-01-25 09:16:14 +01:00
parent 860c44707a
commit 695bac4e12
2 changed files with 24 additions and 8 deletions

18
README.md Normal file
View file

@ -0,0 +1,18 @@
# pythonic_global_lock
This crate provides a `GLock<T>`, that is globally locked. Every `GLock<T>` uses the same global lock, so locking on
will lock all. Sounds like a dumb idea? One of the most popular programming implementations does it, so it must be
smart.
```rust
fn main() {
let lock1 = pythonic_global_lock::GLock::new(1);
let lock2 = pythonic_global_lock::GLock::new(2);
{
let locked1 = lock1.lock();
println!("{}", &*locked1)
// locking lock2 here would be a deadlock
}
}
```

View file

@ -1,6 +1,4 @@
//! This crate provides a `GLock<T>`, that is globally locked. Every `GLock<T>` uses the same #![doc = include_str!("../README.md")]
//! global lock, so locking on will lock all. Sounds like a dumb idea? One of the most popular
//! programming implementations does it, so it must be smart.
use parking_lot::lock_api::{MutexGuard, RawMutex}; use parking_lot::lock_api::{MutexGuard, RawMutex};
use parking_lot::Mutex; use parking_lot::Mutex;
@ -30,23 +28,23 @@ impl<T> GLock<T> {
let value = unsafe { &mut *self.inner.get() }; let value = unsafe { &mut *self.inner.get() };
GLockGuard { GLockGuard {
value, value,
global_guard, _global_guard: global_guard,
} }
} }
pub const fn get_mut(&mut self) -> &mut T { pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut() self.inner.get_mut()
} }
pub const fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.inner.into_inner() self.inner.into_inner()
} }
} }
/// A guard that guards a globally locked value /// A guard that guards a globally locked value
struct GLockGuard<'a, T> { pub struct GLockGuard<'a, T> {
value: &'a mut T, value: &'a mut T,
global_guard: MutexGuard<'a, parking_lot::RawMutex, ()>, _global_guard: MutexGuard<'a, parking_lot::RawMutex, ()>,
} }
impl<'a, T> Deref for GLockGuard<'a, T> { impl<'a, T> Deref for GLockGuard<'a, T> {