1 | // tTask.cpp  |
2 | //  |
3 | // Simple and efficient task management using a heap-based priority queue.  |
4 | //  |
5 | // Copyright (c) 2006, 2017 Tristan Grimmer.  |
6 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
7 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
8 | //  |
9 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
10 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
11 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
12 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
13 | // PERFORMANCE OF THIS SOFTWARE.  |
14 |   |
15 | #include "System/tTask.h"  |
16 |   |
17 |   |
18 | tTaskSet::tTaskSet(int64 counterFreq, double maxTimeDelta) :  |
19 | ExecuteTime(0),  |
20 | CounterFreq(counterFreq),  |
21 | MaxTimeDelta(maxTimeDelta),  |
22 | PriorityQueue(NumTasks, GrowSize)  |
23 | {  |
24 | }  |
25 |   |
26 |   |
27 | tTaskSet::tTaskSet() :  |
28 | ExecuteTime(0),  |
29 | CounterFreq(0),  |
30 | MaxTimeDelta(0),  |
31 | PriorityQueue(NumTasks, GrowSize)  |
32 | {  |
33 | }  |
34 |   |
35 |   |
36 | void tTaskSet::Update(int64 counter)  |
37 | {  |
38 | bool runningTasks = true;  |
39 | while (runningTasks)  |
40 | {  |
41 | if (PriorityQueue.GetNumItems() == 0)  |
42 | return;  |
43 |   |
44 | if (PriorityQueue.GetMin().Key <= counter)  |
45 | {  |
46 | tPQ<tTask*>::tItem qn = PriorityQueue.GetRemoveMin();  |
47 | tTask* t = (tTask*)qn.Data;  |
48 |   |
49 | // If there is no function tTask pointer we're all done. The node is already removed from the queue.  |
50 | if (t)  |
51 | {  |
52 | double td = double(counter - ExecuteTime) / double(CounterFreq);  |
53 | if (td > MaxTimeDelta)  |
54 | td = MaxTimeDelta;  |
55 |   |
56 | double nextTime = t->Execute(td);  |
57 | int64 nextTimeDelta = int64( nextTime*double(CounterFreq) );  |
58 |   |
59 | // The 1 guarantees no infinite loop here.  |
60 | if (nextTimeDelta <= 0)  |
61 | nextTimeDelta = 1;  |
62 |   |
63 | qn.Key = counter + nextTimeDelta;  |
64 | PriorityQueue.Insert(qn);  |
65 | }  |
66 | }  |
67 | else  |
68 | {  |
69 | runningTasks = false;  |
70 | }  |
71 | }  |
72 |   |
73 | ExecuteTime = counter;  |
74 | }  |
75 | |