Skip to content

Commit ee6ebc3

Browse files
committed
src/impl_: drop gil_used_once special casing
This code was for user convenience when gil_used defaulted to true, but since we're changing that to false, it is no longer needed.
1 parent fc9dcee commit ee6ebc3

File tree

1 file changed

+1
-48
lines changed

1 file changed

+1
-48
lines changed

src/impl_/pymodule.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Implementation details of `#[pymodule]` which need to be accessible from proc-macro generated code.
22
3-
#[cfg(Py_3_13)]
4-
use std::sync::Once;
53
use std::{
64
cell::UnsafeCell,
75
ffi::CStr,
@@ -51,10 +49,6 @@ pub struct ModuleDef {
5149
module: PyOnceLock<Py<PyModule>>,
5250
/// Whether or not the module supports running without the GIL
5351
gil_used: bool,
54-
/// Guard to allow updating the `Py_mod_gil` slot within `ffi_def`
55-
/// before first use.
56-
#[cfg(Py_3_13)]
57-
gil_used_once: Once,
5852
}
5953

6054
unsafe impl Sync for ModuleDef {}
@@ -102,21 +96,16 @@ impl ModuleDef {
10296
interpreter: AtomicI64::new(-1),
10397
module: PyOnceLock::new(),
10498
gil_used: true,
105-
#[cfg(Py_3_13)]
106-
gil_used_once: Once::new(),
10799
}
108100
}
109101

110102
pub fn init_multi_phase(&'static self, _py: Python<'_>, _gil_used: bool) -> *mut ffi::PyObject {
111-
// Once the ffi_def has been used, we can no longer modify, so we set the once.
112-
#[cfg(Py_3_13)]
113-
self.gil_used_once.call_once(|| {});
114103
unsafe { ffi::PyModuleDef_Init(self.ffi_def.get()) }
115104
}
116105

117106
/// Builds a module object directly. Used for [`#[pymodule]`][crate::pymodule] submodules.
118107
#[cfg_attr(any(Py_LIMITED_API, not(Py_GIL_DISABLED)), allow(unused_variables))]
119-
pub fn make_module(&'static self, py: Python<'_>, gil_used: bool) -> PyResult<Py<PyModule>> {
108+
pub fn make_module(&'static self, py: Python<'_>, _gil_used: bool) -> PyResult<Py<PyModule>> {
120109
// Check the interpreter ID has not changed, since we currently have no way to guarantee
121110
// that static data is not reused across interpreters.
122111
//
@@ -171,42 +160,6 @@ impl ModuleDef {
171160
kwargs.set_item("name", name)?;
172161
let spec = simple_ns.call((), Some(&kwargs))?;
173162

174-
// If the first time writing this ffi def, we can inherit `gil_used` from parent
175-
// modules.
176-
//
177-
// TODO: remove this once we default to `gil_used = false` (i.e. assume free-threading
178-
// support in extensions). This is purely a convenience so that users only need to
179-
// annotate the top-level module.
180-
//
181-
// SAFETY:
182-
// - ffi_def is known to be non-null
183-
// - we check slots is non-null
184-
// - it is valid for a slot to be zeroed
185-
// - we are writing to the slot under the protection of a `Once`.
186-
#[cfg(Py_3_13)]
187-
self.gil_used_once.call_once(|| {
188-
// SAFETY: ffi_def is non-null
189-
let slots = unsafe { (*ffi_def).m_slots };
190-
if !slots.is_null() {
191-
let mut slot_ptr = slots;
192-
// SAFETY: non-null slots pointer
193-
while unsafe { *slot_ptr != ZEROED_SLOT } {
194-
// SAFETY: no other accessors due to call_once guard
195-
let slot = unsafe { &mut *slot_ptr };
196-
if slot.slot == ffi::Py_mod_gil {
197-
slot.value = if gil_used {
198-
ffi::Py_MOD_GIL_USED
199-
} else {
200-
ffi::Py_MOD_GIL_NOT_USED
201-
};
202-
break;
203-
}
204-
// SAFETY: we have guaranteed there is a trailer zeroed slot
205-
slot_ptr = unsafe { slot_ptr.add(1) };
206-
}
207-
}
208-
});
209-
210163
self.module
211164
.get_or_try_init(py, || {
212165
let def = self.ffi_def.get();

0 commit comments

Comments
 (0)