Enable `GILProtected` access via `PyVisit`

Closes #3615

This simply adds a new method which uses the existence of a `PyVisit`
object as proof that the GIL is held instead of a `Python` object. This
allows `GILProtected` to be used in instances where contained Python
objects need to participate in garbage collection. Usage in this
situation should be valid since no Python calls are made and this does
not provide any additional mechanism for accessing a `Python` object.
This commit is contained in:
Nathan Kent 2023-11-30 13:27:57 -08:00 committed by Nate Kent
parent 81ad2e8bab
commit 3249feb85c
No known key found for this signature in database
GPG Key ID: 8B0077E00820E61C
2 changed files with 7 additions and 1 deletions

View File

@ -0,0 +1 @@
Add `traverse` method to `GILProtected`

View File

@ -1,5 +1,5 @@
//! Synchronization mechanisms based on the Python GIL.
use crate::{types::PyString, types::PyType, Py, PyErr, Python};
use crate::{types::PyString, types::PyType, Py, PyErr, PyVisit, Python};
use std::cell::UnsafeCell;
/// Value with concurrent access protected by the GIL.
@ -37,6 +37,11 @@ impl<T> GILProtected<T> {
pub fn get<'py>(&'py self, _py: Python<'py>) -> &'py T {
&self.value
}
/// 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
}
}
unsafe impl<T> Sync for GILProtected<T> where T: Send {}