rust-cpython: mark PySharedState as Sync so &'PySharedState can be Send
authorYuya Nishihara <yuya@tcha.org>
Tue, 17 Sep 2019 07:59:25 +0900
changeset 43175 a1908eb08342
parent 43174 1c675c5fe5fe
child 43176 aaec70a5f9a8
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.
rust/hg-cpython/src/ref_sharing.rs
--- a/rust/hg-cpython/src/ref_sharing.rs	Sat Sep 14 23:17:19 2019 +0900
+++ b/rust/hg-cpython/src/ref_sharing.rs	Tue Sep 17 07:59:25 2019 +0900
@@ -33,6 +33,10 @@
     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,