diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 3671ef4ce..7b81cd71c 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.2: +- Fixed a potential semaphore race when suspending clock -PhracturedBlue - Function name conflict fixes for Mac OSX -by peterb - Minor fixes for pc-fonts (Outfit screen, gradient color swap) -by Nic - DCQ is now accessed uniformly by routines in gfx_common.c diff --git a/sc2/src/sc2code/clock.c b/sc2/src/sc2code/clock.c index e1a5985fe..28a8c12ae 100644 --- a/sc2/src/sc2code/clock.c +++ b/sc2/src/sc2code/clock.c @@ -20,6 +20,8 @@ #define IsLeapYear(yi) (!((yi) & 3) && (((yi) % 100) || ((yi) % 400))) +static Mutex clock_mutex; + int clock_task_func(void* data) { BOOLEAN LastPilot; @@ -170,7 +172,7 @@ InitGameClock (void) { if (!InitQueue (&GLOBAL (GameClock.event_q), NUM_EVENTS, sizeof (EVENT))) return (FALSE); - + clock_mutex = CreateMutex (); GLOBAL (GameClock.month_index) = 2; GLOBAL (GameClock.day_index) = 17; GLOBAL (GameClock.year_index) = START_YEAR; /* Feb 17, START_YEAR */ @@ -189,13 +191,13 @@ UninitGameClock (void) { if (GLOBAL (GameClock.clock_task)) { - if (!GameClockRunning ()) - ResumeGameClock (); + ResumeGameClock (); ConcludeTask (GLOBAL (GameClock.clock_task)); GLOBAL (GameClock.clock_task) = 0; } + DestroyMutex (clock_mutex); UninitQueue (&GLOBAL (GameClock.event_q)); @@ -205,21 +207,25 @@ UninitGameClock (void) void SuspendGameClock (void) { + LockMutex (clock_mutex); if (GameClockRunning ()) { SetSemaphore (GLOBAL (GameClock.clock_sem)); GLOBAL (GameClock.TimeCounter) = 0; } + UnlockMutex (clock_mutex); } void ResumeGameClock (void) { + LockMutex (clock_mutex); if (!GameClockRunning ()) { GLOBAL (GameClock.TimeCounter) = GetTimeCounter (); ClearSemaphore (GLOBAL (GameClock.clock_sem)); } + UnlockMutex (clock_mutex); } BOOLEAN diff --git a/sc2/src/sc2code/confirm.c b/sc2/src/sc2code/confirm.c index ea32829cb..28853aa74 100644 --- a/sc2/src/sc2code/confirm.c +++ b/sc2/src/sc2code/confirm.c @@ -30,11 +30,8 @@ INPUT_STATE ConfirmExit (void) { INPUT_STATE InputState; - BOOLEAN ClockActive; - ClockActive = (BOOLEAN)(LOBYTE (GLOBAL (CurrentActivity)) != SUPER_MELEE - && GameClockRunning ()); - if (ClockActive) + if (LOBYTE (GLOBAL (CurrentActivity)) != SUPER_MELEE) SuspendGameClock (); else if (CommData.ConversationPhrases && PlayingTrack ()) PauseTrack (); @@ -105,7 +102,7 @@ ConfirmExit (void) } ClearSemaphore (GraphicsSem); - if (ClockActive) + if (LOBYTE (GLOBAL (CurrentActivity)) != SUPER_MELEE) ResumeGameClock (); else if (CommData.ConversationPhrases && PlayingTrack ()) ResumeTrack (); diff --git a/sc2/src/sc2code/hyper.c b/sc2/src/sc2code/hyper.c index eed2c4385..91a06ef19 100644 --- a/sc2/src/sc2code/hyper.c +++ b/sc2/src/sc2code/hyper.c @@ -362,8 +362,7 @@ BOOLEAN FreeHyperspace (void) { ClearSemaphore (GraphicsSem); - if (GameClockRunning ()) - SuspendGameClock (); + SuspendGameClock (); SetSemaphore (GraphicsSem); { diff --git a/sc2/src/sc2code/starcon.c b/sc2/src/sc2code/starcon.c index 980846b71..fb41da157 100644 --- a/sc2/src/sc2code/starcon.c +++ b/sc2/src/sc2code/starcon.c @@ -902,8 +902,7 @@ extern COUNT _simple_count; sc = _simple_count; #endif //TESTING - if (GameClockRunning ()) - SuspendGameClock (); + SuspendGameClock (); if (!((GLOBAL (CurrentActivity) | NextActivity) & CHECK_LOAD)) ZeroVelocityComponents ( diff --git a/sc2/src/sc2code/utils.c b/sc2/src/sc2code/utils.c index f9917f47c..d749070b9 100644 --- a/sc2/src/sc2code/utils.c +++ b/sc2/src/sc2code/utils.c @@ -141,7 +141,6 @@ PauseGame (void) { RECT r; STAMP s; - BOOLEAN ClockActive; CONTEXT OldContext; FRAME F; HOT_SPOT OldHot; @@ -153,11 +152,7 @@ PauseGame (void) GLOBAL (CurrentActivity) |= CHECK_PAUSE; - ClockActive = (BOOLEAN)( - LOBYTE (GLOBAL (CurrentActivity)) != SUPER_MELEE - && GameClockRunning () - ); - if (ClockActive) + if (LOBYTE (GLOBAL (CurrentActivity)) != SUPER_MELEE) SuspendGameClock (); else if (CommData.ConversationPhrases && PlayingTrack ()) PauseTrack (); @@ -201,7 +196,7 @@ PauseGame (void) FlushInput (); ClearSemaphore (GraphicsSem); - if (ClockActive) + if (LOBYTE (GLOBAL (CurrentActivity)) != SUPER_MELEE) ResumeGameClock (); else if (CommData.ConversationPhrases && PlayingTrack ()) ResumeTrack ();