mod common;
use std::ffi::{c_int, c_void};
use std::sync::mpsc;
use common::{context, global, settled_thread_count, sym, thread_count};
type PthreadSelf = unsafe extern "C" fn() -> usize;
type ErrnoLocation = unsafe extern "C" fn() -> *mut c_int;
type Close = unsafe extern "C" fn(c_int) -> c_int;
type Pipe = unsafe extern "C" fn(*mut [c_int; 2]) -> c_int;
type Read = unsafe extern "C" fn(c_int, *mut c_void, usize) -> isize;
type Write = unsafe extern "C" fn(c_int, *const c_void, usize) -> isize;
static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(());
const EBADF: c_int = 9;
const THREADS: usize = 8;
#[test]
fn host_threads_are_distinct_foreign_threads() {
let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
let ctx = context();
let libc = global(&ctx);
let (pthread_self, errno_location, close) = unsafe {
(
sym::<PthreadSelf>(&libc, c"pthread_self"),
sym::<ErrnoLocation>(&libc, c"__errno_location"),
sym::<Close>(&libc, c"close"),
)
};
let identity = move || unsafe { (pthread_self(), errno_location() as usize) };
let me = identity();
assert_eq!(me, identity(), "a thread keeps its foreign thread");
let baseline = thread_count();
let (seen_tx, seen_rx) = mpsc::channel();
std::thread::scope(|s| {
let (go_tx, go_rx) = mpsc::channel::<()>();
let seen = seen_tx.clone();
s.spawn(move || {
assert_eq!(unsafe { close(-1) }, -1);
seen.send(identity()).unwrap();
go_rx.recv().unwrap();
assert_eq!(unsafe { *errno_location() }, EBADF);
});
std::thread::scope(|inner| {
for _ in 1..THREADS {
let seen = seen_tx.clone();
inner.spawn(move || {
assert_eq!(unsafe { *errno_location() }, 0);
assert_eq!(unsafe { close(-1) }, -1);
seen.send(identity()).unwrap();
assert_eq!(identity(), identity());
});
}
});
go_tx.send(()).unwrap();
});
drop(seen_tx);
let mut all: Vec<(usize, usize)> = seen_rx.iter().collect();
assert_eq!(all.len(), THREADS);
all.push(me);
let distinct = |f: fn(&(usize, usize)) -> usize| {
let mut v: Vec<usize> = all.iter().map(f).collect();
v.sort_unstable();
v.dedup();
v.len()
};
assert!(distinct(|i| i.0) >= 2, "pthread_self shared: {all:x?}");
assert!(distinct(|i| i.1) >= 2, "errno shared: {all:x?}");
assert_ne!(all[0], me);
assert_eq!(settled_thread_count(baseline), baseline);
assert_eq!(unsafe { *errno_location() }, 0);
}
#[test]
fn foreign_calls_run_concurrently() {
let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
let ctx = context();
let libc = global(&ctx);
let (pipe, read, write, close) = unsafe {
(
sym::<Pipe>(&libc, c"pipe"),
sym::<Read>(&libc, c"read"),
sym::<Write>(&libc, c"write"),
sym::<Close>(&libc, c"close"),
)
};
let mut fds = [0; 2];
assert_eq!(unsafe { pipe(&mut fds) }, 0);
let [rd, wr] = fds;
std::thread::scope(|s| {
let (entering_tx, entering_rx) = mpsc::channel();
let reader = s.spawn(move || {
let mut byte = 0u8;
entering_tx.send(()).unwrap();
let n = unsafe { read(rd, (&raw mut byte).cast(), 1) };
(n, byte)
});
entering_rx.recv().unwrap();
std::thread::sleep(std::time::Duration::from_millis(50));
assert_eq!(unsafe { write(wr, c"x".as_ptr().cast(), 1) }, 1);
assert_eq!(reader.join().unwrap(), (1, b'x'));
});
unsafe {
close(rd);
close(wr);
}
}