Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions crates/common/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/contextvars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
9 changes: 4 additions & 5 deletions crates/stdlib/src/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,7 +95,6 @@ mod _ssl {
fmt,
io::{Read, Write},
path::{Path, PathBuf},
sync::LazyLock,
time::Instant,
};

Expand All @@ -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(())
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/stdlib/src/ssl/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,7 +27,6 @@ use crate::{
};
use alloc::fmt;
use rustpython_common::lock::PyMutex;
use std::sync::LazyLock;

pub type DictContentType = dict_inner::Dict;

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/genericalias.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// spell-checker:ignore iparam gaiterobject
use std::sync::LazyLock;
use crate::common::lock::LazyLock;

use super::type_;
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,7 +14,6 @@ use crate::{
PyComparisonOp, Representable,
},
};
use std::sync::LazyLock;

#[pyclass(module = false, name = "mappingproxy", traverse)]
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +31,6 @@ use rustpython_common::{
atomic::{Ordering, PyAtomic, Radium},
hash,
};
use std::sync::LazyLock;

pub type SetContentType = dict_inner::Dict<()>;

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/template.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,7 +11,6 @@ use crate::{
SelfIter,
},
};
use std::sync::LazyLock;

use super::interpolation::PyInterpolation;

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/union.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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__"];

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/weakproxy.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,7 +12,6 @@ use crate::{
Iterable, PyComparisonOp, Representable, SetAttr,
},
};
use std::sync::LazyLock;

#[pyclass(module = false, name = "weakproxy", unhashable = true, traverse)]
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/ctypes/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/stdlib/sre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ 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_},
function::FuncArgs,
protocol::{PyMappingMethods, PyNumberMethods},
types::{AsMapping, AsNumber, Constructor, Iterable, Representable},
};
use std::sync::LazyLock;

#[pyfunction]
pub(crate) fn _idfunc(args: FuncArgs, _vm: &VirtualMachine) -> PyObjectRef {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/types/structseq.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -9,7 +10,6 @@ use crate::{
types::PyComparisonOp,
vm::Context,
};
use std::sync::LazyLock;

const DEFAULT_STRUCTSEQ_REDUCE: PyMethodDef = PyMethodDef::new_const(
"__reduce__",
Expand Down
Loading