From 11ec156b64a59cd1e73c1a1c795498c5c790bd38 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 20 Apr 2024 15:39:36 +0100 Subject: [PATCH] Codechange: Add a priority field to TimerGameTick::TPeriod Use this as the primary sort key for TimerGameTick::TPeriod, to avoid container sort order changes on timer period saveload. See: #12509 --- src/company_cmd.cpp | 4 ++-- src/newgrf_profiling.cpp | 4 ++-- src/saveload/afterload.cpp | 2 +- src/saveload/misc_sl.cpp | 6 +++--- src/saveload/oldloader_sl.cpp | 2 +- src/timer/timer_game_tick.cpp | 21 +++++++++++++++------ src/timer/timer_game_tick.h | 29 ++++++++++++++++++++++++++++- src/timetable_gui.cpp | 2 +- 8 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index f88ad63538..6fa44f4bc5 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -637,7 +637,7 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY) } /** Start a new competitor company if possible. */ -TimeoutTimer _new_competitor_timeout(0, []() { +TimeoutTimer _new_competitor_timeout({ TimerGameTick::Priority::COMPETITOR_TIMEOUT, 0 }, []() { if (_game_mode == GM_MENU || !AI::CanStartNew()) return; if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return; @@ -778,7 +778,7 @@ void OnTick_Companies() /* Randomize a bit when the AI is actually going to start; ranges from 87.5% .. 112.5% of indicated value. */ timeout += ScriptObject::GetRandomizer(OWNER_NONE).Next(timeout / 4) - timeout / 8; - _new_competitor_timeout.Reset(std::max(1, timeout)); + _new_competitor_timeout.Reset({ TimerGameTick::Priority::COMPETITOR_TIMEOUT, static_cast(std::max(1, timeout)) }); } _cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES; diff --git a/src/newgrf_profiling.cpp b/src/newgrf_profiling.cpp index 115dd88b2d..48ed9cdab0 100644 --- a/src/newgrf_profiling.cpp +++ b/src/newgrf_profiling.cpp @@ -157,7 +157,7 @@ std::string NewGRFProfiler::GetOutputFilename() const /** * Check whether profiling is active and should be finished. */ -static TimeoutTimer _profiling_finish_timeout(0, []() +static TimeoutTimer _profiling_finish_timeout({ TimerGameTick::Priority::NONE, 0 }, []() { NewGRFProfiler::FinishAll(); }); @@ -167,7 +167,7 @@ static TimeoutTimer _profiling_finish_timeout(0, []() */ /* static */ void NewGRFProfiler::StartTimer(uint64_t ticks) { - _profiling_finish_timeout.Reset(ticks); + _profiling_finish_timeout.Reset({ TimerGameTick::Priority::NONE, static_cast(ticks) }); } /** diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 43f9c79253..a1d016a1f1 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -3255,7 +3255,7 @@ bool AfterLoadGame() /* We did load the "period" of the timer, but not the fired/elapsed. We can deduce that here. */ extern TimeoutTimer _new_competitor_timeout; _new_competitor_timeout.storage.elapsed = 0; - _new_competitor_timeout.fired = _new_competitor_timeout.period == 0; + _new_competitor_timeout.fired = _new_competitor_timeout.period.value == 0; } if (IsSavegameVersionBefore(SLV_NEWGRF_LAST_SERVICE)) { diff --git a/src/saveload/misc_sl.cpp b/src/saveload/misc_sl.cpp index 3cc21c083f..77c6b8cc36 100644 --- a/src/saveload/misc_sl.cpp +++ b/src/saveload/misc_sl.cpp @@ -99,9 +99,9 @@ static const SaveLoad _date_desc[] = { SLEG_CONDVAR("pause_mode", _pause_mode, SLE_UINT8, SLV_4, SL_MAX_VERSION), SLEG_CONDSSTR("id", _game_session_stats.savegame_id, SLE_STR, SLV_SAVEGAME_ID, SL_MAX_VERSION), /* For older savegames, we load the current value as the "period"; afterload will set the "fired" and "elapsed". */ - SLEG_CONDVAR("next_competitor_start", _new_competitor_timeout.period, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_109), - SLEG_CONDVAR("next_competitor_start", _new_competitor_timeout.period, SLE_UINT32, SLV_109, SLV_AI_START_DATE), - SLEG_CONDVAR("competitors_interval", _new_competitor_timeout.period, SLE_UINT32, SLV_AI_START_DATE, SL_MAX_VERSION), + SLEG_CONDVAR("next_competitor_start", _new_competitor_timeout.period.value, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_109), + SLEG_CONDVAR("next_competitor_start", _new_competitor_timeout.period.value, SLE_UINT32, SLV_109, SLV_AI_START_DATE), + SLEG_CONDVAR("competitors_interval", _new_competitor_timeout.period.value, SLE_UINT32, SLV_AI_START_DATE, SL_MAX_VERSION), SLEG_CONDVAR("competitors_interval_elapsed", _new_competitor_timeout.storage.elapsed, SLE_UINT32, SLV_AI_START_DATE, SL_MAX_VERSION), SLEG_CONDVAR("competitors_interval_fired", _new_competitor_timeout.fired, SLE_BOOL, SLV_AI_START_DATE, SL_MAX_VERSION), }; diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 7beb30a490..538c1336c3 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1701,7 +1701,7 @@ static const OldChunks main_chunk[] = { OCL_ASSERT( OC_TTO, 0x496CE ), - OCL_VAR ( OC_FILE_U16 | OC_VAR_U32, 1, &_new_competitor_timeout.period ), + OCL_VAR ( OC_FILE_U16 | OC_VAR_U32, 1, &_new_competitor_timeout.period.value ), OCL_CNULL( OC_TTO, 2 ), ///< available monorail bitmask diff --git a/src/timer/timer_game_tick.cpp b/src/timer/timer_game_tick.cpp index 92399a4e63..d86992209c 100644 --- a/src/timer/timer_game_tick.cpp +++ b/src/timer/timer_game_tick.cpp @@ -21,13 +21,13 @@ TimerGameTick::TickCounter TimerGameTick::counter = 0; template<> void IntervalTimer::Elapsed(TimerGameTick::TElapsed delta) { - if (this->period == 0) return; + if (this->period.value == 0) return; this->storage.elapsed += delta; uint count = 0; - while (this->storage.elapsed >= this->period) { - this->storage.elapsed -= this->period; + while (this->storage.elapsed >= this->period.value) { + this->storage.elapsed -= this->period.value; count++; } @@ -40,11 +40,11 @@ template<> void TimeoutTimer::Elapsed(TimerGameTick::TElapsed delta) { if (this->fired) return; - if (this->period == 0) return; + if (this->period.value == 0) return; this->storage.elapsed += delta; - if (this->storage.elapsed >= this->period) { + if (this->storage.elapsed >= this->period.value) { this->callback(); this->fired = true; } @@ -64,7 +64,16 @@ bool TimerManager::Elapsed(TimerGameTick::TElapsed delta) #ifdef WITH_ASSERT template<> -void TimerManager::Validate(TimerGameTick::TPeriod) +void TimerManager::Validate(TimerGameTick::TPeriod period) { + if (period.priority == TimerGameTick::Priority::NONE) return; + + /* Validate we didn't make a developer error and scheduled more than one + * entry on the same priority. There can only be one timer on + * a specific priority, to ensure we are deterministic, and to avoid + * container sort order invariant issues with timer period saveload. */ + for (const auto &timer : TimerManager::GetTimers()) { + assert(timer->period.priority != period.priority); + } } #endif /* WITH_ASSERT */ diff --git a/src/timer/timer_game_tick.h b/src/timer/timer_game_tick.h index 0151e45be2..02ae2b16ff 100644 --- a/src/timer/timer_game_tick.h +++ b/src/timer/timer_game_tick.h @@ -24,7 +24,34 @@ public: using Ticks = int32_t; ///< The type to store ticks in using TickCounter = uint64_t; ///< The type that the tick counter is stored in - using TPeriod = uint; + enum Priority { + NONE, ///< These timers can be executed in any order; the order is not relevant. + + /* For all other priorities, the order is important. + * For safety, you can only setup a single timer on a single priority. */ + COMPETITOR_TIMEOUT, + }; + + struct TPeriod { + Priority priority; + uint value; + + TPeriod(Priority priority, uint value) : priority(priority), value(value) + {} + + bool operator < (const TPeriod &other) const + { + /* Sort by priority before value, such that changes in value for priorities other than NONE do not change the container order */ + if (this->priority != other.priority) return this->priority < other.priority; + return this->value < other.value; + } + + bool operator == (const TPeriod &other) const + { + return this->priority == other.priority && this->value == other.value; + } + }; + using TElapsed = uint; struct TStorage { uint elapsed; diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index 283dbd401e..a8bd962001 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -799,7 +799,7 @@ struct TimetableWindow : Window { /** * In real-time mode, the timetable GUI shows relative times and needs to be redrawn every second. */ - IntervalTimer redraw_interval = {Ticks::TICKS_PER_SECOND, [this](auto) { + IntervalTimer redraw_interval = { { TimerGameTick::Priority::NONE, Ticks::TICKS_PER_SECOND }, [this](auto) { if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) { this->SetDirty(); }