mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-01 22:27:30 +08:00
Port test_pthread
This commit is contained in:
parent
daf96a35b5
commit
afe9013b4c
|
@ -22,6 +22,8 @@ mod redirection;
|
|||
mod screen;
|
||||
mod string_escape;
|
||||
#[cfg(test)]
|
||||
mod threads;
|
||||
#[cfg(test)]
|
||||
mod tokenizer;
|
||||
mod topic_monitor;
|
||||
mod wgetopt;
|
||||
|
|
31
fish-rust/src/tests/threads.rs
Normal file
31
fish-rust/src/tests/threads.rs
Normal 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")
|
||||
}
|
||||
}
|
|
@ -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},
|
||||
|
|
Loading…
Reference in New Issue
Block a user