Skip to content
Open
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
1 change: 0 additions & 1 deletion madsim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ macros = ["madsim-macros", "tokio/macros"]
bincode = { version = "1", optional = true }
bytes = "1"
futures-util = "0.3"
lazy_static = "1.4"
madsim-macros = { version = "0.2", path = "../madsim-macros", optional = true }
rand = "0.8"
serde = { version = "1", features = ["derive"] }
Expand Down
28 changes: 14 additions & 14 deletions madsim/src/sim/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,24 @@ unsafe extern "C" fn getrandom(buf: *mut u8, buflen: usize, _flags: u32) -> isiz
#[cfg(target_os = "linux")]
{
// not in madsim, call the original function.
lazy_static::lazy_static! {
static ref GETRANDOM: unsafe extern "C" fn(buf: *mut u8, buflen: usize, flags: u32) -> isize = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"getrandom".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
static GETRANDOM: std::sync::LazyLock<
unsafe extern "C" fn(buf: *mut u8, buflen: usize, flags: u32) -> isize,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"getrandom".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
GETRANDOM(buf, buflen, _flags)
}
#[cfg(target_os = "macos")]
{
lazy_static::lazy_static! {
static ref GETENTROPY: unsafe extern "C" fn(buf: *mut u8, buflen: usize) -> libc::c_int = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"getentropy".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
static GETENTROPY: std::sync::LazyLock<
unsafe extern "C" fn(buf: *mut u8, buflen: usize) -> libc::c_int,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"getentropy".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
match GETENTROPY(buf, buflen) {
-1 => -1,
0 => buflen as _,
Expand Down
51 changes: 25 additions & 26 deletions madsim/src/sim/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,17 +730,17 @@ unsafe extern "C" fn sched_getaffinity(
}
return 0;
}
lazy_static::lazy_static! {
static ref SCHED_GETAFFINITY: unsafe extern "C" fn(
static SCHED_GETAFFINITY: std::sync::LazyLock<
unsafe extern "C" fn(
pid: libc::pid_t,
cpusetsize: libc::size_t,
cpuset: *mut libc::cpu_set_t,
) -> libc::c_int = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"sched_getaffinity".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
) -> libc::c_int,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"sched_getaffinity".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
SCHED_GETAFFINITY(pid, cpusetsize, cpuset)
}

Expand All @@ -755,13 +755,12 @@ unsafe extern "C" fn sysconf(name: libc::c_int) -> libc::c_long {
return info.node.cores as _;
}
}
lazy_static::lazy_static! {
static ref SYSCONF: unsafe extern "C" fn(name: libc::c_int) -> libc::c_long = unsafe {
static SYSCONF: std::sync::LazyLock<unsafe extern "C" fn(name: libc::c_int) -> libc::c_long> =
std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"sysconf".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
});
SYSCONF(name)
}

Expand All @@ -781,13 +780,13 @@ unsafe extern "C" fn pthread_attr_init(attr: *mut libc::pthread_attr_t) -> libc:
return -1;
}
}
lazy_static::lazy_static! {
static ref PTHREAD_ATTR_INIT: unsafe extern "C" fn(attr: *mut libc::pthread_attr_t) -> libc::c_int = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"pthread_attr_init".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
static PTHREAD_ATTR_INIT: std::sync::LazyLock<
unsafe extern "C" fn(attr: *mut libc::pthread_attr_t) -> libc::c_int,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"pthread_attr_init".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
PTHREAD_ATTR_INIT(attr)
}

Expand Down Expand Up @@ -826,13 +825,13 @@ unsafe extern "C" fn gethostname(name: *mut libc::c_char, size: libc::size_t) ->
}
return 0;
}
lazy_static::lazy_static! {
static ref GETHOSTNAME: unsafe extern "C" fn(name: *mut libc::c_char, size: libc::size_t) -> libc::c_int = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"gethostname".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
static GETHOSTNAME: std::sync::LazyLock<
unsafe extern "C" fn(name: *mut libc::c_char, size: libc::size_t) -> libc::c_int,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"gethostname".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
GETHOSTNAME(name, size)
}

Expand Down
41 changes: 17 additions & 24 deletions madsim/src/sim/time/system_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ unsafe extern "C" fn gettimeofday(tp: *mut libc::timeval, tz: *mut libc::c_void)
0
} else {
// not in madsim, call the original function.
lazy_static::lazy_static! {
static ref GETTIMEOFDAY: unsafe extern "C" fn(
tp: *mut libc::timeval,
tz: *mut libc::c_void,
) -> libc::c_int = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"gettimeofday".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
static GETTIMEOFDAY: std::sync::LazyLock<
unsafe extern "C" fn(tp: *mut libc::timeval, tz: *mut libc::c_void) -> libc::c_int,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"gettimeofday".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
GETTIMEOFDAY(tp, tz)
}
}
Expand Down Expand Up @@ -73,16 +70,13 @@ unsafe extern "C" fn clock_gettime(
});
0
} else {
lazy_static::lazy_static! {
static ref CLOCK_GETTIME: unsafe extern "C" fn(
clockid: libc::clockid_t,
tp: *mut libc::timespec,
) -> libc::c_int = unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"clock_gettime".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
static CLOCK_GETTIME: std::sync::LazyLock<
unsafe extern "C" fn(clockid: libc::clockid_t, tp: *mut libc::timespec) -> libc::c_int,
> = std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"clock_gettime".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
});
CLOCK_GETTIME(clockid, tp)
}
}
Expand All @@ -101,13 +95,12 @@ extern "C" fn mach_absolute_time() -> u64 {
let instant = time.now_instant();
unsafe { std::mem::transmute(instant) }
} else {
lazy_static::lazy_static! {
static ref MACH_ABSOLUTE_TIME: extern "C" fn() -> u64 = unsafe {
static MACH_ABSOLUTE_TIME: std::sync::LazyLock<extern "C" fn() -> u64> =
std::sync::LazyLock::new(|| unsafe {
let ptr = libc::dlsym(libc::RTLD_NEXT, c"mach_absolute_time".as_ptr() as _);
assert!(!ptr.is_null());
std::mem::transmute(ptr)
};
}
});
MACH_ABSOLUTE_TIME()
}
}
Expand Down
5 changes: 2 additions & 3 deletions madsim/src/std/net/ucx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use std::{
use tokio::sync::{mpsc, oneshot};
use tracing::*;

lazy_static::lazy_static! {
static ref CONTEXT: Arc<ucp::Context> = ucp::Context::new().expect("failed to initialize UCX context");
}
static CONTEXT: std::sync::LazyLock<Arc<ucp::Context>> =
std::sync::LazyLock::new(|| ucp::Context::new().expect("failed to initialize UCX context"));

/// An endpoint.
#[derive(Clone)]
Expand Down