Skip to content

Commit ccd3d4f

Browse files
authored
Replace std::sync::LazyLock with common::lock::LazyLock (#7079)
1 parent c06cf56 commit ccd3d4f

File tree

23 files changed

+47
-30
lines changed

23 files changed

+47
-30
lines changed

crates/common/src/lock.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,29 @@ cfg_if::cfg_if! {
1010
if #[cfg(feature = "threading")] {
1111
pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};
1212

13-
pub use std::sync::{LazyLock as Lazy, OnceLock as OnceCell};
13+
pub use std::sync::{LazyLock, OnceLock as OnceCell};
14+
pub use core::cell::LazyCell;
1415
} else {
1516
mod cell_lock;
1617
pub use cell_lock::{RawCellMutex as RawMutex, RawCellRwLock as RawRwLock, SingleThreadId as RawThreadId};
1718

18-
pub use core::cell::{LazyCell as Lazy, OnceCell};
19+
pub use core::cell::{LazyCell, OnceCell};
20+
21+
/// `core::cell::LazyCell` with `Sync` for use in `static` items.
22+
/// SAFETY: Without threading, there can be no concurrent access.
23+
pub struct LazyLock<T, F = fn() -> T>(core::cell::LazyCell<T, F>);
24+
// SAFETY: Without threading, there can be no concurrent access.
25+
unsafe impl<T, F> Sync for LazyLock<T, F> {}
26+
27+
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
28+
pub const fn new(f: F) -> Self { Self(core::cell::LazyCell::new(f)) }
29+
pub fn force(this: &Self) -> &T { core::cell::LazyCell::force(&this.0) }
30+
}
31+
32+
impl<T, F: FnOnce() -> T> core::ops::Deref for LazyLock<T, F> {
33+
type Target = T;
34+
fn deref(&self) -> &T { &self.0 }
35+
}
1936
}
2037
}
2138

crates/stdlib/src/contextvars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod _contextvars {
2626
};
2727
use crossbeam_utils::atomic::AtomicCell;
2828
use indexmap::IndexMap;
29-
use std::sync::LazyLock;
29+
use rustpython_common::lock::LazyLock;
3030

3131
// TODO: Real hamt implementation
3232
type Hamt = IndexMap<PyRef<ContextVar>, PyObjectRef, ahash::RandomState>;

crates/stdlib/src/csv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ mod _csv {
1616
use csv_core::Terminator;
1717
use itertools::{self, Itertools};
1818
use parking_lot::Mutex;
19+
use rustpython_common::lock::LazyLock;
1920
use rustpython_vm::match_class;
2021
use std::collections::HashMap;
21-
use std::sync::LazyLock;
2222

2323
#[pyattr]
2424
const QUOTE_MINIMAL: i32 = QuoteStyle::Minimal as i32;

crates/stdlib/src/mmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ mod mmap {
687687

688688
impl AsSequence for PyMmap {
689689
fn as_sequence() -> &'static PySequenceMethods {
690-
use std::sync::LazyLock;
690+
use rustpython_common::lock::LazyLock;
691691
static AS_SEQUENCE: LazyLock<PySequenceMethods> = LazyLock::new(|| PySequenceMethods {
692692
length: atomic_func!(|seq, _vm| Ok(PyMmap::sequence_downcast(seq).__len__())),
693693
item: atomic_func!(|seq, i, vm| {

crates/stdlib/src/openssl.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ cfg_if::cfg_if! {
2626
pub(crate) use _ssl::module_def;
2727

2828
use openssl_probe::ProbeResult;
29-
use std::sync::LazyLock;
29+
use rustpython_common::lock::LazyLock;
3030

3131
// define our own copy of ProbeResult so we can handle the vendor case
3232
// easily, without having to have a bunch of cfgs
@@ -95,7 +95,6 @@ mod _ssl {
9595
fmt,
9696
io::{Read, Write},
9797
path::{Path, PathBuf},
98-
sync::LazyLock,
9998
time::Instant,
10099
};
101100

@@ -106,7 +105,7 @@ mod _ssl {
106105
// if openssl is vendored, it doesn't know the locations
107106
// of system certificates - cache the probe result now.
108107
#[cfg(openssl_vendored)]
109-
std::sync::LazyLock::force(&super::PROBE);
108+
rustpython_common::lock::LazyLock::force(&super::PROBE);
110109

111110
__module_exec(vm, module);
112111
Ok(())
@@ -569,7 +568,7 @@ mod _ssl {
569568

570569
// Get or create an ex_data index for SNI callback data
571570
fn get_sni_ex_data_index() -> libc::c_int {
572-
use std::sync::LazyLock;
571+
use rustpython_common::lock::LazyLock;
573572
static SNI_EX_DATA_IDX: LazyLock<libc::c_int> = LazyLock::new(|| unsafe {
574573
sys::SSL_get_ex_new_index(
575574
0,
@@ -613,7 +612,7 @@ mod _ssl {
613612

614613
// Get or create an ex_data index for msg_callback data
615614
fn get_msg_callback_ex_data_index() -> libc::c_int {
616-
use std::sync::LazyLock;
615+
use rustpython_common::lock::LazyLock;
617616
static MSG_CB_EX_DATA_IDX: LazyLock<libc::c_int> = LazyLock::new(|| unsafe {
618617
sys::SSL_get_ex_new_index(
619618
0,

crates/stdlib/src/ssl/oid.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ impl OidTable {
132132
}
133133

134134
/// Global OID table
135-
static OID_TABLE: std::sync::LazyLock<OidTable> = std::sync::LazyLock::new(OidTable::build);
135+
static OID_TABLE: rustpython_common::lock::LazyLock<OidTable> =
136+
rustpython_common::lock::LazyLock::new(OidTable::build);
136137

137138
/// Macro to define OID entry using oid-registry constant
138139
macro_rules! oid_static {

crates/vm/src/builtins/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
PositionIterInternal, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
33
PyType, PyTypeRef, iter::builtins_iter,
44
};
5+
use crate::common::lock::LazyLock;
56
use crate::{
67
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
78
TryFromBorrowedObject, TryFromObject, VirtualMachine,
@@ -30,7 +31,6 @@ use crate::{
3031
};
3132
use bstr::ByteSlice;
3233
use core::{mem::size_of, ops::Deref};
33-
use std::sync::LazyLock;
3434

3535
#[pyclass(module = false, name = "bytes")]
3636
#[derive(Clone, Debug)]

crates/vm/src/builtins/dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, PyMappingProxy, PySet,
33
PyStr, PyStrRef, PyTupleRef, PyType, PyTypeRef, set::PySetInner,
44
};
5+
use crate::common::lock::LazyLock;
56
use crate::object::{Traverse, TraverseFn};
67
use crate::{
78
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
@@ -26,7 +27,6 @@ use crate::{
2627
};
2728
use alloc::fmt;
2829
use rustpython_common::lock::PyMutex;
29-
use std::sync::LazyLock;
3030

3131
pub type DictContentType = dict_inner::Dict;
3232

crates/vm/src/builtins/genericalias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// spell-checker:ignore iparam gaiterobject
2-
use std::sync::LazyLock;
2+
use crate::common::lock::LazyLock;
33

44
use super::type_;
55
use crate::{

crates/vm/src/builtins/mappingproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef};
2+
use crate::common::lock::LazyLock;
23
use crate::{
34
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
45
atomic_func,
@@ -13,7 +14,6 @@ use crate::{
1314
PyComparisonOp, Representable,
1415
},
1516
};
16-
use std::sync::LazyLock;
1717

1818
#[pyclass(module = false, name = "mappingproxy", traverse)]
1919
#[derive(Debug)]

0 commit comments

Comments
 (0)