diff --git a/engines/tinsel/token.cpp b/engines/tinsel/token.cpp index 6494bc28836..2acdbd7fbe0 100644 --- a/engines/tinsel/token.cpp +++ b/engines/tinsel/token.cpp @@ -31,6 +31,7 @@ namespace Tinsel { struct Token { Common::PROCESS *proc; + bool isFree; }; // These vars are reset upon engine destruction @@ -44,13 +45,15 @@ static void TerminateProcess(Common::PROCESS *tProc) { // Release tokens held by the process for (int i = 0; i < NUMTOKENS; i++) { - if (g_tokens[i].proc == tProc) { + if (!g_tokens[i].isFree && g_tokens[i].proc == tProc) { g_tokens[i].proc = nullptr; + g_tokens[i].isFree = true; } } // Kill the process - CoroScheduler.killProcess(tProc); + if (tProc != nullptr) + CoroScheduler.killProcess(tProc); } /** @@ -59,8 +62,9 @@ static void TerminateProcess(Common::PROCESS *tProc) { void GetControlToken() { const int which = TOKEN_CONTROL; - if (g_tokens[which].proc == NULL) { + if (g_tokens[which].isFree) { g_tokens[which].proc = CoroScheduler.getCurrentProcess(); + g_tokens[which].isFree = false; } } @@ -70,6 +74,7 @@ void GetControlToken() { void FreeControlToken() { // Allow anyone to free TOKEN_CONTROL g_tokens[TOKEN_CONTROL].proc = nullptr; + g_tokens[TOKEN_CONTROL].isFree = true; } @@ -84,12 +89,13 @@ void FreeControlToken() { void GetToken(int which) { assert(TOKEN_LEAD <= which && which < NUMTOKENS); - if (g_tokens[which].proc != NULL) { + if (!g_tokens[which].isFree) { assert(g_tokens[which].proc != CoroScheduler.getCurrentProcess()); TerminateProcess(g_tokens[which].proc); } g_tokens[which].proc = CoroScheduler.getCurrentProcess(); + g_tokens[which].isFree = false; } /** @@ -102,6 +108,7 @@ void FreeToken(int which) { assert(g_tokens[which].proc == CoroScheduler.getCurrentProcess()); // we'd have been killed if some other proc had taken this token g_tokens[which].proc = nullptr; + g_tokens[which].isFree = true; } /** @@ -111,7 +118,7 @@ bool TestToken(int which) { if (which < 0 || which >= NUMTOKENS) return false; - return (g_tokens[which].proc == NULL); + return (g_tokens[which].isFree); } /** @@ -120,6 +127,7 @@ bool TestToken(int which) { void FreeAllTokens() { for (int i = 0; i < NUMTOKENS; i++) { g_tokens[i].proc = nullptr; + g_tokens[i].isFree = true; } }