mirror of
https://github.com/Noratrieb/pythonic_global_lock.git
synced 2026-01-14 16:25:02 +01:00
awesomeness
This commit is contained in:
parent
ea28dc4f4c
commit
860c44707a
1 changed files with 26 additions and 8 deletions
34
src/lib.rs
34
src/lib.rs
|
|
@ -1,3 +1,7 @@
|
|||
//! 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.
|
||||
|
||||
use parking_lot::lock_api::{MutexGuard, RawMutex};
|
||||
use parking_lot::Mutex;
|
||||
use std::cell::UnsafeCell;
|
||||
|
|
@ -5,30 +9,44 @@ use std::ops::{Deref, DerefMut};
|
|||
|
||||
static LOCK: Mutex<()> = Mutex::const_new(RawMutex::INIT, ());
|
||||
|
||||
/// A global lock that locks every `GLock` using the same global lock (like the GIL in cpython lmao)
|
||||
pub struct GLock<T> {
|
||||
inner: UnsafeCell<T>,
|
||||
}
|
||||
|
||||
impl<T> GLock<T> {
|
||||
pub fn lock(&self) -> GLockGuard<T> {
|
||||
let mutex_guard = LOCK.lock();
|
||||
// SAFETY: We have the global lock
|
||||
let value = unsafe { &mut *self.inner.get() };
|
||||
GLockGuard { value, mutex_guard }
|
||||
/// Creates a new GLock
|
||||
pub const fn new(value: T) -> Self {
|
||||
Self {
|
||||
inner: UnsafeCell::new(value),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut T {
|
||||
/// Lock the global lock, not allowing any other `GLock` to be locked during the locked duration
|
||||
pub fn lock(&self) -> GLockGuard<T> {
|
||||
let global_guard = LOCK.lock();
|
||||
// SAFETY: We took the global lock above,
|
||||
// so no other one is allowed to get to this critical section here
|
||||
let value = unsafe { &mut *self.inner.get() };
|
||||
GLockGuard {
|
||||
value,
|
||||
global_guard,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn get_mut(&mut self) -> &mut T {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> T {
|
||||
pub const fn into_inner(self) -> T {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// A guard that guards a globally locked value
|
||||
struct GLockGuard<'a, T> {
|
||||
value: &'a mut T,
|
||||
mutex_guard: MutexGuard<'a, parking_lot::RawMutex, ()>,
|
||||
global_guard: MutexGuard<'a, parking_lot::RawMutex, ()>,
|
||||
}
|
||||
|
||||
impl<'a, T> Deref for GLockGuard<'a, T> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue