mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Update pool test.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user