2 # include <simgear_config.h>
5 #include "event_mgr.hxx"
7 #include <simgear/debug/logstream.hxx>
9 void SGEventMgr::add(const std::string& name, SGCallback* cb,
10 double interval, double delay,
11 bool repeat, bool simtime)
13 // Clamp the delay value to 1 usec, so that user code can use
14 // "zero" as a synonym for "next frame".
15 if(delay <= 0) delay = 0.000001;
17 SGTimer* t = new SGTimer;
18 t->interval = interval;
24 SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
40 void SGEventMgr::update(double delta_time_sec)
42 _simQueue.update(delta_time_sec);
44 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
48 void SGEventMgr::removeTask(const std::string& name)
50 SGTimer* t = _simQueue.findByName(name);
53 } else if ((t = _rtQueue.findByName(name))) {
56 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
60 // mark as not repeating so that the SGTimerQueue::update()
68 ////////////////////////////////////////////////////////////////////////
70 // This is the priority queue implementation:
71 ////////////////////////////////////////////////////////////////////////
73 SGTimerQueue::SGTimerQueue(int size)
78 while(size > _tableSize)
79 _tableSize = ((_tableSize + 1)<<1) - 1;
81 _table = new HeapEntry[_tableSize];
82 for(int i=0; i<_tableSize; i++) {
89 SGTimerQueue::~SGTimerQueue()
91 for(int i=0; i<_numEntries; i++) {
92 delete _table[i].timer;
101 void SGTimerQueue::update(double deltaSecs)
104 while(_numEntries && nextTime() <= _now) {
105 SGTimer* t = remove();
107 insert(t, t->interval);
108 // warning: this is not thread safe
109 // but the entire timer queue isn't either
118 void SGTimerQueue::insert(SGTimer* timer, double time)
120 if(_numEntries >= _tableSize)
124 _table[_numEntries-1].pri = -(_now + time);
125 _table[_numEntries-1].timer = timer;
127 siftUp(_numEntries-1);
130 SGTimer* SGTimerQueue::remove(SGTimer* t)
133 for(entry=0; entry<_numEntries; entry++)
134 if(_table[entry].timer == t)
136 if(entry == _numEntries)
139 // Swap in the last item in the table, and sift down
140 swap(entry, _numEntries-1);
147 SGTimer* SGTimerQueue::remove()
149 if(_numEntries == 0) {
151 } else if(_numEntries == 1) {
153 return _table[0].timer;
156 SGTimer *result = _table[0].timer;
157 _table[0] = _table[_numEntries - 1];
163 void SGTimerQueue::siftDown(int n)
165 // While we have children bigger than us, swap us with the biggest
167 while(lchild(n) < _numEntries) {
168 int bigc = lchild(n);
169 if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
171 if(pri(bigc) <= pri(n))
178 void SGTimerQueue::siftUp(int n)
180 while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
187 void SGTimerQueue::growArray()
189 _tableSize = ((_tableSize+1)<<1) - 1;
190 HeapEntry *newTable = new HeapEntry[_tableSize];
191 for(int i=0; i<_numEntries; i++) {
192 newTable[i].pri = _table[i].pri;
193 newTable[i].timer = _table[i].timer;
199 SGTimer* SGTimerQueue::findByName(const std::string& name) const
201 for (int i=0; i < _numEntries; ++i) {
202 if (_table[i].timer->name == name) {
203 return _table[i].timer;