2023-02-21 20:59:20 +00:00
|
|
|
//! Synchronization mechanisms based on the Python GIL.
|
2023-11-30 21:27:57 +00:00
|
|
|
use crate::{types::PyString, types::PyType, Py, PyErr, PyVisit, Python};
|
2020-06-14 15:29:40 +00:00
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
|
2023-02-21 20:59:20 +00:00
|
|
|
/// Value with concurrent access protected by the GIL.
|
|
|
|
///
|
|
|
|
/// This is a synchronization primitive based on Python's global interpreter lock (GIL).
|
|
|
|
/// It ensures that only one thread at a time can access the inner value via shared references.
|
|
|
|
/// It can be combined with interior mutability to obtain mutable references.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Combining `GILProtected` with `RefCell` enables mutable access to static data:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use pyo3::prelude::*;
|
|
|
|
/// use pyo3::sync::GILProtected;
|
|
|
|
/// use std::cell::RefCell;
|
|
|
|
///
|
|
|
|
/// static NUMBERS: GILProtected<RefCell<Vec<i32>>> = GILProtected::new(RefCell::new(Vec::new()));
|
|
|
|
///
|
|
|
|
/// Python::with_gil(|py| {
|
|
|
|
/// NUMBERS.get(py).borrow_mut().push(42);
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
pub struct GILProtected<T> {
|
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> GILProtected<T> {
|
|
|
|
/// Place the given value under the protection of the GIL.
|
|
|
|
pub const fn new(value: T) -> Self {
|
|
|
|
Self { value }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gain access to the inner value by giving proof of having acquired the GIL.
|
|
|
|
pub fn get<'py>(&'py self, _py: Python<'py>) -> &'py T {
|
|
|
|
&self.value
|
|
|
|
}
|
2023-11-30 21:27:57 +00:00
|
|
|
|
|
|
|
/// Gain access to the inner value by giving proof that garbage collection is happening.
|
|
|
|
pub fn traverse<'py>(&'py self, _visit: PyVisit<'py>) -> &'py T {
|
|
|
|
&self.value
|
|
|
|
}
|
2023-02-21 20:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T> Sync for GILProtected<T> where T: Send {}
|
|
|
|
|
2022-05-24 12:29:18 +00:00
|
|
|
/// A write-once cell similar to [`once_cell::OnceCell`](https://docs.rs/once_cell/latest/once_cell/).
|
2020-06-14 15:29:40 +00:00
|
|
|
///
|
|
|
|
/// Unlike `once_cell::sync` which blocks threads to achieve thread safety, this implementation
|
2022-12-26 20:38:25 +00:00
|
|
|
/// uses the Python GIL to mediate concurrent access. This helps in cases where `once_cell` or
|
2020-06-14 15:29:40 +00:00
|
|
|
/// `lazy_static`'s synchronization strategy can lead to deadlocks when interacting with the Python
|
2021-07-24 07:47:02 +00:00
|
|
|
/// GIL. For an example, see [the FAQ section](https://pyo3.rs/latest/faq.html) of the guide.
|
2020-06-14 15:29:40 +00:00
|
|
|
///
|
2022-12-26 20:38:25 +00:00
|
|
|
/// Note that:
|
|
|
|
/// 1) `get_or_init` and `get_or_try_init` do not protect against infinite recursion
|
|
|
|
/// from reentrant initialization.
|
|
|
|
/// 2) If the initialization function `f` provided to `get_or_init` (or `get_or_try_init`)
|
|
|
|
/// temporarily releases the GIL (e.g. by calling `Python::import`) then it is possible
|
|
|
|
/// for a second thread to also begin initializing the `GITOnceCell`. Even when this
|
|
|
|
/// happens `GILOnceCell` guarantees that only **one** write to the cell ever occurs
|
|
|
|
/// - this is treated as a race, other threads will discard the value they compute and
|
|
|
|
/// return the result of the first complete computation.
|
|
|
|
///
|
2021-03-20 07:45:56 +00:00
|
|
|
/// # Examples
|
2020-06-14 15:29:40 +00:00
|
|
|
///
|
|
|
|
/// The following example shows how to use `GILOnceCell` to share a reference to a Python list
|
|
|
|
/// between threads:
|
|
|
|
///
|
|
|
|
/// ```
|
2023-02-21 20:59:20 +00:00
|
|
|
/// use pyo3::sync::GILOnceCell;
|
2020-06-14 15:29:40 +00:00
|
|
|
/// use pyo3::prelude::*;
|
|
|
|
/// use pyo3::types::PyList;
|
|
|
|
///
|
|
|
|
/// static LIST_CELL: GILOnceCell<Py<PyList>> = GILOnceCell::new();
|
|
|
|
///
|
2022-03-23 07:07:28 +00:00
|
|
|
/// pub fn get_shared_list(py: Python<'_>) -> &PyList {
|
2020-06-14 15:29:40 +00:00
|
|
|
/// LIST_CELL
|
|
|
|
/// .get_or_init(py, || PyList::empty(py).into())
|
|
|
|
/// .as_ref(py)
|
|
|
|
/// }
|
2021-03-20 07:44:28 +00:00
|
|
|
/// # Python::with_gil(|py| assert_eq!(get_shared_list(py).len(), 0));
|
2020-06-14 15:29:40 +00:00
|
|
|
/// ```
|
|
|
|
pub struct GILOnceCell<T>(UnsafeCell<Option<T>>);
|
|
|
|
|
|
|
|
// T: Send is needed for Sync because the thread which drops the GILOnceCell can be different
|
|
|
|
// to the thread which fills it.
|
|
|
|
unsafe impl<T: Send + Sync> Sync for GILOnceCell<T> {}
|
|
|
|
unsafe impl<T: Send> Send for GILOnceCell<T> {}
|
|
|
|
|
|
|
|
impl<T> GILOnceCell<T> {
|
|
|
|
/// Create a `GILOnceCell` which does not yet contain a value.
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self(UnsafeCell::new(None))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the contained value, or `None` if the cell has not yet been written.
|
2022-04-03 14:21:37 +00:00
|
|
|
#[inline]
|
2022-03-23 07:07:28 +00:00
|
|
|
pub fn get(&self, _py: Python<'_>) -> Option<&T> {
|
2020-06-14 15:29:40 +00:00
|
|
|
// Safe because if the cell has not yet been written, None is returned.
|
|
|
|
unsafe { &*self.0.get() }.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the contained value, initializing it if needed using the provided
|
|
|
|
/// closure.
|
|
|
|
///
|
2022-12-26 20:38:25 +00:00
|
|
|
/// See the type-level documentation for detail on re-entrancy and concurrent initialization.
|
2022-04-03 14:21:37 +00:00
|
|
|
#[inline]
|
2022-03-23 07:07:28 +00:00
|
|
|
pub fn get_or_init<F>(&self, py: Python<'_>, f: F) -> &T
|
2020-06-14 15:29:40 +00:00
|
|
|
where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
2022-04-03 14:21:37 +00:00
|
|
|
if let Some(value) = self.get(py) {
|
2020-06-14 15:29:40 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-05-24 12:29:18 +00:00
|
|
|
match self.init(py, || Ok::<T, std::convert::Infallible>(f())) {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(void) => match void {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 20:38:25 +00:00
|
|
|
/// Like `get_or_init`, but accepts a fallible initialization function. If it fails, the cell
|
|
|
|
/// is left uninitialized.
|
2022-05-24 12:29:18 +00:00
|
|
|
///
|
2022-12-26 20:38:25 +00:00
|
|
|
/// See the type-level documentation for detail on re-entrancy and concurrent initialization.
|
2022-05-24 12:29:18 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn get_or_try_init<F, E>(&self, py: Python<'_>, f: F) -> Result<&T, E>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> Result<T, E>,
|
|
|
|
{
|
|
|
|
if let Some(value) = self.get(py) {
|
|
|
|
return Ok(value);
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:21:37 +00:00
|
|
|
self.init(py, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cold]
|
2022-05-24 12:29:18 +00:00
|
|
|
fn init<F, E>(&self, py: Python<'_>, f: F) -> Result<&T, E>
|
2022-04-03 14:21:37 +00:00
|
|
|
where
|
2022-05-24 12:29:18 +00:00
|
|
|
F: FnOnce() -> Result<T, E>,
|
2022-04-03 14:21:37 +00:00
|
|
|
{
|
2020-06-14 15:29:40 +00:00
|
|
|
// Note that f() could temporarily release the GIL, so it's possible that another thread
|
|
|
|
// writes to this GILOnceCell before f() finishes. That's fine; we'll just have to discard
|
|
|
|
// the value computed here and accept a bit of wasted computation.
|
2022-05-24 12:29:18 +00:00
|
|
|
let value = f()?;
|
2020-06-14 15:29:40 +00:00
|
|
|
let _ = self.set(py, value);
|
|
|
|
|
2022-05-24 12:29:18 +00:00
|
|
|
Ok(self.get(py).unwrap())
|
2020-06-14 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the contents of the cell mutably. This is only possible if the reference to the cell is
|
|
|
|
/// unique.
|
|
|
|
pub fn get_mut(&mut self) -> Option<&mut T> {
|
2023-10-29 12:50:36 +00:00
|
|
|
self.0.get_mut().as_mut()
|
2020-06-14 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the value in the cell.
|
|
|
|
///
|
|
|
|
/// If the cell has already been written, `Err(value)` will be returned containing the new
|
|
|
|
/// value which was not written.
|
2022-03-23 07:07:28 +00:00
|
|
|
pub fn set(&self, _py: Python<'_>, value: T) -> Result<(), T> {
|
2020-06-14 15:29:40 +00:00
|
|
|
// Safe because GIL is held, so no other thread can be writing to this cell concurrently.
|
|
|
|
let inner = unsafe { &mut *self.0.get() };
|
|
|
|
if inner.is_some() {
|
|
|
|
return Err(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
*inner = Some(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-29 07:28:39 +00:00
|
|
|
|
|
|
|
/// Takes the value out of the cell, moving it back to an uninitialized state.
|
|
|
|
///
|
|
|
|
/// Has no effect and returns None if the cell has not yet been written.
|
|
|
|
pub fn take(&mut self) -> Option<T> {
|
|
|
|
self.0.get_mut().take()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes the cell, returning the wrapped value.
|
|
|
|
///
|
|
|
|
/// Returns None if the cell has not yet been written.
|
|
|
|
pub fn into_inner(self) -> Option<T> {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
2020-06-14 15:29:40 +00:00
|
|
|
}
|
2022-04-03 10:27:34 +00:00
|
|
|
|
2023-06-01 11:29:23 +00:00
|
|
|
impl GILOnceCell<Py<PyType>> {
|
|
|
|
/// Get a reference to the contained Python type, initializing it if needed.
|
|
|
|
///
|
|
|
|
/// This is a shorthand method for `get_or_init` which imports the type from Python on init.
|
|
|
|
pub(crate) fn get_or_try_init_type_ref<'py>(
|
|
|
|
&'py self,
|
|
|
|
py: Python<'py>,
|
|
|
|
module_name: &str,
|
|
|
|
attr_name: &str,
|
|
|
|
) -> Result<&'py PyType, PyErr> {
|
|
|
|
self.get_or_try_init(py, || py.import(module_name)?.getattr(attr_name)?.extract())
|
|
|
|
.map(|ty| ty.as_ref(py))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:58:51 +00:00
|
|
|
/// Interns `text` as a Python string and stores a reference to it in static storage.
|
2022-04-03 10:27:34 +00:00
|
|
|
///
|
2022-04-03 18:58:51 +00:00
|
|
|
/// A reference to the same Python string is returned on each invocation.
|
2022-04-03 10:27:34 +00:00
|
|
|
///
|
2022-04-03 18:58:51 +00:00
|
|
|
/// # Example: Using `intern!` to avoid needlessly recreating the same Python string
|
2022-04-03 10:27:34 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use pyo3::intern;
|
2022-04-04 18:25:47 +00:00
|
|
|
/// # use pyo3::{pyfunction, types::PyDict, wrap_pyfunction, PyResult, Python};
|
2022-04-03 10:27:34 +00:00
|
|
|
///
|
|
|
|
/// #[pyfunction]
|
|
|
|
/// fn create_dict(py: Python<'_>) -> PyResult<&PyDict> {
|
2022-11-20 12:47:06 +00:00
|
|
|
/// let dict = PyDict::new(py);
|
|
|
|
/// // 👇 A new `PyString` is created
|
|
|
|
/// // for every call of this function.
|
|
|
|
/// dict.set_item("foo", 42)?;
|
|
|
|
/// Ok(dict)
|
2022-04-03 10:27:34 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[pyfunction]
|
|
|
|
/// fn create_dict_faster(py: Python<'_>) -> PyResult<&PyDict> {
|
2022-11-20 12:47:06 +00:00
|
|
|
/// let dict = PyDict::new(py);
|
|
|
|
/// // 👇 A `PyString` is created once and reused
|
|
|
|
/// // for the lifetime of the program.
|
|
|
|
/// dict.set_item(intern!(py, "foo"), 42)?;
|
|
|
|
/// Ok(dict)
|
2022-04-03 10:27:34 +00:00
|
|
|
/// }
|
2022-04-04 18:25:47 +00:00
|
|
|
/// #
|
|
|
|
/// # Python::with_gil(|py| {
|
2022-05-10 07:17:10 +00:00
|
|
|
/// # let fun_slow = wrap_pyfunction!(create_dict, py).unwrap();
|
|
|
|
/// # let dict = fun_slow.call0().unwrap();
|
|
|
|
/// # assert!(dict.contains("foo").unwrap());
|
2022-04-04 18:25:47 +00:00
|
|
|
/// # let fun = wrap_pyfunction!(create_dict_faster, py).unwrap();
|
|
|
|
/// # let dict = fun.call0().unwrap();
|
|
|
|
/// # assert!(dict.contains("foo").unwrap());
|
|
|
|
/// # });
|
2022-04-03 10:27:34 +00:00
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! intern {
|
2022-04-04 16:44:19 +00:00
|
|
|
($py: expr, $text: expr) => {{
|
2023-02-21 20:59:20 +00:00
|
|
|
static INTERNED: $crate::sync::Interned = $crate::sync::Interned::new($text);
|
2022-06-01 08:07:00 +00:00
|
|
|
INTERNED.get($py)
|
2022-04-03 10:27:34 +00:00
|
|
|
}};
|
|
|
|
}
|
2022-04-04 16:44:19 +00:00
|
|
|
|
2022-06-01 08:07:00 +00:00
|
|
|
/// Implementation detail for `intern!` macro.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct Interned(&'static str, GILOnceCell<Py<PyString>>);
|
|
|
|
|
|
|
|
impl Interned {
|
2022-09-06 18:26:23 +00:00
|
|
|
/// Creates an empty holder for an interned `str`.
|
2022-06-01 08:07:00 +00:00
|
|
|
pub const fn new(value: &'static str) -> Self {
|
|
|
|
Interned(value, GILOnceCell::new())
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:26:23 +00:00
|
|
|
/// Gets or creates the interned `str` value.
|
2022-06-01 08:07:00 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn get<'py>(&'py self, py: Python<'py>) -> &'py PyString {
|
|
|
|
self.1
|
|
|
|
.get_or_init(py, || PyString::intern(py, self.0).into())
|
|
|
|
.as_ref(py)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:44:19 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::types::PyDict;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intern() {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let foo1 = "foo";
|
|
|
|
let foo2 = intern!(py, "foo");
|
|
|
|
let foo3 = intern!(py, stringify!(foo));
|
|
|
|
|
|
|
|
let dict = PyDict::new(py);
|
|
|
|
dict.set_item(foo1, 42_usize).unwrap();
|
|
|
|
assert!(dict.contains(foo2).unwrap());
|
2023-07-19 20:24:24 +00:00
|
|
|
assert_eq!(
|
|
|
|
dict.get_item(foo3)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.extract::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
42
|
|
|
|
);
|
2022-04-04 16:44:19 +00:00
|
|
|
});
|
|
|
|
}
|
2022-05-24 12:29:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_once_cell() {
|
|
|
|
Python::with_gil(|py| {
|
2023-10-29 07:28:39 +00:00
|
|
|
let mut cell = GILOnceCell::new();
|
2022-05-24 12:29:18 +00:00
|
|
|
|
|
|
|
assert!(cell.get(py).is_none());
|
|
|
|
|
|
|
|
assert_eq!(cell.get_or_try_init(py, || Err(5)), Err(5));
|
|
|
|
assert!(cell.get(py).is_none());
|
|
|
|
|
|
|
|
assert_eq!(cell.get_or_try_init(py, || Ok::<_, ()>(2)), Ok(&2));
|
|
|
|
assert_eq!(cell.get(py), Some(&2));
|
|
|
|
|
|
|
|
assert_eq!(cell.get_or_try_init(py, || Err(5)), Ok(&2));
|
2023-10-29 07:28:39 +00:00
|
|
|
|
|
|
|
assert_eq!(cell.take(), Some(2));
|
|
|
|
assert_eq!(cell.into_inner(), None)
|
2022-05-24 12:29:18 +00:00
|
|
|
})
|
|
|
|
}
|
2022-04-04 16:44:19 +00:00
|
|
|
}
|