[PATCH 03 of 10] rust-cpython: mark PySharedState as Sync so &'PySharedState can be Send

Yuya Nishihara yuya at tcha.org
Sat Oct 5 15:03:02 UTC 2019


On Sun, 22 Sep 2019 15:41:40 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya at tcha.org>
> # Date 1568674765 -32400
> #      Tue Sep 17 07:59:25 2019 +0900
> # Node ID fcaf01804ac8198d52d141d642d5ef7c935360cc
> # Parent  34a355c69f643f15ff46f4524c75137c08a8de3b
> rust-cpython: mark PySharedState as Sync so &'PySharedState can be Send
> 
> The goal is to store &'static PySharedState in $leaked struct, which allows
> us to move the $leaked struct out of the macro. Currently, it depends on
> inner.$data_member(py), which can't be generalized.
> 
> PySharedState is Sync because any mutation or read operation is synchronized
> by the Python GIL, py: Python<'a>, which should guarantee that &'PySharedState
> can be sent to another thread.
> 
> diff --git a/rust/hg-cpython/src/ref_sharing.rs b/rust/hg-cpython/src/ref_sharing.rs
> --- a/rust/hg-cpython/src/ref_sharing.rs
> +++ b/rust/hg-cpython/src/ref_sharing.rs
> @@ -18,6 +18,10 @@ pub struct PySharedState {
>      mutably_borrowed: Cell<bool>,
>  }
>  
> +// &PySharedState can be Send because any access to inner cells is
> +// synchronized by the GIL.
> +unsafe impl Sync for PySharedState {}

Perhaps, this unsafe impl can be removed later.

If we take the generation-poisoning idea, PySharedState will be just
{ generation: AtomicUsize }, which is inherently Sync.


More information about the Mercurial-devel mailing list