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 = 1e-6;
16 if(interval <= 0) interval = 1e-6; // No timer endless loops please...
18 SGTimer* t = new SGTimer;
19 t->interval = interval;
25 SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
41 SGEventMgr::SGEventMgr()
46 SGEventMgr::~SGEventMgr()
51 void SGEventMgr::unbind()
57 void SGEventMgr::update(double delta_time_sec)
59 _simQueue.update(delta_time_sec);
61 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
65 void SGEventMgr::removeTask(const std::string& name)
67 SGTimer* t = _simQueue.findByName(name);
70 } else if ((t = _rtQueue.findByName(name))) {
73 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
77 // mark as not repeating so that the SGTimerQueue::update()
85 ////////////////////////////////////////////////////////////////////////
87 // This is the priority queue implementation:
88 ////////////////////////////////////////////////////////////////////////
90 SGTimerQueue::SGTimerQueue(int size)
95 while(size > _tableSize)
96 _tableSize = ((_tableSize + 1)<<1) - 1;
98 _table = new HeapEntry[_tableSize];
99 for(int i=0; i<_tableSize; i++) {
106 SGTimerQueue::~SGTimerQueue()
108 for(int i=0; i<_numEntries; i++) {
109 delete _table[i].timer;
118 void SGTimerQueue::update(double deltaSecs)
121 while(_numEntries && nextTime() <= _now) {
122 SGTimer* t = remove();
124 insert(t, t->interval);
125 // warning: this is not thread safe
126 // but the entire timer queue isn't either
135 void SGTimerQueue::insert(SGTimer* timer, double time)
137 if(_numEntries >= _tableSize)
141 _table[_numEntries-1].pri = -(_now + time);
142 _table[_numEntries-1].timer = timer;
144 siftUp(_numEntries-1);
147 SGTimer* SGTimerQueue::remove(SGTimer* t)
150 for(entry=0; entry<_numEntries; entry++)
151 if(_table[entry].timer == t)
153 if(entry == _numEntries)
156 // Swap in the last item in the table, and sift down
157 swap(entry, _numEntries-1);
164 SGTimer* SGTimerQueue::remove()
166 if(_numEntries == 0) {
168 } else if(_numEntries == 1) {
170 return _table[0].timer;
173 SGTimer *result = _table[0].timer;
174 _table[0] = _table[_numEntries - 1];
180 void SGTimerQueue::siftDown(int n)
182 // While we have children bigger than us, swap us with the biggest
184 while(lchild(n) < _numEntries) {
185 int bigc = lchild(n);
186 if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
188 if(pri(bigc) <= pri(n))
195 void SGTimerQueue::siftUp(int n)
197 while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
204 void SGTimerQueue::growArray()
206 _tableSize = ((_tableSize+1)<<1) - 1;
207 HeapEntry *newTable = new HeapEntry[_tableSize];
208 for(int i=0; i<_numEntries; i++) {
209 newTable[i].pri = _table[i].pri;
210 newTable[i].timer = _table[i].timer;
216 SGTimer* SGTimerQueue::findByName(const std::string& name) const
218 for (int i=0; i < _numEntries; ++i) {
219 if (_table[i].timer->name == name) {
220 return _table[i].timer;