Replace std::sync::LazyLock with common::lock::LazyLock (#7079)

This commit is contained in:
Jeong, YunWon
2026-02-11 16:09:42 +09:00
committed by GitHub
parent c06cf56c60
commit ccd3d4f964
23 changed files with 47 additions and 30 deletions

View File

@@ -10,12 +10,29 @@ cfg_if::cfg_if! {
if #[cfg(feature = "threading")] {
pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};
pub use std::sync::{LazyLock as Lazy, OnceLock as OnceCell};
pub use std::sync::{LazyLock, OnceLock as OnceCell};
pub use core::cell::LazyCell;
} else {
mod cell_lock;
pub use cell_lock::{RawCellMutex as RawMutex, RawCellRwLock as RawRwLock, SingleThreadId as RawThreadId};
pub use core::cell::{LazyCell as Lazy, OnceCell};
pub use core::cell::{LazyCell, OnceCell};
/// `core::cell::LazyCell` with `Sync` for use in `static` items.
/// SAFETY: Without threading, there can be no concurrent access.
pub struct LazyLock<T, F = fn() -> T>(core::cell::LazyCell<T, F>);
// SAFETY: Without threading, there can be no concurrent access.
unsafe impl<T, F> Sync for LazyLock<T, F> {}
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
pub const fn new(f: F) -> Self { Self(core::cell::LazyCell::new(f)) }
pub fn force(this: &Self) -> &T { core::cell::LazyCell::force(&this.0) }
}
impl<T, F: FnOnce() -> T> core::ops::Deref for LazyLock<T, F> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
}
}
}

View File

@@ -26,7 +26,7 @@ mod _contextvars {
};
use crossbeam_utils::atomic::AtomicCell;
use indexmap::IndexMap;
use std::sync::LazyLock;
use rustpython_common::lock::LazyLock;
// TODO: Real hamt implementation
type Hamt = IndexMap<PyRef<ContextVar>, PyObjectRef, ahash::RandomState>;

View File

@@ -16,9 +16,9 @@ mod _csv {
use csv_core::Terminator;
use itertools::{self, Itertools};
use parking_lot::Mutex;
use rustpython_common::lock::LazyLock;
use rustpython_vm::match_class;
use std::collections::HashMap;
use std::sync::LazyLock;
#[pyattr]
const QUOTE_MINIMAL: i32 = QuoteStyle::Minimal as i32;

View File

@@ -687,7 +687,7 @@ mod mmap {
impl AsSequence for PyMmap {
fn as_sequence() -> &'static PySequenceMethods {
use std::sync::LazyLock;
use rustpython_common::lock::LazyLock;
static AS_SEQUENCE: LazyLock<PySequenceMethods> = LazyLock::new(|| PySequenceMethods {
length: atomic_func!(|seq, _vm| Ok(PyMmap::sequence_downcast(seq).__len__())),
item: atomic_func!(|seq, i, vm| {

View File

@@ -26,7 +26,7 @@ cfg_if::cfg_if! {
pub(crate) use _ssl::module_def;
use openssl_probe::ProbeResult;
use std::sync::LazyLock;
use rustpython_common::lock::LazyLock;
// define our own copy of ProbeResult so we can handle the vendor case
// easily, without having to have a bunch of cfgs
@@ -95,7 +95,6 @@ mod _ssl {
fmt,
io::{Read, Write},
path::{Path, PathBuf},
sync::LazyLock,
time::Instant,
};
@@ -106,7 +105,7 @@ mod _ssl {
// if openssl is vendored, it doesn't know the locations
// of system certificates - cache the probe result now.
#[cfg(openssl_vendored)]
std::sync::LazyLock::force(&super::PROBE);
rustpython_common::lock::LazyLock::force(&super::PROBE);
__module_exec(vm, module);
Ok(())
@@ -569,7 +568,7 @@ mod _ssl {
// Get or create an ex_data index for SNI callback data
fn get_sni_ex_data_index() -> libc::c_int {
use std::sync::LazyLock;
use rustpython_common::lock::LazyLock;
static SNI_EX_DATA_IDX: LazyLock<libc::c_int> = LazyLock::new(|| unsafe {
sys::SSL_get_ex_new_index(
0,
@@ -613,7 +612,7 @@ mod _ssl {
// Get or create an ex_data index for msg_callback data
fn get_msg_callback_ex_data_index() -> libc::c_int {
use std::sync::LazyLock;
use rustpython_common::lock::LazyLock;
static MSG_CB_EX_DATA_IDX: LazyLock<libc::c_int> = LazyLock::new(|| unsafe {
sys::SSL_get_ex_new_index(
0,

View File

@@ -132,7 +132,8 @@ impl OidTable {
}
/// Global OID table
static OID_TABLE: std::sync::LazyLock<OidTable> = std::sync::LazyLock::new(OidTable::build);
static OID_TABLE: rustpython_common::lock::LazyLock<OidTable> =
rustpython_common::lock::LazyLock::new(OidTable::build);
/// Macro to define OID entry using oid-registry constant
macro_rules! oid_static {

View File

@@ -2,6 +2,7 @@ use super::{
PositionIterInternal, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
PyType, PyTypeRef, iter::builtins_iter,
};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
@@ -30,7 +31,6 @@ use crate::{
};
use bstr::ByteSlice;
use core::{mem::size_of, ops::Deref};
use std::sync::LazyLock;
#[pyclass(module = false, name = "bytes")]
#[derive(Clone, Debug)]

View File

@@ -2,6 +2,7 @@ use super::{
IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, PyMappingProxy, PySet,
PyStr, PyStrRef, PyTupleRef, PyType, PyTypeRef, set::PySetInner,
};
use crate::common::lock::LazyLock;
use crate::object::{Traverse, TraverseFn};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
@@ -26,7 +27,6 @@ use crate::{
};
use alloc::fmt;
use rustpython_common::lock::PyMutex;
use std::sync::LazyLock;
pub type DictContentType = dict_inner::Dict;

View File

@@ -1,5 +1,5 @@
// spell-checker:ignore iparam gaiterobject
use std::sync::LazyLock;
use crate::common::lock::LazyLock;
use super::type_;
use crate::{

View File

@@ -1,4 +1,5 @@
use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
@@ -13,7 +14,6 @@ use crate::{
PyComparisonOp, Representable,
},
};
use std::sync::LazyLock;
#[pyclass(module = false, name = "mappingproxy", traverse)]
#[derive(Debug)]

View File

@@ -2,6 +2,7 @@ use super::{
PositionIterInternal, PyBytes, PyBytesRef, PyGenericAlias, PyInt, PyListRef, PySlice, PyStr,
PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef, iter::builtins_iter,
};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
TryFromBorrowedObject, TryFromObject, VirtualMachine, atomic_func,
@@ -30,7 +31,6 @@ use core::{cmp::Ordering, fmt::Debug, mem::ManuallyDrop, ops::Range};
use crossbeam_utils::atomic::AtomicCell;
use itertools::Itertools;
use rustpython_common::lock::PyMutex;
use std::sync::LazyLock;
#[derive(FromArgs)]
pub struct PyMemoryViewNewArgs {

View File

@@ -2,6 +2,7 @@ use super::{
PyGenericAlias, PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter,
tuple::tuple_hash,
};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
VirtualMachine, atomic_func,
@@ -19,7 +20,6 @@ use crossbeam_utils::atomic::AtomicCell;
use malachite_bigint::{BigInt, Sign};
use num_integer::Integer;
use num_traits::{One, Signed, ToPrimitive, Zero};
use std::sync::LazyLock;
// Search flag passed to iter_search
enum SearchType {

View File

@@ -5,6 +5,7 @@ use super::{
IterStatus, PositionIterInternal, PyDict, PyDictRef, PyGenericAlias, PyTupleRef, PyType,
PyTypeRef, builtins_iter,
};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
atomic_func,
@@ -30,7 +31,6 @@ use rustpython_common::{
atomic::{Ordering, PyAtomic, Radium},
hash,
};
use std::sync::LazyLock;
pub type SetContentType = dict_inner::Dict<()>;

View File

@@ -6,6 +6,7 @@ use super::{
builtins_iter,
},
};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyExact, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
TryFromBorrowedObject, VirtualMachine,
@@ -42,7 +43,6 @@ use rustpython_common::{
str::DeduceStrKind,
wtf8::{CodePoint, Wtf8, Wtf8Buf, Wtf8Chunk},
};
use std::sync::LazyLock;
use unic_ucd_bidi::BidiClass;
use unic_ucd_category::GeneralCategory;
use unic_ucd_ident::{is_xid_continue, is_xid_start};

View File

@@ -1,4 +1,5 @@
use super::{PyStr, PyTupleRef, PyType};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
@@ -10,7 +11,6 @@ use crate::{
SelfIter,
},
};
use std::sync::LazyLock;
use super::interpolation::PyInterpolation;

View File

@@ -1,6 +1,7 @@
use super::{
PositionIterInternal, PyGenericAlias, PyStrRef, PyType, PyTypeRef, iter::builtins_iter,
};
use crate::common::lock::LazyLock;
use crate::common::{
hash::{PyHash, PyUHash},
lock::PyMutex,
@@ -25,7 +26,6 @@ use crate::{
vm::VirtualMachine,
};
use alloc::fmt;
use std::sync::LazyLock;
#[pyclass(module = false, name = "tuple", traverse = "manual")]
pub struct PyTuple<R = PyObjectRef> {

View File

@@ -1,4 +1,5 @@
use super::{genericalias, type_};
use crate::common::lock::LazyLock;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
@@ -12,7 +13,6 @@ use crate::{
types::{AsMapping, AsNumber, Comparable, GetAttr, Hashable, PyComparisonOp, Representable},
};
use alloc::fmt;
use std::sync::LazyLock;
const CLS_ATTRS: &[&str] = &["__module__"];

View File

@@ -1,4 +1,5 @@
use super::{PyStr, PyStrRef, PyType, PyTypeRef, PyWeak};
use crate::common::lock::LazyLock;
use crate::{
Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
class::PyClassImpl,
@@ -11,7 +12,6 @@ use crate::{
Iterable, PyComparisonOp, Representable, SetAttr,
},
};
use std::sync::LazyLock;
#[pyclass(module = false, name = "weakproxy", unhashable = true, traverse)]
#[derive(Debug)]

View File

@@ -445,7 +445,7 @@ impl Initializer for PyCArray {
impl AsSequence for PyCArray {
fn as_sequence() -> &'static PySequenceMethods {
use std::sync::LazyLock;
use crate::common::lock::LazyLock;
static AS_SEQUENCE: LazyLock<PySequenceMethods> = LazyLock::new(|| PySequenceMethods {
length: atomic_func!(|seq, _vm| {
let zelf = PyCArray::sequence_downcast(seq);
@@ -470,7 +470,7 @@ impl AsSequence for PyCArray {
impl AsMapping for PyCArray {
fn as_mapping() -> &'static PyMappingMethods {
use std::sync::LazyLock;
use crate::common::lock::LazyLock;
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
length: atomic_func!(|mapping, _vm| {
let zelf = PyCArray::mapping_downcast(mapping);

View File

@@ -790,7 +790,7 @@ impl AsNumber for PyCPointer {
impl AsMapping for PyCPointer {
fn as_mapping() -> &'static PyMappingMethods {
use std::sync::LazyLock;
use crate::common::lock::LazyLock;
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
subscript: atomic_func!(|mapping, needle, vm| {
let zelf = PyCPointer::mapping_downcast(mapping);

View File

@@ -836,8 +836,8 @@ mod _sre {
impl AsMapping for Match {
fn as_mapping() -> &'static PyMappingMethods {
static AS_MAPPING: std::sync::LazyLock<PyMappingMethods> =
std::sync::LazyLock::new(|| PyMappingMethods {
static AS_MAPPING: crate::common::lock::LazyLock<PyMappingMethods> =
crate::common::lock::LazyLock::new(|| PyMappingMethods {
subscript: atomic_func!(|mapping, needle, vm| {
Match::mapping_downcast(mapping)
.__getitem__(needle.to_owned(), vm)

View File

@@ -28,6 +28,7 @@ pub fn call_typing_func_object<'a>(
#[pymodule(name = "_typing", with(super::typevar::typevar))]
pub(crate) mod decl {
use crate::common::lock::LazyLock;
use crate::{
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
builtins::{PyGenericAlias, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef, type_},
@@ -35,7 +36,6 @@ pub(crate) mod decl {
protocol::{PyMappingMethods, PyNumberMethods},
types::{AsMapping, AsNumber, Constructor, Iterable, Representable},
};
use std::sync::LazyLock;
#[pyfunction]
pub(crate) fn _idfunc(args: FuncArgs, _vm: &VirtualMachine) -> PyObjectRef {

View File

@@ -1,3 +1,4 @@
use crate::common::lock::LazyLock;
use crate::{
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
builtins::{PyBaseExceptionRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef},
@@ -9,7 +10,6 @@ use crate::{
types::PyComparisonOp,
vm::Context,
};
use std::sync::LazyLock;
const DEFAULT_STRUCTSEQ_REDUCE: PyMethodDef = PyMethodDef::new_const(
"__reduce__",