Skip to content

Commit

Permalink
Drop parking_lot in favor of std::sync
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Apr 26, 2024
1 parent 630eb98 commit 46eb338
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rust-version = "1.63"
[dependencies]
cfg-if = "1.0"
libc = "0.2.62"
parking_lot = ">= 0.11, < 0.13"
memoffset = "0.9"
portable-atomic = "1.0"

Expand Down
9 changes: 4 additions & 5 deletions src/coroutine/cancel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{Py, PyAny, PyObject};
use parking_lot::Mutex;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};

#[derive(Debug, Default)]
Expand All @@ -25,12 +24,12 @@ impl CancelHandle {

/// Returns whether the associated coroutine has been cancelled.
pub fn is_cancelled(&self) -> bool {
self.0.lock().exception.is_some()
self.0.lock().unwrap().exception.is_some()
}

/// Poll to retrieve the exception thrown in the associated coroutine.
pub fn poll_cancelled(&mut self, cx: &mut Context<'_>) -> Poll<PyObject> {
let mut inner = self.0.lock();
let mut inner = self.0.lock().unwrap();
if let Some(exc) = inner.exception.take() {
return Poll::Ready(exc);
}
Expand Down Expand Up @@ -69,7 +68,7 @@ pub struct ThrowCallback(Arc<Mutex<Inner>>);

impl ThrowCallback {
pub(super) fn throw(&self, exc: Py<PyAny>) {
let mut inner = self.0.lock();
let mut inner = self.0.lock().unwrap();
inner.exception = Some(exc);
if let Some(waker) = inner.waker.take() {
waker.wake();
Expand Down
48 changes: 23 additions & 25 deletions src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
use crate::impl_::not_send::{NotSend, NOT_SEND};
use crate::{ffi, Python};
use parking_lot::{const_mutex, Mutex, Once};
use std::cell::Cell;
#[cfg(debug_assertions)]
use std::cell::RefCell;
#[cfg(not(debug_assertions))]
use std::cell::UnsafeCell;
use std::{mem, ptr::NonNull};
use std::{mem, ptr::NonNull, sync};

static START: Once = Once::new();
static START: sync::Once = sync::Once::new();

cfg_if::cfg_if! {
if #[cfg(thread_local_const_init)] {
Expand Down Expand Up @@ -249,26 +248,26 @@ type PyObjVec = Vec<NonNull<ffi::PyObject>>;
/// Thread-safe storage for objects which were inc_ref / dec_ref while the GIL was not held.
struct ReferencePool {
// .0 is INCREFs, .1 is DECREFs
pointer_ops: Mutex<(PyObjVec, PyObjVec)>,
pointer_ops: sync::Mutex<(PyObjVec, PyObjVec)>,
}

impl ReferencePool {
const fn new() -> Self {
Self {
pointer_ops: const_mutex((Vec::new(), Vec::new())),
pointer_ops: sync::Mutex::new((Vec::new(), Vec::new())),
}
}

fn register_incref(&self, obj: NonNull<ffi::PyObject>) {
self.pointer_ops.lock().0.push(obj);
self.pointer_ops.lock().unwrap().0.push(obj);
}

fn register_decref(&self, obj: NonNull<ffi::PyObject>) {
self.pointer_ops.lock().1.push(obj);
self.pointer_ops.lock().unwrap().1.push(obj);
}

fn update_counts(&self, _py: Python<'_>) {
let mut ops = self.pointer_ops.lock();
let mut ops = self.pointer_ops.lock().unwrap();
if ops.0.is_empty() && ops.1.is_empty() {
return;
}
Expand Down Expand Up @@ -523,9 +522,8 @@ mod tests {
use super::{gil_is_acquired, GIL_COUNT, OWNED_OBJECTS, POOL};
use crate::types::any::PyAnyMethods;
use crate::{ffi, gil, PyObject, Python};
#[cfg(not(target_arch = "wasm32"))]
use parking_lot::{const_mutex, Condvar, Mutex};
use std::ptr::NonNull;
use std::sync;

fn get_object(py: Python<'_>) -> PyObject {
py.eval_bound("object()", None, None).unwrap().unbind()
Expand All @@ -543,6 +541,7 @@ mod tests {
!POOL
.pointer_ops
.lock()
.unwrap()
.0
.contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) })
}
Expand All @@ -551,6 +550,7 @@ mod tests {
!POOL
.pointer_ops
.lock()
.unwrap()
.1
.contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) })
}
Expand All @@ -559,6 +559,7 @@ mod tests {
fn pool_dec_refs_contains(obj: &PyObject) -> bool {
POOL.pointer_ops
.lock()
.unwrap()
.1
.contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) })
}
Expand Down Expand Up @@ -671,8 +672,8 @@ mod tests {
Python::with_gil(|py| {
assert_eq!(obj.get_refcnt(py), 1);
let non_null = unsafe { NonNull::new_unchecked(obj.as_ptr()) };
assert!(!POOL.pointer_ops.lock().0.contains(&non_null));
assert!(!POOL.pointer_ops.lock().1.contains(&non_null));
assert!(!POOL.pointer_ops.lock().unwrap().0.contains(&non_null));
assert!(!POOL.pointer_ops.lock().unwrap().1.contains(&non_null));
});
}

Expand Down Expand Up @@ -770,29 +771,26 @@ mod tests {

#[cfg(not(target_arch = "wasm32"))]
struct Event {
set: Mutex<bool>,
wait: Condvar,
set: sync::Mutex<bool>,
wait: sync::Condvar,
}

#[cfg(not(target_arch = "wasm32"))]
impl Event {
const fn new() -> Self {
Self {
set: const_mutex(false),
wait: Condvar::new(),
set: sync::Mutex::new(false),
wait: sync::Condvar::new(),
}
}

fn set(&self) {
*self.set.lock() = true;
*self.set.lock().unwrap() = true;
self.wait.notify_all();
}

fn wait(&self) {
let mut set = self.set.lock();
while !*set {
self.wait.wait(&mut set);
}
drop(self.wait.wait_while(self.set.lock().unwrap(), |s| !*s));
}
}

Expand Down Expand Up @@ -891,16 +889,16 @@ mod tests {

// The pointer should appear once in the incref pool, and once in the
// decref pool (for the clone being created and also dropped)
assert!(POOL.pointer_ops.lock().0.contains(&ptr));
assert!(POOL.pointer_ops.lock().1.contains(&ptr));
assert!(POOL.pointer_ops.lock().unwrap().0.contains(&ptr));
assert!(POOL.pointer_ops.lock().unwrap().1.contains(&ptr));

(obj, count, ptr)
});

Python::with_gil(|py| {
// Acquiring the gil clears the pool
assert!(!POOL.pointer_ops.lock().0.contains(&ptr));
assert!(!POOL.pointer_ops.lock().1.contains(&ptr));
assert!(!POOL.pointer_ops.lock().unwrap().0.contains(&ptr));
assert!(!POOL.pointer_ops.lock().unwrap().1.contains(&ptr));

// Overall count is still unchanged
assert_eq!(count, obj.get_refcnt(py));
Expand Down

0 comments on commit 46eb338

Please # to comment.