mod common;
use std::cell::Cell;
use std::ffi::{c_int, c_void};
use common::{context, global, sym};
type StartRoutine = unsafe extern "C" fn(*mut c_void) -> *mut c_void;
type PthreadCreate =
unsafe extern "C" fn(*mut usize, *const c_void, StartRoutine, *mut c_void) -> c_int;
type PthreadJoin = unsafe extern "C" fn(usize, *mut *mut c_void) -> c_int;
thread_local! {
static CALLS: Cell<usize> = const { Cell::new(0) };
}
unsafe extern "C" fn host_work(arg: *mut c_void) -> *mut c_void {
CALLS.with(|c| c.set(c.get() + 1));
let seed = arg as usize;
let text: String = (0..1000)
.map(|i| char::from(b'a' + ((seed + i) % 26) as u8))
.collect();
let summary = format!("{}:{}", CALLS.with(Cell::get), text.len());
Box::into_raw(Box::new(summary)).cast()
}
#[test]
#[ignore = "callbacks on foreign-created threads need a host TCB (open design question)"]
fn callback_on_foreign_thread() {
let ctx = context();
let libc = global(&ctx);
let (create, join) = unsafe {
(
sym::<PthreadCreate>(&libc, c"pthread_create"),
sym::<PthreadJoin>(&libc, c"pthread_join"),
)
};
let start = unsafe { ctx.callback::<StartRoutine>(host_work) }.expect("create callback");
let threads: Vec<usize> = (0..8)
.map(|seed| {
let mut tid = 0usize;
let rc = unsafe { create(&mut tid, std::ptr::null(), *start, seed as *mut c_void) };
assert_eq!(rc, 0, "pthread_create");
tid
})
.collect();
for tid in threads {
let mut ret = std::ptr::null_mut();
assert_eq!(unsafe { join(tid, &mut ret) }, 0, "pthread_join");
let summary = unsafe { Box::from_raw(ret.cast::<String>()) };
assert_eq!(*summary, "1:1000");
}
}