2 # include <simgear_config.h>
5 #include "event_mgr.hxx"
7 #include <simgear/math/SGMath.hxx>
8 #include <simgear/debug/logstream.hxx>
10 void SGEventMgr::add(const std::string& name, SGCallback* cb,
11 double interval, double delay,
12 bool repeat, bool simtime)
14 // Clamp the delay value to 1 usec, so that user code can use
15 // "zero" as a synonym for "next frame".
16 if(delay <= 0) delay = 0.000001;
18 SGTimer* t = new SGTimer;
19 t->interval = interval;
25 SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
41 void SGEventMgr::update(double delta_time_sec)
43 _simQueue.update(delta_time_sec);
45 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
49 void SGEventMgr::removeTask(const std::string& name)
51 SGTimer* t = _simQueue.findByName(name);
54 } else if ((t = _rtQueue.findByName(name))) {
57 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
61 // mark as not repeating so that the SGTimerQueue::update()
69 ////////////////////////////////////////////////////////////////////////
71 // This is the priority queue implementation:
72 ////////////////////////////////////////////////////////////////////////
74 SGTimerQueue::SGTimerQueue(int size)
79 while(size > _tableSize)
80 _tableSize = ((_tableSize + 1)<<1) - 1;
82 _table = new HeapEntry[_tableSize];
83 for(int i=0; i<_tableSize; i++) {
90 SGTimerQueue::~SGTimerQueue()
92 for(int i=0; i<_numEntries; i++) {
93 delete _table[i].timer;
102 void SGTimerQueue::update(double deltaSecs)
105 while(_numEntries && nextTime() <= _now) {
106 SGTimer* t = remove();
108 insert(t, t->interval);
109 // warning: this is not thread safe
110 // but the entire timer queue isn't either
119 void SGTimerQueue::insert(SGTimer* timer, double time)
121 if(_numEntries >= _tableSize)
125 _table[_numEntries-1].pri = -(_now + time);
126 _table[_numEntries-1].timer = timer;
128 siftUp(_numEntries-1);
131 SGTimer* SGTimerQueue::remove(SGTimer* t)
134 for(entry=0; entry<_numEntries; entry++)
135 if(_table[entry].timer == t)
137 if(entry == _numEntries)
140 // Swap in the last item in the table, and sift down
141 swap(entry, _numEntries-1);
148 SGTimer* SGTimerQueue::remove()
150 if(_numEntries == 0) {
152 } else if(_numEntries == 1) {
154 return _table[0].timer;
157 SGTimer *result = _table[0].timer;
158 _table[0] = _table[_numEntries - 1];
164 void SGTimerQueue::siftDown(int n)
166 // While we have children bigger than us, swap us with the biggest
168 while(lchild(n) < _numEntries) {
169 int bigc = lchild(n);
170 if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
172 if(pri(bigc) <= pri(n))
179 void SGTimerQueue::siftUp(int n)
181 while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
188 void SGTimerQueue::growArray()
190 _tableSize = ((_tableSize+1)<<1) - 1;
191 HeapEntry *newTable = new HeapEntry[_tableSize];
192 for(int i=0; i<_numEntries; i++) {
193 newTable[i].pri = _table[i].pri;
194 newTable[i].timer = _table[i].timer;
200 SGTimer* SGTimerQueue::findByName(const std::string& name) const
202 for (int i=0; i < _numEntries; ++i) {
203 if (_table[i].timer->name == name) {
204 return _table[i].timer;