From afe9013b4c5cb5f08377e7917bf3ec6e6c6b9a0d Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 10 Dec 2023 10:38:06 +0100 Subject: [PATCH] Port test_pthread --- fish-rust/src/tests/mod.rs | 2 ++ fish-rust/src/tests/threads.rs | 31 +++++++++++++++++++++++++++++++ src/fish_tests.cpp | 15 --------------- 3 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 fish-rust/src/tests/threads.rs diff --git a/fish-rust/src/tests/mod.rs b/fish-rust/src/tests/mod.rs index dba612ccd..22e4b357e 100644 --- a/fish-rust/src/tests/mod.rs +++ b/fish-rust/src/tests/mod.rs @@ -22,6 +22,8 @@ mod redirection; mod screen; mod string_escape; #[cfg(test)] +mod threads; +#[cfg(test)] mod tokenizer; mod topic_monitor; mod wgetopt; diff --git a/fish-rust/src/tests/threads.rs b/fish-rust/src/tests/threads.rs new file mode 100644 index 000000000..d5a3ba644 --- /dev/null +++ b/fish-rust/src/tests/threads.rs @@ -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") + } +} diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index b46d4c253..4556930dd 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -432,20 +432,6 @@ static void test_iothread() { } } -// todo!("port this"); -static void test_pthread() { - say(L"Testing pthreads"); - std::atomic val{3}; - std::promise 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},