// PipelineWorkers (runtime/src/pipeline_workers.h): every job runs once and before run() returns, on the workers and on // the caller; with no workers, after stop(), with several callers at once and when jobs throw. CPU only, no Vulkan. #include "../src/pipeline_workers.h" #include #include #include #include #include #include #include #include #include namespace { using leonida::PipelineWorkers; using namespace std::chrono_literals; std::atomic failures = 0; void check(bool ok, const char* what) { if (ok) return; std::printf("FAIL: %s\n", what); ++failures; } // Waits until `ready`, at most 10 s: a job that never comes fails the test instead of hanging it. template bool wait_for(Ready ready) { const auto end = std::chrono::steady_clock::now() + 10s; while (!ready()) { if (std::chrono::steady_clock::now() > end) return false; std::this_thread::sleep_for(1ms); } return true; } // Time spent in a job. Not a sleep: those end on the system's timer tick, up to 16 ms later. void spend(std::chrono::microseconds time) { const auto end = std::chrono::steady_clock::now() + time; while (std::chrono::steady_clock::now() < end) std::this_thread::yield(); } // No workers, or one job: on the calling thread, in order, and no thread is started. void inline_runs() { for (const unsigned threads : {0u, 4u}) { PipelineWorkers pool(threads); std::vector order; std::set ids; const auto job = [&](std::size_t i) { order.push_back(i); ids.insert(std::this_thread::get_id()); }; pool.run(0, job); check(order.empty(), "run(0) called the job"); pool.run(1, job); if (!threads) pool.run(5, job); check(order == (threads ? std::vector{0} : std::vector{0, 0, 1, 2, 3, 4}), "inline jobs out of order, missing or repeated"); check(ids == std::set{std::this_thread::get_id()}, "an inline job ran on another thread"); check(pool.stats().batches == 0 && pool.stats().started == 0, "an inline run was a batch or started a worker"); check(pool.threads() == threads, "threads()"); } PipelineWorkers pool(0); int ran = 0; try { pool.run(3, [&](std::size_t i) { ++ran; if (i == 1) throw std::runtime_error("inline"); }); check(false, "an inline exception was lost"); } catch (const std::runtime_error&) { check(ran == 2, "inline jobs went on after an exception"); } } // Each job once, all of them done when run() returns. Run often: a lost job or an early return shows up as a race. void each_once() { PipelineWorkers pool(4); for (int round = 0; round < 500; ++round) { const std::size_t count = 2 + std::size_t(round % 37); std::vector> calls(count); pool.run(count, [&](std::size_t i) { if (round % 8 == 0) spend(50us); calls[i].fetch_add(1); }); for (auto& c : calls) check(c == 1, "a job ran zero or several times, or after run() returned"); } const auto stats = pool.stats(); check(stats.batches == 500 && stats.threads == 4 && stats.started == 4 && stats.running == 0, "stats after the runs"); PipelineWorkers small(4); small.run(3, [](std::size_t) {}); check(small.stats().started == 2, "a batch of three starts two workers beside its caller"); } // The last job is still running when all the others are done: run() has to wait for it. void waits_for_the_last() { PipelineWorkers pool(3); for (int round = 0; round < 20; ++round) { std::atomic finished = 0; std::atomic worker_has_one = false; const auto caller = std::this_thread::get_id(); pool.run(8, [&](std::size_t) { if (std::this_thread::get_id() != caller) { worker_has_one = true; std::this_thread::sleep_for(20ms); } else { wait_for([&] { return worker_has_one.load(); }); } ++finished; }); check(finished == 8, "run() returned while a worker was in a job"); } } // The jobs do run at the same time, on the workers and the caller together, and never on more threads than that. void in_parallel() { PipelineWorkers pool(3); std::atomic inside = 0, most = 0; std::mutex mutex; std::set ids; pool.run(4, [&](std::size_t) { most = std::max(most.load(), ++inside); check(wait_for([&] { return inside.load() == 4; }), "four jobs, three workers and the caller did not meet"); most = std::max(most.load(), inside.load()); std::lock_guard lock(mutex); ids.insert(std::this_thread::get_id()); }); check(ids.size() == 4 && ids.count(std::this_thread::get_id()) == 1, "the caller and each worker took one job"); ids.clear(); inside = most = 0; pool.run(64, [&](std::size_t) { const int now = ++inside; int seen = most; while (now > seen && !most.compare_exchange_weak(seen, now)) {} spend(1ms); --inside; }); check(most >= 2 && most <= 4, "more jobs at once than workers and caller, or no two at once"); } void exceptions() { PipelineWorkers pool(4); std::atomic ran = 0; for (int round = 0; round < 50; ++round) { ran = 0; std::string what; try { pool.run(16, [&](std::size_t i) { ++ran; if (i == 3 || i == 11) { // the later one first: the lowest is still the one rethrown if (i == 3) spend(2ms); throw std::runtime_error(std::to_string(i)); } }); } catch (const std::runtime_error& error) { what = error.what(); } check(what == "3", "run() rethrew no exception, or not the lowest job's"); check(ran == 16, "an exception kept other jobs from running"); } int after = 0; pool.run(1, [&](std::size_t) { ++after; }); std::atomic more = 0; pool.run(8, [&](std::size_t) { ++more; }); check(after == 1 && more == 8, "the pool does not work after an exception"); } // Several threads submit at once (several queues): each gets its own batch done, and no batch waits for another to end. void several_callers() { PipelineWorkers pool(2); std::atomic wrong = 0; std::vector callers; for (int c = 0; c < 6; ++c) callers.emplace_back([&] { for (int round = 0; round < 100; ++round) { std::vector> calls(5); pool.run(calls.size(), [&](std::size_t i) { ++calls[i]; }); for (auto& n : calls) wrong += n != 1; } }); for (auto& c : callers) c.join(); check(wrong == 0, "a concurrent run() lost or repeated a job"); check(pool.stats().running == 0, "jobs still counted as running"); // One batch whose jobs hold every worker; another caller still gets its batch done. std::atomic release = false; std::atomic held = 0; std::thread blocked([&] { pool.run(3, [&](std::size_t) { ++held; wait_for([&] { return release.load(); }); }); }); check(wait_for([&] { return held == 3; }), "the first batch did not take both workers and its caller"); std::atomic done = 0; pool.run(4, [&](std::size_t) { ++done; }); check(done == 4, "a batch did not complete while the workers were busy"); release = true; blocked.join(); } // A job that itself submits a batch (a pipeline that needs another): the caller's own work keeps it from waiting forever. void nested() { PipelineWorkers pool(2); std::atomic inner = 0; pool.run(6, [&](std::size_t) { pool.run(6, [&](std::size_t) { ++inner; }); }); check(inner == 36, "nested runs"); } // stop() during a run(): the jobs nobody took yet are the caller's, none is lost, and stop() waits for the workers. void stopped_while_queued() { for (int round = 0; round < 30; ++round) { PipelineWorkers pool(3); std::vector> calls(40); std::atomic started = false, stopped = false; std::atomic in_job = 0; std::thread runner([&] { const auto self = std::this_thread::get_id(); pool.run(calls.size(), [&](std::size_t i) { started = true; if (std::this_thread::get_id() != self) ++in_job; spend(500us); ++calls[i]; if (std::this_thread::get_id() != self) --in_job; }); }); wait_for([&] { return started.load(); }); pool.stop(); stopped = true; check(in_job == 0, "stop() returned while a worker was in a job"); runner.join(); for (auto& c : calls) check(c == 1, "a queued job was lost or repeated by stop()"); // And later batches still complete, on this thread alone, with no worker started for them. const std::size_t workers = pool.stats().started; std::set ids; int count = 0; pool.run(5, [&](std::size_t) { ++count; ids.insert(std::this_thread::get_id()); }); check(count == 5 && ids == std::set{std::this_thread::get_id()} && pool.stats().started == workers, "a run() after stop()"); pool.stop(); // twice is fine } } // The destructor stops too, with the workers idle or never started. void destroyed() { for (int i = 0; i < 50; ++i) { PipelineWorkers pool(4); std::atomic n = 0; if (i & 1) pool.run(6, [&](std::size_t) { ++n; }); check(n == (i & 1 ? 6 : 0), "jobs before the destructor"); } } void setting() { using leonida::pipeline_threads; check(pipeline_threads(nullptr, 16).count == 8 && pipeline_threads(nullptr, 16).valid, "unset on 16 processors"); check(pipeline_threads("", 32).count == 8, "empty on 32 processors"); check(pipeline_threads(nullptr, 6).count == 3 && pipeline_threads(nullptr, 1).count == 0 && pipeline_threads(nullptr, 0).count == 0, "unset on few processors"); check(pipeline_threads("0", 16).count == 0 && pipeline_threads("0", 16).valid, "0 turns it off"); check(pipeline_threads("3", 16).count == 3, "a number"); check(pipeline_threads("64", 16).count == 64 && pipeline_threads("65", 16).count == 64 && pipeline_threads("99999999999999999999", 16).count == 64, "at most kMaxThreads, without wrapping"); for (const char* text : {"-1", "two", "4x", " 4", "0x4"}) check(!pipeline_threads(text, 16).valid && pipeline_threads(text, 16).count == 8, "not a number counts as unset"); } } // namespace int main() { inline_runs(); each_once(); waits_for_the_last(); in_parallel(); exceptions(); several_callers(); nested(); stopped_while_queued(); destroyed(); setting(); if (failures) return 1; std::printf("pipeline_workers_test: passed\n"); return 0; }