From e35c7f0b9082569b3c1a672edb352bc2cf2b3f58 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 24 Feb 2025 18:07:36 +0100 Subject: [PATCH] Update pool test. --- test/unit/stdlib/threads/pool.c3 | 44 ++++++------------------------- test/unit7/stdlib/threads/pool.c3 | 42 +++++------------------------ 2 files changed, 15 insertions(+), 71 deletions(-) diff --git a/test/unit/stdlib/threads/pool.c3 b/test/unit/stdlib/threads/pool.c3 index decd54044..f07140a62 100644 --- a/test/unit/stdlib/threads/pool.c3 +++ b/test/unit/stdlib/threads/pool.c3 @@ -1,9 +1,7 @@ module thread_pool_test; -import std::io; -import std::thread; -import std::thread::pool; +import std::io, std::thread, std::time; -def Pool = ThreadPool(<4>); +def Pool = ThreadPool{4}; fn void init_destroy() @test { @@ -19,26 +17,14 @@ fn void push_destroy() @test { for FOO: (usz i = 0; i < 20; i++) { - x = 0; + @atomic_store(x, 0); int y = 20; Pool pool; pool.init()!!; defer pool.destroy()!!; - work_done.lock()!!; pool.push(&do_work, &y)!!; - work_done.unlock()!!; - for (int j = 0; j < 1000; j++) - { - work_done.lock()!!; - if (@atomic_load(x) == @atomic_load(y)) - { - (void)work_done.unlock(); - break FOO; - } - (void)work_done.unlock(); - thread::yield(); - } - assert(false, "y never changed"); + thread::sleep(time::ms(50)); + test::eq(@atomic_load(x), @atomic_load(y)); } } @@ -46,34 +32,20 @@ fn void push_stop() @test { for (usz i = 0; i < 20; i++) { - x = 0; + @atomic_store(x, 0); int y = 20; Pool pool; pool.init()!!; - work_done.lock()!!; pool.push(&do_work, &y)!!; - work_done.unlock()!!; pool.stop_and_destroy()!!; - assert(x == y, "%d: %d != %d", i, x, y); + test::eq(@atomic_load(x), @atomic_load(y)); } } int x; -Mutex work_done; - -fn void startup() @init { - work_done.init()!!; -} - -fn void shutdown() @finalizer { - (void)work_done.destroy(); -} - fn int do_work(void* arg) { - work_done.lock()!!; - x = *(int*)arg; - work_done.unlock()!!; + @atomic_store(x, @atomic_load(*(int*)arg)); return 0; } \ No newline at end of file diff --git a/test/unit7/stdlib/threads/pool.c3 b/test/unit7/stdlib/threads/pool.c3 index 28380f7b5..f07140a62 100644 --- a/test/unit7/stdlib/threads/pool.c3 +++ b/test/unit7/stdlib/threads/pool.c3 @@ -1,7 +1,5 @@ module thread_pool_test; -import std::io; -import std::thread; -import std::thread::pool; +import std::io, std::thread, std::time; def Pool = ThreadPool{4}; @@ -19,26 +17,14 @@ fn void push_destroy() @test { for FOO: (usz i = 0; i < 20; i++) { - x = 0; + @atomic_store(x, 0); int y = 20; Pool pool; pool.init()!!; defer pool.destroy()!!; - work_done.lock()!!; pool.push(&do_work, &y)!!; - work_done.unlock()!!; - for (int j = 0; j < 1000; j++) - { - work_done.lock()!!; - if (@atomic_load(x) == @atomic_load(y)) - { - (void)work_done.unlock(); - break FOO; - } - (void)work_done.unlock(); - thread::yield(); - } - assert(false, "y never changed"); + thread::sleep(time::ms(50)); + test::eq(@atomic_load(x), @atomic_load(y)); } } @@ -46,34 +32,20 @@ fn void push_stop() @test { for (usz i = 0; i < 20; i++) { - x = 0; + @atomic_store(x, 0); int y = 20; Pool pool; pool.init()!!; - work_done.lock()!!; pool.push(&do_work, &y)!!; - work_done.unlock()!!; pool.stop_and_destroy()!!; - assert(x == y, "%d: %d != %d", i, x, y); + test::eq(@atomic_load(x), @atomic_load(y)); } } int x; -Mutex work_done; - -fn void startup() @init { - work_done.init()!!; -} - -fn void shutdown() @finalizer { - (void)work_done.destroy(); -} - fn int do_work(void* arg) { - work_done.lock()!!; - x = *(int*)arg; - work_done.unlock()!!; + @atomic_store(x, @atomic_load(*(int*)arg)); return 0; } \ No newline at end of file