1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * To ensure exclusive use of resources and exclusive control responsibilities.
22 */
23
24 #include "common/util.h"
25
26 #include "tinsel/sched.h"
27 #include "tinsel/token.h"
28
29 namespace Tinsel {
30
31 //----------------- LOCAL GLOBAL DATA --------------------
32
33 struct Token {
34 Common::PROCESS *proc;
35 };
36
37 // These vars are reset upon engine destruction
38 static Token g_tokens[NUMTOKENS];
39
40
41 /**
42 * Release all tokens held by this process, and kill the process.
43 */
TerminateProcess(Common::PROCESS * tProc)44 static void TerminateProcess(Common::PROCESS *tProc) {
45
46 // Release tokens held by the process
47 for (int i = 0; i < NUMTOKENS; i++) {
48 if (g_tokens[i].proc == tProc) {
49 g_tokens[i].proc = nullptr;
50 }
51 }
52
53 // Kill the process
54 CoroScheduler.killProcess(tProc);
55 }
56
57 /**
58 * Gain control of the CONTROL token if it is free.
59 */
GetControlToken()60 void GetControlToken() {
61 const int which = TOKEN_CONTROL;
62
63 if (g_tokens[which].proc == NULL) {
64 g_tokens[which].proc = CoroScheduler.getCurrentProcess();
65 }
66 }
67
68 /**
69 * Release control of the CONTROL token.
70 */
FreeControlToken()71 void FreeControlToken() {
72 // Allow anyone to free TOKEN_CONTROL
73 g_tokens[TOKEN_CONTROL].proc = nullptr;
74 }
75
76
77 /**
78 * Gain control of a token. If the requested token is out of range, or
79 * is already held by the calling process, then the calling process
80 * will be killed off.
81 *
82 * Otherwise, the calling process will gain the token. If the token was
83 * held by another process, then the previous holder is killed off.
84 */
GetToken(int which)85 void GetToken(int which) {
86 assert(TOKEN_LEAD <= which && which < NUMTOKENS);
87
88 if (g_tokens[which].proc != NULL) {
89 assert(g_tokens[which].proc != CoroScheduler.getCurrentProcess());
90 TerminateProcess(g_tokens[which].proc);
91 }
92
93 g_tokens[which].proc = CoroScheduler.getCurrentProcess();
94 }
95
96 /**
97 * Release control of a token. If the requested token is not owned by
98 * the calling process, then the calling process will be killed off.
99 */
FreeToken(int which)100 void FreeToken(int which) {
101 assert(TOKEN_LEAD <= which && which < NUMTOKENS);
102
103 assert(g_tokens[which].proc == CoroScheduler.getCurrentProcess()); // we'd have been killed if some other proc had taken this token
104
105 g_tokens[which].proc = nullptr;
106 }
107
108 /**
109 * If it's a valid token and it's free, returns true.
110 */
TestToken(int which)111 bool TestToken(int which) {
112 if (which < 0 || which >= NUMTOKENS)
113 return false;
114
115 return (g_tokens[which].proc == NULL);
116 }
117
118 /**
119 * Call at the start of each scene.
120 */
FreeAllTokens()121 void FreeAllTokens() {
122 for (int i = 0; i < NUMTOKENS; i++) {
123 g_tokens[i].proc = nullptr;
124 }
125 }
126
127 } // End of namespace Tinsel
128