Patchwork [4,of,6,V3] rust-cpython: keep Python<'a> token in PyRefMut

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 17, 2019, 1:06 p.m.
Message ID <b8da799af04314e4bca9.1571317595@mimosa>
Download mbox | patch
Permalink /patch/42454/
State New
Headers show

Comments

Yuya Nishihara - Oct. 17, 2019, 1:06 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1569053101 -32400
#      Sat Sep 21 17:05:01 2019 +0900
# Node ID b8da799af04314e4bca94a4a8baf7686b6d75151
# Parent  eed6194fd69499df3eff272f8df144fe239405b1
rust-cpython: keep Python<'a> token in PyRefMut

This just clarifies that the GIL is obtained while PyRefMut is dereferenced,
so there's no need of extra acquire_gil() to drop the reference.

Patch

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
@@ -205,6 +205,7 @@  impl<'a, T> PySharedRef<'a, T> {
 
 /// Holds a mutable reference to data shared between Python and Rust.
 pub struct PyRefMut<'a, T> {
+    py: Python<'a>,
     inner: RefMut<'a, T>,
     py_shared_state: &'a PySharedState,
 }
@@ -213,11 +214,12 @@  impl<'a, T> PyRefMut<'a, T> {
     // Must be constructed by PySharedState after checking its leak_count.
     // Otherwise, drop() would incorrectly update the state.
     fn new(
-        _py: Python<'a>,
+        py: Python<'a>,
         inner: RefMut<'a, T>,
         py_shared_state: &'a PySharedState,
     ) -> Self {
         Self {
+            py,
             inner,
             py_shared_state,
         }
@@ -239,10 +241,8 @@  impl<'a, T> std::ops::DerefMut for PyRef
 
 impl<'a, T> Drop for PyRefMut<'a, T> {
     fn drop(&mut self) {
-        let gil = Python::acquire_gil();
-        let py = gil.python();
         unsafe {
-            self.py_shared_state.decrease_leak_count(py, true);
+            self.py_shared_state.decrease_leak_count(self.py, true);
         }
     }
 }