1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Process scheduler.
*/
#include "tinsel/handle.h"
#include "tinsel/pcode.h"
#include "tinsel/pid.h"
#include "tinsel/polygons.h"
#include "tinsel/sched.h"
#include "common/textconsole.h"
#include "common/util.h"
namespace Tinsel {
Scheduler *g_scheduler = 0;
#include "common/pack-start.h" // START STRUCT PACKING
struct PROCESS_STRUC {
uint32 processId; // ID of process
SCNHANDLE hProcessCode; // handle to actor script
} PACKED_STRUCT;
#include "common/pack-end.h" // END STRUCT PACKING
//----------------- LOCAL GLOBAL DATA --------------------
// FIXME: Avoid non-const global vars
static uint32 g_numSceneProcess;
static SCNHANDLE g_hSceneProcess;
static uint32 g_numGlobalProcess;
static PROCESS_STRUC *g_pGlobalProcess;
//--------------------- FUNCTIONS ------------------------
Scheduler::Scheduler() {
processList = 0;
pFreeProcesses = 0;
pCurrent = 0;
#ifdef DEBUG
// diagnostic process counters
numProcs = 0;
maxProcs = 0;
#endif
pRCfunction = 0;
active = new PROCESS;
active->pPrevious = NULL;
active->pNext = NULL;
g_scheduler = this; // FIXME HACK
}
Scheduler::~Scheduler() {
// Kill all running processes (i.e. free memory allocated for their state).
PROCESS *pProc = active->pNext;
while (pProc != NULL) {
delete pProc->state;
pProc->state = 0;
pProc = pProc->pNext;
}
free(processList);
processList = NULL;
delete active;
active = 0;
}
/**
* Kills all processes and places them on the free list.
*/
void Scheduler::reset() {
#ifdef DEBUG
// clear number of process in use
numProcs = 0;
#endif
if (processList == NULL) {
// first time - allocate memory for process list
processList = (PROCESS *)calloc(MAX_PROCESSES, sizeof(PROCESS));
// make sure memory allocated
if (processList == NULL) {
error("Cannot allocate memory for process data");
}
// fill with garbage
memset(processList, 'S', MAX_PROCESSES * sizeof(PROCESS));
}
// Kill all running processes (i.e. free memory allocated for their state).
PROCESS *pProc = active->pNext;
while (pProc != NULL) {
delete pProc->state;
pProc->state = 0;
pProc = pProc->pNext;
}
// no active processes
pCurrent = active->pNext = NULL;
// place first process on free list
pFreeProcesses = processList;
// link all other processes after first
for (int i = 1; i <= NUM_PROCESS; i++) {
processList[i - 1].pNext = (i == NUM_PROCESS) ? NULL : processList + i;
processList[i - 1].pPrevious = (i == 1) ? active : processList + (i - 2);
}
}
#ifdef DEBUG
/**
* Shows the maximum number of process used at once.
*/
void Scheduler::printStats() {
debug("%i process of %i used", maxProcs, NUM_PROCESS);
}
#endif
#ifdef DEBUG
/**
* Checks both the active and free process list to insure all the links are valid,
* and that no processes have been lost
*/
void Scheduler::CheckStack() {
Common::List<PROCESS *> pList;
// Check both the active and free process lists
for (int i = 0; i < 2; ++i) {
PROCESS *p = (i == 0) ? active : pFreeProcesses;
if (p != NULL) {
// Make sure the linkages are correct
while (p->pNext != NULL) {
assert(p->pNext->pPrevious == p);
pList.push_back(p);
p = p->pNext;
}
pList.push_back(p);
}
}
// Make sure all processes are accounted for
for (int idx = 0; idx < NUM_PROCESS; idx++) {
bool found = false;
for (Common::List<PROCESS *>::iterator i = pList.begin(); i != pList.end(); ++i) {
PROCESS *pTemp = *i;
if (*i == &processList[idx]) {
found = true;
break;
}
}
assert(found);
}
}
#endif
/**
* Give all active processes a chance to run
*/
void Scheduler::schedule() {
// start dispatching active process list
PROCESS *pNext;
PROCESS *pProc = active->pNext;
while (pProc != NULL) {
pNext = pProc->pNext;
if (--pProc->sleepTime <= 0) {
// process is ready for dispatch, activate it
pCurrent = pProc;
pProc->coroAddr(pProc->state, pProc->param);
if (!pProc->state || pProc->state->_sleep <= 0) {
// Coroutine finished
pCurrent = pCurrent->pPrevious;
killProcess(pProc);
} else {
pProc->sleepTime = pProc->state->_sleep;
}
// pCurrent may have been changed
pNext = pCurrent->pNext;
pCurrent = NULL;
}
pProc = pNext;
}
}
/**
* Reschedules all the processes to run again this query
*/
void Scheduler::rescheduleAll() {
assert(pCurrent);
// Unlink current process
pCurrent->pPrevious->pNext = pCurrent->pNext;
if (pCurrent->pNext)
pCurrent->pNext->pPrevious = pCurrent->pPrevious;
// Add process to the start of the active list
pCurrent->pNext = active->pNext;
active->pNext->pPrevious = pCurrent;
active->pNext = pCurrent;
pCurrent->pPrevious = active;
}
/**
* If the specified process has already run on this tick, make it run
* again on the current tick.
*/
void Scheduler::reschedule(PPROCESS pReSchedProc) {
// If not currently processing the schedule list, then no action is needed
if (!pCurrent)
return;
if (!pReSchedProc)
pReSchedProc = pCurrent;
PPROCESS pEnd;
// Find the last process in the list.
// But if the target process is down the list from here, do nothing
for (pEnd = pCurrent; pEnd->pNext != NULL; pEnd = pEnd->pNext) {
if (pEnd->pNext == pReSchedProc)
return;
}
assert(pEnd->pNext == NULL);
// Could be in the middle of a KillProc()!
// Dying process was last and this process was penultimate
if (pReSchedProc->pNext == NULL)
return;
// If we're moving the current process, move it back by one, so that the next
// schedule() iteration moves to the now next one
if (pCurrent == pReSchedProc)
pCurrent = pCurrent->pPrevious;
// Unlink the process, and add it at the end
pReSchedProc->pPrevious->pNext = pReSchedProc->pNext;
pReSchedProc->pNext->pPrevious = pReSchedProc->pPrevious;
pEnd->pNext = pReSchedProc;
pReSchedProc->pPrevious = pEnd;
pReSchedProc->pNext = NULL;
}
/**
* Moves the specified process to the end of the dispatch queue
* allowing it to run again within the current game cycle.
* @param pGiveProc Which process
*/
void Scheduler::giveWay(PPROCESS pReSchedProc) {
// If not currently processing the schedule list, then no action is needed
if (!pCurrent)
return;
if (!pReSchedProc)
pReSchedProc = pCurrent;
// If the process is already at the end of the queue, nothing has to be done
if (!pReSchedProc->pNext)
return;
PPROCESS pEnd;
// Find the last process in the list.
for (pEnd = pCurrent; pEnd->pNext != NULL; pEnd = pEnd->pNext)
;
assert(pEnd->pNext == NULL);
// If we're moving the current process, move it back by one, so that the next
// schedule() iteration moves to the now next one
if (pCurrent == pReSchedProc)
pCurrent = pCurrent->pPrevious;
// Unlink the process, and add it at the end
pReSchedProc->pPrevious->pNext = pReSchedProc->pNext;
pReSchedProc->pNext->pPrevious = pReSchedProc->pPrevious;
pEnd->pNext = pReSchedProc;
pReSchedProc->pPrevious = pEnd;
pReSchedProc->pNext = NULL;
}
/**
* Creates a new process.
*
* @param pid process identifier
* @param CORO_ADDR coroutine start address
* @param pParam process specific info
* @param sizeParam size of process specific info
*/
PROCESS *Scheduler::createProcess(int pid, CORO_ADDR coroAddr, const void *pParam, int sizeParam) {
PROCESS *pProc;
// get a free process
pProc = pFreeProcesses;
// trap no free process
assert(pProc != NULL); // Out of processes
#ifdef DEBUG
// one more process in use
if (++numProcs > maxProcs)
maxProcs = numProcs;
#endif
// get link to next free process
pFreeProcesses = pProc->pNext;
if (pFreeProcesses)
pFreeProcesses->pPrevious = NULL;
if (pCurrent != NULL) {
// place new process before the next active process
pProc->pNext = pCurrent->pNext;
if (pProc->pNext)
pProc->pNext->pPrevious = pProc;
// make this new process the next active process
pCurrent->pNext = pProc;
pProc->pPrevious = pCurrent;
} else { // no active processes, place process at head of list
pProc->pNext = active->pNext;
pProc->pPrevious = active;
if (pProc->pNext)
pProc->pNext->pPrevious = pProc;
active->pNext = pProc;
}
// set coroutine entry point
pProc->coroAddr = coroAddr;
// clear coroutine state
pProc->state = 0;
// wake process up as soon as possible
pProc->sleepTime = 1;
// set new process id
pProc->pid = pid;
// set new process specific info
if (sizeParam) {
assert(sizeParam > 0 && sizeParam <= PARAM_SIZE);
// set new process specific info
memcpy(pProc->param, pParam, sizeParam);
}
// return created process
return pProc;
}
/**
* Kills the specified process.
*
* @param pKillProc which process to kill
*/
void Scheduler::killProcess(PROCESS *pKillProc) {
// make sure a valid process pointer
assert(pKillProc >= processList && pKillProc <= processList + NUM_PROCESS - 1);
// can not kill the current process using killProcess !
assert(pCurrent != pKillProc);
#ifdef DEBUG
// one less process in use
--numProcs;
assert(numProcs >= 0);
#endif
// Free process' resources
if (pRCfunction != NULL)
(pRCfunction)(pKillProc);
delete pKillProc->state;
pKillProc->state = 0;
// Take the process out of the active chain list
pKillProc->pPrevious->pNext = pKillProc->pNext;
if (pKillProc->pNext)
pKillProc->pNext->pPrevious = pKillProc->pPrevious;
// link first free process after pProc
pKillProc->pNext = pFreeProcesses;
if (pFreeProcesses)
pKillProc->pNext->pPrevious = pKillProc;
pKillProc->pPrevious = NULL;
// make pKillProc the first free process
pFreeProcesses = pKillProc;
}
/**
* Returns a pointer to the currently running process.
*/
PROCESS *Scheduler::getCurrentProcess() {
return pCurrent;
}
/**
* Returns the process identifier of the specified process.
*
* @param pProc which process
*/
int Scheduler::getCurrentPID() const {
PROCESS *pProc = pCurrent;
// make sure a valid process pointer
assert(pProc >= processList && pProc <= processList + NUM_PROCESS - 1);
// return processes PID
return pProc->pid;
}
/**
* Kills any process matching the specified PID. The current
* process cannot be killed.
*
* @param pidKill process identifier of process to kill
* @param pidMask mask to apply to process identifiers before comparison
* @return The number of processes killed is returned.
*/
int Scheduler::killMatchingProcess(int pidKill, int pidMask) {
int numKilled = 0;
PROCESS *pProc, *pPrev; // process list pointers
for (pProc = active->pNext, pPrev = active; pProc != NULL; pPrev = pProc, pProc = pProc->pNext) {
if ((pProc->pid & pidMask) == pidKill) {
// found a matching process
// dont kill the current process
if (pProc != pCurrent) {
// kill this process
numKilled++;
// Free the process' resources
if (pRCfunction != NULL)
(pRCfunction)(pProc);
delete pProc->state;
pProc->state = 0;
// make prev point to next to unlink pProc
pPrev->pNext = pProc->pNext;
if (pProc->pNext)
pPrev->pNext->pPrevious = pPrev;
// link first free process after pProc
pProc->pNext = pFreeProcesses;
pProc->pPrevious = NULL;
pFreeProcesses->pPrevious = pProc;
// make pProc the first free process
pFreeProcesses = pProc;
// set to a process on the active list
pProc = pPrev;
}
}
}
#ifdef DEBUG
// adjust process in use
numProcs -= numKilled;
assert(numProcs >= 0);
#endif
// return number of processes killed
return numKilled;
}
/**
* Set pointer to a function to be called by killProcess().
*
* May be called by a resource allocator, the function supplied is
* called by killProcess() to allow the resource allocator to free
* resources allocated to the dying process.
*
* @param pFunc Function to be called by killProcess()
*/
void Scheduler::setResourceCallback(VFPTRPP pFunc) {
pRCfunction = pFunc;
}
/**************************************************************************\
|*********** Stuff to do with scene and global processes ************|
\**************************************************************************/
/**
* The code for for restored scene processes.
*/
static void RestoredProcessProcess(CORO_PARAM, const void *param) {
CORO_BEGIN_CONTEXT;
INT_CONTEXT *pic;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
// get the stuff copied to process when it was created
_ctx->pic = *(const PINT_CONTEXT *)param;
_ctx->pic = RestoreInterpretContext(_ctx->pic);
AttachInterpret(_ctx->pic, g_scheduler->getCurrentProcess());
CORO_INVOKE_1(Interpret, _ctx->pic);
CORO_END_CODE;
}
/**
* Process Tinsel Process
*/
static void ProcessTinselProcess(CORO_PARAM, const void *param) {
const PINT_CONTEXT *pPic = (const PINT_CONTEXT *)param;
CORO_BEGIN_CONTEXT;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
// get the stuff copied to process when it was created
CORO_INVOKE_1(Interpret, *pPic);
CORO_KILL_SELF();
CORO_END_CODE;
}
/**************************************************************************\
|***************** Stuff to do with scene processes *****************|
\**************************************************************************/
/**
* Called to restore a scene process.
*/
void RestoreSceneProcess(INT_CONTEXT *pic) {
uint32 i;
PROCESS_STRUC *pStruc;
pStruc = (PROCESS_STRUC *)LockMem(g_hSceneProcess);
for (i = 0; i < g_numSceneProcess; i++) {
if (FROM_LE_32(pStruc[i].hProcessCode) == pic->hCode) {
g_scheduler->createProcess(PID_PROCESS + i, RestoredProcessProcess,
&pic, sizeof(pic));
break;
}
}
assert(i < g_numSceneProcess);
}
/**
* Run a scene process with the given event.
*/
void SceneProcessEvent(CORO_PARAM, uint32 procID, TINSEL_EVENT event, bool bWait, int myEscape,
bool *result) {
uint32 i; // Loop counter
if (result) *result = false;
CORO_BEGIN_CONTEXT;
PROCESS_STRUC *pStruc;
PPROCESS pProc;
PINT_CONTEXT pic;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
_ctx->pStruc = (PROCESS_STRUC *)LockMem(g_hSceneProcess);
for (i = 0; i < g_numSceneProcess; i++) {
if (FROM_LE_32(_ctx->pStruc[i].processId) == procID) {
assert(_ctx->pStruc[i].hProcessCode); // Must have some code to run
_ctx->pic = InitInterpretContext(GS_PROCESS,
FROM_LE_32(_ctx->pStruc[i].hProcessCode),
event,
NOPOLY, // No polygon
0, // No actor
NULL, // No object
myEscape);
if (_ctx->pic == NULL)
return;
_ctx->pProc = g_scheduler->createProcess(PID_PROCESS + i, ProcessTinselProcess,
&_ctx->pic, sizeof(_ctx->pic));
AttachInterpret(_ctx->pic, _ctx->pProc);
break;
}
}
if (i == g_numSceneProcess)
return;
if (bWait) {
CORO_INVOKE_2(WaitInterpret, _ctx->pProc, result);
}
CORO_END_CODE;
}
/**
* Kill all instances of a scene process.
*/
void KillSceneProcess(uint32 procID) {
uint32 i; // Loop counter
PROCESS_STRUC *pStruc;
pStruc = (PROCESS_STRUC *) LockMem(g_hSceneProcess);
for (i = 0; i < g_numSceneProcess; i++) {
if (FROM_LE_32(pStruc[i].processId) == procID) {
g_scheduler->killMatchingProcess(PID_PROCESS + i, -1);
break;
}
}
}
/**
* Register the scene processes in a scene.
*/
void SceneProcesses(uint32 numProcess, SCNHANDLE hProcess) {
g_numSceneProcess = numProcess;
g_hSceneProcess = hProcess;
}
/**************************************************************************\
|***************** Stuff to do with global processes ****************|
\**************************************************************************/
/**
* Called to restore a global process.
*/
void RestoreGlobalProcess(INT_CONTEXT *pic) {
uint32 i; // Loop counter
for (i = 0; i < g_numGlobalProcess; i++) {
if (g_pGlobalProcess[i].hProcessCode == pic->hCode) {
g_scheduler->createProcess(PID_GPROCESS + i, RestoredProcessProcess,
&pic, sizeof(pic));
break;
}
}
assert(i < g_numGlobalProcess);
}
/**
* Kill them all (restore game).
*/
void KillGlobalProcesses() {
for (uint32 i = 0; i < g_numGlobalProcess; ++i) {
g_scheduler->killMatchingProcess(PID_GPROCESS + i, -1);
}
}
/**
* Run a global process with the given event.
*/
bool GlobalProcessEvent(CORO_PARAM, uint32 procID, TINSEL_EVENT event, bool bWait, int myEscape) {
CORO_BEGIN_CONTEXT;
PINT_CONTEXT pic;
PPROCESS pProc;
CORO_END_CONTEXT(_ctx);
bool result = false;
CORO_BEGIN_CODE(_ctx);
uint32 i; // Loop counter
_ctx->pProc = NULL;
for (i = 0; i < g_numGlobalProcess; ++i) {
if (g_pGlobalProcess[i].processId == procID) {
assert(g_pGlobalProcess[i].hProcessCode); // Must have some code to run
_ctx->pic = InitInterpretContext(GS_GPROCESS,
g_pGlobalProcess[i].hProcessCode,
event,
NOPOLY, // No polygon
0, // No actor
NULL, // No object
myEscape);
if (_ctx->pic != NULL) {
_ctx->pProc = g_scheduler->createProcess(PID_GPROCESS + i, ProcessTinselProcess,
&_ctx->pic, sizeof(_ctx->pic));
AttachInterpret(_ctx->pic, _ctx->pProc);
}
break;
}
}
if ((i == g_numGlobalProcess) || (_ctx->pic == NULL))
result = false;
else if (bWait)
CORO_INVOKE_ARGS_V(WaitInterpret, false, (CORO_SUBCTX, _ctx->pProc, &result));
CORO_END_CODE;
return result;
}
/**
* Kill all instances of a global process.
*/
void xKillGlobalProcess(uint32 procID) {
uint32 i; // Loop counter
for (i = 0; i < g_numGlobalProcess; ++i) {
if (g_pGlobalProcess[i].processId == procID) {
g_scheduler->killMatchingProcess(PID_GPROCESS + i, -1);
break;
}
}
}
/**
* Register the global processes list
*/
void GlobalProcesses(uint32 numProcess, byte *pProcess) {
g_pGlobalProcess = new PROCESS_STRUC[numProcess];
g_numGlobalProcess = numProcess;
byte *p = pProcess;
for (uint i = 0; i < numProcess; ++i, p += 8) {
g_pGlobalProcess[i].processId = READ_LE_UINT32(p);
g_pGlobalProcess[i].hProcessCode = READ_LE_UINT32(p + 4);
}
}
/**
* Frees the global processes list
*/
void FreeGlobalProcesses() {
delete[] g_pGlobalProcess;
g_pGlobalProcess = 0;
g_numGlobalProcess = 0;
}
} // End of namespace Tinsel
|