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 void SGEventMgr::unbind()
47 void SGEventMgr::update(double delta_time_sec)
49 _simQueue.update(delta_time_sec);
51 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
55 void SGEventMgr::removeTask(const std::string& name)
57 SGTimer* t = _simQueue.findByName(name);
60 } else if ((t = _rtQueue.findByName(name))) {
63 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
67 // mark as not repeating so that the SGTimerQueue::update()
75 ////////////////////////////////////////////////////////////////////////
77 // This is the priority queue implementation:
78 ////////////////////////////////////////////////////////////////////////
80 SGTimerQueue::SGTimerQueue(int size)
85 while(size > _tableSize)
86 _tableSize = ((_tableSize + 1)<<1) - 1;
88 _table = new HeapEntry[_tableSize];
89 for(int i=0; i<_tableSize; i++) {
96 SGTimerQueue::~SGTimerQueue()
98 for(int i=0; i<_numEntries; i++) {
99 delete _table[i].timer;
108 void SGTimerQueue::update(double deltaSecs)
111 while(_numEntries && nextTime() <= _now) {
112 SGTimer* t = remove();
114 insert(t, t->interval);
115 // warning: this is not thread safe
116 // but the entire timer queue isn't either
125 void SGTimerQueue::insert(SGTimer* timer, double time)
127 if(_numEntries >= _tableSize)
131 _table[_numEntries-1].pri = -(_now + time);
132 _table[_numEntries-1].timer = timer;
134 siftUp(_numEntries-1);
137 SGTimer* SGTimerQueue::remove(SGTimer* t)
140 for(entry=0; entry<_numEntries; entry++)
141 if(_table[entry].timer == t)
143 if(entry == _numEntries)
146 // Swap in the last item in the table, and sift down
147 swap(entry, _numEntries-1);
154 SGTimer* SGTimerQueue::remove()
156 if(_numEntries == 0) {
158 } else if(_numEntries == 1) {
160 return _table[0].timer;
163 SGTimer *result = _table[0].timer;
164 _table[0] = _table[_numEntries - 1];
170 void SGTimerQueue::siftDown(int n)
172 // While we have children bigger than us, swap us with the biggest
174 while(lchild(n) < _numEntries) {
175 int bigc = lchild(n);
176 if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
178 if(pri(bigc) <= pri(n))
185 void SGTimerQueue::siftUp(int n)
187 while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
194 void SGTimerQueue::growArray()
196 _tableSize = ((_tableSize+1)<<1) - 1;
197 HeapEntry *newTable = new HeapEntry[_tableSize];
198 for(int i=0; i<_numEntries; i++) {
199 newTable[i].pri = _table[i].pri;
200 newTable[i].timer = _table[i].timer;
206 SGTimer* SGTimerQueue::findByName(const std::string& name) const
208 for (int i=0; i < _numEntries; ++i) {
209 if (_table[i].timer->name == name) {
210 return _table[i].timer;