Port test_pthread

This commit is contained in:
Johannes Altmanninger 2023-12-10 10:38:06 +01:00
parent daf96a35b5
commit afe9013b4c
3 changed files with 33 additions and 15 deletions

View File

@ -22,6 +22,8 @@ mod redirection;
mod screen;
mod string_escape;
#[cfg(test)]
mod threads;
#[cfg(test)]
mod tokenizer;
mod topic_monitor;
mod wgetopt;

View File

@ -0,0 +1,31 @@
use crate::threads::spawn;
use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::{Arc, Condvar, Mutex};
#[test]
fn test_pthread() {
struct Context {
val: AtomicI32,
condvar: Condvar,
}
let ctx = Arc::new(Context {
val: AtomicI32::new(3),
condvar: Condvar::new(),
});
let mutex = Mutex::new(());
let ctx2 = ctx.clone();
let made = spawn(move || {
ctx2.val.fetch_add(2, Ordering::Release);
ctx2.condvar.notify_one();
});
assert!(made);
let mut lock = mutex.lock().unwrap();
loop {
lock = ctx.condvar.wait(lock).unwrap();
let v = ctx.val.load(Ordering::Acquire);
if v == 5 {
return;
}
println!("test_pthread: value did not yet reach goal")
}
}

View File

@ -432,20 +432,6 @@ static void test_iothread() {
}
}
// todo!("port this");
static void test_pthread() {
say(L"Testing pthreads");
std::atomic<int> val{3};
std::promise<void> promise;
bool made = make_detached_pthread([&]() {
val = val + 2;
promise.set_value();
});
do_test(made);
promise.get_future().wait();
do_test(val == 5);
}
static void test_const_strlen() {
do_test(const_strlen("") == 0);
do_test(const_strlen(L"") == 0);
@ -1186,7 +1172,6 @@ static const test_t s_tests[]{
{TEST_GROUP("convert"), test_convert_private_use},
{TEST_GROUP("convert_ascii"), test_convert_ascii},
{TEST_GROUP("iothread"), test_iothread},
{TEST_GROUP("pthread"), test_pthread},
{TEST_GROUP("lru"), test_lru},
{TEST_GROUP("word_motion"), test_word_motion},
{TEST_GROUP("colors"), test_colors},