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

Yuya Nishihara yuya at tcha.org
Sun Sep 22 02:41:40 EDT 2019


# 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 {}
+
 impl PySharedState {
     pub fn borrow_mut<'a, T>(
         &'a self,


More information about the Mercurial-devel mailing list