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
|
/* 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.
*
*/
#include "common/scummsys.h"
#include "backends/timer/default/default-timer.h"
#include "common/util.h"
#include "common/system.h"
struct TimerSlot {
Common::TimerManager::TimerProc callback;
void *refCon;
Common::String id;
uint32 interval; // in microseconds
uint32 nextFireTime; // in milliseconds
uint32 nextFireTimeMicro; // microseconds part of nextFire
TimerSlot *next;
};
void insertPrioQueue(TimerSlot *head, TimerSlot *newSlot) {
// The head points to a fake anchor TimerSlot; this common
// trick allows us to get rid of many special cases.
const uint32 nextFireTime = newSlot->nextFireTime;
TimerSlot *slot = head;
newSlot->next = 0;
// Insert the new slot into the sorted list of already scheduled
// timers in such a way that the list stays sorted...
while (true) {
assert(slot);
if (slot->next == 0 || nextFireTime < slot->next->nextFireTime) {
newSlot->next = slot->next;
slot->next = newSlot;
return;
}
slot = slot->next;
}
}
DefaultTimerManager::DefaultTimerManager() :
_head(0) {
_head = new TimerSlot();
memset(_head, 0, sizeof(TimerSlot));
}
DefaultTimerManager::~DefaultTimerManager() {
Common::StackLock lock(_mutex);
TimerSlot *slot = _head;
while (slot) {
TimerSlot *next = slot->next;
delete slot;
slot = next;
}
_head = 0;
}
void DefaultTimerManager::handler() {
Common::StackLock lock(_mutex);
uint32 curTime = g_system->getMillis(true);
// Repeat as long as there is a TimerSlot that is scheduled to fire.
TimerSlot *slot = _head->next;
while (slot && slot->nextFireTime < curTime) {
// Remove the slot from the priority queue
_head->next = slot->next;
// Update the fire time and reinsert the TimerSlot into the priority
// queue.
assert(slot->interval > 0);
slot->nextFireTime += (slot->interval / 1000);
slot->nextFireTimeMicro += (slot->interval % 1000);
if (slot->nextFireTimeMicro > 1000) {
slot->nextFireTime += slot->nextFireTimeMicro / 1000;
slot->nextFireTimeMicro %= 1000;
}
insertPrioQueue(_head, slot);
// Invoke the timer callback
assert(slot->callback);
slot->callback(slot->refCon);
// Look at the next scheduled timer
slot = _head->next;
}
}
bool DefaultTimerManager::installTimerProc(TimerProc callback, int32 interval, void *refCon, const Common::String &id) {
assert(interval > 0);
Common::StackLock lock(_mutex);
if (_callbacks.contains(id)) {
if (_callbacks[id] != callback) {
error("Different callbacks are referred by same name (%s)", id.c_str());
}
}
TimerSlotMap::const_iterator i;
for (i = _callbacks.begin(); i != _callbacks.end(); ++i) {
if (i->_value == callback) {
error("Same callback added twice (old name: %s, new name: %s)", i->_key.c_str(), id.c_str());
}
}
_callbacks[id] = callback;
TimerSlot *slot = new TimerSlot;
slot->callback = callback;
slot->refCon = refCon;
slot->id = id;
slot->interval = interval;
slot->nextFireTime = g_system->getMillis() + interval / 1000;
slot->nextFireTimeMicro = interval % 1000;
slot->next = 0;
insertPrioQueue(_head, slot);
return true;
}
void DefaultTimerManager::removeTimerProc(TimerProc callback) {
Common::StackLock lock(_mutex);
TimerSlot *slot = _head;
while (slot->next) {
if (slot->next->callback == callback) {
TimerSlot *next = slot->next->next;
delete slot->next;
slot->next = next;
} else {
slot = slot->next;
}
}
// We need to remove all names referencing the timer proc here.
//
// Else we run into troubles, when the client code removes and readds timer
// callbacks.
//
// Another issues occurs when one plays a game with ALSA as music driver,
// does RTL and starts a different engine game with ALSA as music driver.
// In this case the MPU401 code will add different timer procs with the
// same name, resulting in two different callbacks added with the same
// name and causing installTimerProc to error out.
// A good test case is running a SCUMM with ALSA output and then a KYRA
// game for example.
for (TimerSlotMap::iterator i = _callbacks.begin(), end = _callbacks.end(); i != end; ++i) {
if (i->_value == callback)
_callbacks.erase(i);
}
}
|