mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 17:38:37 +00:00
display_white_worker() waited out its second with the predicate "worker_stop_" only, so the notify effective_sdr_white_nits() sends on the first HDR10 present ended no wait, and the first Windows SDR white level was read up to a second late (found by djrolfes, #1608). A request sent before the worker reached its wait was lost the same way. The worker moves to leonida::DisplayWhiteCache (src/display_white_cache.h) with a read_now_ flag, set under the lock with the notify and checked by the wait, so the first get() is read at once. XboxPresentation holds one, made last in its constructor and dropped first in its destructor, as the thread was before. display_white_cache_test checks the first read with a one-hour period, the periodic reads, a failed read keeping the last value, and a prompt shutdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
3.4 KiB
C++
81 lines
3.4 KiB
C++
// DisplayWhiteCache (src/display_white_cache.h): XboxPresentation's Windows SDR white level, read on a worker thread.
|
|
// The first HDR10 present asks for the value, and the worker must read it then, not when its once-a-period wait runs
|
|
// out (djrolfes' finding in #1608: the wake-up was notified but the wait's predicate ignored it, so the first read
|
|
// came up to a second late). No window, no GPU: the read is a counter.
|
|
#include "../src/display_white_cache.h"
|
|
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <thread>
|
|
|
|
namespace {
|
|
|
|
bool check(bool condition, const char* description) {
|
|
if (!condition) std::fprintf(stderr, "FAIL: %s\n", description);
|
|
return condition;
|
|
}
|
|
|
|
// Polls `done` for up to `limit`.
|
|
template <class F>
|
|
bool within(std::chrono::milliseconds limit, F done) {
|
|
const auto end = std::chrono::steady_clock::now() + limit;
|
|
while (!done()) {
|
|
if (std::chrono::steady_clock::now() >= end) return false;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
using namespace std::chrono_literals;
|
|
bool ok = true;
|
|
|
|
// A period far longer than the test: only the first get()'s wake-up can bring the first read.
|
|
{
|
|
std::atomic<float> windows{240.0f};
|
|
leonida::DisplayWhiteCache cache([&] { return windows.load(); }, std::chrono::hours(1));
|
|
std::this_thread::sleep_for(20ms); // the worker is in its wait by now
|
|
ok &= check(cache.reads() == 0, "nothing is read before the first HDR10 present asks");
|
|
ok &= check(cache.get() == 0.0f, "the first get() has no value yet (the caller falls back to 203)");
|
|
ok &= check(within(2s, [&] { return cache.reads() == 1; }), "the first get() wakes the worker to read now");
|
|
ok &= check(within(2s, [&] { return cache.get() == 240.0f; }), "and the value it read is cached");
|
|
windows = 300.0f;
|
|
std::this_thread::sleep_for(50ms);
|
|
ok &= check(cache.reads() == 1 && cache.get() == 240.0f, "later get()s do not read again before the period");
|
|
}
|
|
|
|
// get() before the worker reaches its wait: the request is not lost.
|
|
for (int i = 0; i < 50; ++i) {
|
|
leonida::DisplayWhiteCache cache([] { return 203.0f; }, std::chrono::hours(1));
|
|
cache.get();
|
|
if (!check(within(2s, [&] { return cache.reads() == 1; }), "a get() right after construction is read")) {
|
|
ok = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Once wanted, the worker reads once a period, and a failed read keeps the last good value.
|
|
{
|
|
std::atomic<int> calls{0};
|
|
leonida::DisplayWhiteCache cache([&] { return ++calls == 1 ? 180.0f : 0.0f; }, 20ms);
|
|
cache.get();
|
|
ok &= check(within(2s, [&] { return cache.reads() >= 4; }), "reads repeat about once a period");
|
|
ok &= check(cache.get() == 180.0f, "a failed read keeps the last good value");
|
|
}
|
|
|
|
// Destruction stops a worker that waits out a long period at once.
|
|
{
|
|
const auto start = std::chrono::steady_clock::now();
|
|
{
|
|
leonida::DisplayWhiteCache cache([] { return 1.0f; }, std::chrono::hours(1));
|
|
cache.get();
|
|
within(2s, [&] { return cache.reads() == 1; });
|
|
}
|
|
ok &= check(std::chrono::steady_clock::now() - start < 2s, "destruction wakes and joins the worker");
|
|
}
|
|
if (ok) std::printf("display_white_cache_test: PASS\n");
|
|
return ok ? 0 : 1;
|
|
}
|