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

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 12, 2019, 8 a.m.
Message ID <f95a0332aec4dace1579.1570867247@mimosa>
Download mbox | patch
Permalink /patch/42259/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 12, 2019, 8 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1569053101 -32400
#      Sat Sep 21 17:05:01 2019 +0900
# Node ID f95a0332aec4dace1579c44bbd73feca9f228fda
# Parent  c1cfc2cf51ecd02e523325bd5d49c09004591e7d
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);
         }
     }
 }