Update pool test.

This commit is contained in:
Christoffer Lerno
2025-02-24 18:07:36 +01:00
parent 7083b2b8e5
commit e35c7f0b90
2 changed files with 15 additions and 71 deletions

View File

@@ -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;
}

View File

@@ -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;
}