From 8aab2ae592b5e0c1472d7ebcee168391505015a8 Mon Sep 17 00:00:00 2001 From: avolkov Date: Sun, 29 Jan 2012 20:18:50 +0000 Subject: [PATCH] Split debug key function into sync and async parts (wrt the game logic thread), paving the way for GraphicsLock and GameClock lock removal git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3759 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/ChangeLog | 2 ++ sc2/src/uqm/clock.c | 12 +++------- sc2/src/uqm/gameinp.c | 18 ++++----------- sc2/src/uqm/planets/calc.c | 3 ++- sc2/src/uqm/starcon.c | 18 +++++++++++---- sc2/src/uqm/uqmdebug.c | 47 ++++++++++++++++++++++++++------------ sc2/src/uqm/uqmdebug.h | 23 +++++++++++-------- 7 files changed, 71 insertions(+), 52 deletions(-) diff --git a/sc2/ChangeLog b/sc2/ChangeLog index f6d64b3d9..c0da43e62 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,6 @@ Changes towards version 0.8: +- Split debug key function into sync and async parts, paving the way + for GraphicsLock removal - Alex - PageUp/PageDown now add/remove 10 fuel in the shipyard, from Scott A. Colcord, Nic - Annihigate flash thread - SvdB diff --git a/sc2/src/uqm/clock.c b/sc2/src/uqm/clock.c index c45deffb8..17293520c 100644 --- a/sc2/src/uqm/clock.c +++ b/sc2/src/uqm/clock.c @@ -157,22 +157,16 @@ UnlockGameClock (void) BOOLEAN GameClockRunning (void) { - SIZE prev_tick, cur_tick; + SIZE day_in_ticks; if (!clock_mutex) return FALSE; LockMutex (clock_mutex); - prev_tick = GLOBAL (GameClock.tick_count); + day_in_ticks = GLOBAL (GameClock.day_in_ticks); UnlockMutex (clock_mutex); - SleepThread (ONE_SECOND / 5); - - LockMutex (clock_mutex); - cur_tick = GLOBAL (GameClock.tick_count); - UnlockMutex (clock_mutex); - - return cur_tick != prev_tick; + return day_in_ticks != 0; } void diff --git a/sc2/src/uqm/gameinp.c b/sc2/src/uqm/gameinp.c index befd9937e..66e667fe0 100644 --- a/sc2/src/uqm/gameinp.c +++ b/sc2/src/uqm/gameinp.c @@ -275,6 +275,11 @@ UpdateInputState (void) if (CurrentInputState.menu[KEY_EXIT]) ExitRequested = TRUE; + +#if defined(DEBUG) || defined(USE_DEBUG_KEY) + if (PulsedInputState.menu[KEY_DEBUG]) + debugKeyPressedSynchronous (); +#endif } InputFrameCallback * @@ -365,19 +370,6 @@ DoInput (void *pInputState, BOOLEAN resetInput) UpdateInputState (); -#ifdef DEBUG - if (doInputDebugHook != NULL) - { - void (*saveDebugHook) (void); - saveDebugHook = doInputDebugHook; - doInputDebugHook = NULL; - // No further debugHook calls unless the called - // function resets doInputDebugHook. - (*saveDebugHook) (); - continue; - } -#endif - #if DEMO_MODE || CREATE_JOURNAL if (ArrowInput != DemoInput) #endif diff --git a/sc2/src/uqm/planets/calc.c b/sc2/src/uqm/planets/calc.c index 20425e4b4..047a990f2 100644 --- a/sc2/src/uqm/planets/calc.c +++ b/sc2/src/uqm/planets/calc.c @@ -352,7 +352,8 @@ CalcLifeChance (const PLANET_INFO *PlanetInfoPtr) void DoPlanetaryAnalysis (SYSTEM_INFO *SysInfoPtr, PLANET_DESC *pPlanetDesc) { - assert (pPlanetDesc->data_index != HIERARCHY_STARBASE); + assert ((pPlanetDesc->data_index & ~WORLD_TYPE_SPECIAL) + < NUMBER_OF_PLANET_TYPES); RandomContext_SeedRandom (SysGenRNG, pPlanetDesc->rand_seed); diff --git a/sc2/src/uqm/starcon.c b/sc2/src/uqm/starcon.c index c18e2ad6a..ac38f950b 100644 --- a/sc2/src/uqm/starcon.c +++ b/sc2/src/uqm/starcon.c @@ -105,6 +105,7 @@ BackgroundInitKernel (DWORD TimeOut) } } +// Executes on the main() thread void SignalStopMainThread (void) { @@ -113,6 +114,7 @@ SignalStopMainThread (void) TaskSwitch (); } +// Executes on the main() thread void ProcessUtilityKeys (void) { @@ -132,11 +134,17 @@ ProcessUtilityKeys (void) } #if defined(DEBUG) || defined(USE_DEBUG_KEY) - if (ImmediateInputState.menu[KEY_DEBUG]) - { - // clear ImmediateInputState so we don't repeat this next frame - FlushInput (); - debugKeyPressed (); + { // Only call the debug func on the rising edge of + // ImmediateInputState[KEY_DEBUG] so it does not execute repeatedly. + // This duplicates the PulsedInputState somewhat, but we cannot + // use PulsedInputState here because it is meant for another thread. + static int debugKeyState; + + if (ImmediateInputState.menu[KEY_DEBUG] && debugKeyState == 0) + { + debugKeyPressed (); + } + debugKeyState = ImmediateInputState.menu[KEY_DEBUG]; } #endif /* DEBUG */ } diff --git a/sc2/src/uqm/uqmdebug.c b/sc2/src/uqm/uqmdebug.c index 10b67020a..f5d8d4b33 100644 --- a/sc2/src/uqm/uqmdebug.c +++ b/sc2/src/uqm/uqmdebug.c @@ -72,11 +72,12 @@ static void dumpPlanetTypeCallback (int index, const PlanetFrame *planet, BOOLEAN instantMove = FALSE; BOOLEAN disableInteractivity = FALSE; void (* volatile debugHook) (void) = NULL; -void (* volatile doInputDebugHook) (void) = NULL; +// Must be called on the Starcon2Main thread. +// This function is called synchronously wrt the game logic thread. void -debugKeyPressed (void) +debugKeyPressedSynchronous (void) { // State modifying: equipShip (); @@ -107,28 +108,36 @@ debugKeyPressed (void) // SET_GAME_STATE (MELNORME_CREDIT1, 100); // GLOBAL_SIS (ResUnits) = 100000; + // Informational: +// dumpEvents (stderr); + + // Graphical and textual: +// debugContexts(); +} + +// Can be called on any thread, but usually on main() +// This function is called asynchronously wrt the game logic thread, +// which means locking applies. Use carefully. +// TODO: Once game logic thread is purged of graphics and clock locks, +// this function may not call graphics and game clock functions at all. +void +debugKeyPressed (void) +{ // Tests // Scale_PerfTest (); // Informational: // dumpStrings (stdout); -// dumpEvents (stderr); // dumpPlanetTypes(stderr); // debugHook = dumpUniverseToFile; // This will cause dumpUniverseToFile to be called from the - // main loop. Calling it from here would give threading + // Starcon2Main loop. Calling it from here would give threading // problems. // debugHook = tallyResourcesToFile; // This will cause tallyResourcesToFile to be called from the - // main loop. Calling it from here would give threading + // Starcon2Main loop. Calling it from here would give threading // problems. - // Graphical and textual: - //doInputDebugHook = debugContexts; - // This will cause debugContexts to be called from the - // Starcon2Main thread, from DoInput(). Calling it from here - // would give threading problems. - // Interactive: // uio_debugInteractive(stdin, stdout, stderr); } @@ -137,6 +146,9 @@ debugKeyPressed (void) // Fast forwards to the next event. // If skipHEE is set, HYPERSPACE_ENCOUNTER_EVENTs are skipped. +// Must be called from the Starcon2Main thread. +// TODO: GraphicsLock and LockGameClock may be removed since it is only +// supposed to be called synchronously wrt the game logic thread. void forwardToNextEvent (BOOLEAN skipHEE) { @@ -596,6 +608,8 @@ forAllMoons (STAR_DESC *star, SOLARSYS_STATE *system, PLANET_DESC *planet, //////////////////////////////////////////////////////////////////////////// +// Must be called from the Starcon2Main thread. +// TODO: LockGameClock may be removed void UniverseRecurse (UniverseRecurseArg *universeRecurseArg) { @@ -728,10 +742,13 @@ moonRecurse (STAR_DESC *star, SOLARSYS_STATE *system, PLANET_DESC *planet, if (universeRecurseArg->moonFunc != NULL) { system->pOrbitalDesc = moon; - DoPlanetaryAnalysis (&system->SysInfo, moon); + if (moon->data_index != HIERARCHY_STARBASE && moon->data_index != SA_MATRA) + { + DoPlanetaryAnalysis (&system->SysInfo, moon); // When GenerateDefaultFunctions is used as genFuncs, // generateOrbital will also call DoPlanetaryAnalysis, // but with other GenerateFunctions this is not guaranteed. + } (*system->genFuncs->generateOrbital) (system, moon); (*universeRecurseArg->moonFunc) ( moon, universeRecurseArg->arg); @@ -745,6 +762,7 @@ typedef struct FILE *out; } DumpUniverseArg; +// Must be called from the Starcon2Main thread. void dumpUniverse (FILE *out) { @@ -763,7 +781,7 @@ dumpUniverse (FILE *out) UniverseRecurse (&universeRecurseArg); } -// Must be called from the main thread. +// Must be called from the Starcon2Main thread. void dumpUniverseToFile (void) { @@ -1146,6 +1164,7 @@ struct TallyResourcesArg COUNT bioCount; }; +// Must be called from the Starcon2Main thread. void tallyResources (FILE *out) { @@ -1164,7 +1183,7 @@ tallyResources (FILE *out) UniverseRecurse (&universeRecurseArg); } -// Must be called from the main thread. +// Must be called from the Starcon2Main thread. void tallyResourcesToFile (void) { diff --git a/sc2/src/uqm/uqmdebug.h b/sc2/src/uqm/uqmdebug.h index d1996c7d6..d100625bb 100644 --- a/sc2/src/uqm/uqmdebug.h +++ b/sc2/src/uqm/uqmdebug.h @@ -33,19 +33,19 @@ extern BOOLEAN disableInteractivity; // Starcon2Main thread, in the main game loop. extern void (* volatile debugHook) (void); -// If a function is assigned to this, it will be called from the -// Starcon2Main thread, in doInput(). -extern void (* volatile doInputDebugHook) (void); - - -// Called when the debug key (symbol 'Debug' in the keys.cfg) is pressed. +// Called on the main() thread when the debug key (symbol 'Debug' in the +// keys.cfg) is pressed void debugKeyPressed (void); +// Called on the Starcon2Main() thread when the debug key (symbol 'Debug' +// in the keys.cfg) is pressed. +void debugKeyPressedSynchronous (void); // Forward time to the next event. If skipHEE is set, the event named // HYPERSPACE_ENCOUNTER_EVENT, which normally occurs every game day, -// is skipped. +// is skipped. Must be called on the Starcon2Main thread. void forwardToNextEvent (BOOLEAN skipHEE); // Generate a list of all events in the event queue. +// Must be called on the Starcon2Main thread. void dumpEvents (FILE *out); // Describe one event. void dumpEvent (FILE *out, const EVENT *eventPtr); @@ -102,11 +102,13 @@ typedef struct // User data. } UniverseRecurseArg; // Recurse through all systems, planets, and moons in the universe. +// Must be called on the Starcon2Main thread. void UniverseRecurse (UniverseRecurseArg *universeRecurseArg); -// Describe the entire universe. +// Describe the entire universe. Must be called on the Starcon2Main thread. void dumpUniverse (FILE *out); // Describe the entire universe, output to a file "./PlanetInfo". +// Must be called on the Starcon2Main thread. void dumpUniverseToFile (void); // Describe one star system. void dumpSystem (FILE *out, const STAR_DESC *star, @@ -137,9 +139,10 @@ void generateBioIndex(const SOLARSYS_STATE *system, const PLANET_DESC *world, COUNT bio[]); // Tally the resources for each star system. +// Must be called on the Starcon2Main thread. void tallyResources (FILE *out); // Tally the resources for each star system, output to a file -// "./ResourceTally". +// "./ResourceTally". Must be called on the Starcon2Main thread. void tallyResourcesToFile (void); @@ -186,7 +189,7 @@ void dumpStrings(FILE *out); // Graphically and textually show all the contexts. -// Should be called from debugHook. +// Must be called on the Starcon2Main thread. void debugContexts (void);