1 #include "event_mgr.hxx"
3 #include <simgear/math/SGMath.hxx>
4 #include <simgear/debug/logstream.hxx>
6 void SGEventMgr::add(const std::string& name, SGCallback* cb,
7 double interval, double delay,
8 bool repeat, bool simtime)
10 // Clamp the delay value to 1 usec, so that user code can use
11 // "zero" as a synonym for "next frame".
12 if(delay <= 0) delay = 0.000001;
14 SGTimer* t = new SGTimer;
15 t->interval = interval;
21 SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
37 void SGEventMgr::update(double delta_time_sec)
39 _simQueue.update(delta_time_sec);
41 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
45 void SGEventMgr::removeTask(const std::string& name)
47 SGTimer* t = _simQueue.findByName(name);
50 } else if ((t = _rtQueue.findByName(name))) {
53 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
57 // mark as not repeating so that the SGTimerQueue::update()
65 ////////////////////////////////////////////////////////////////////////
67 // This is the priority queue implementation:
68 ////////////////////////////////////////////////////////////////////////
70 SGTimerQueue::SGTimerQueue(int size)
75 while(size > _tableSize)
76 _tableSize = ((_tableSize + 1)<<1) - 1;
78 _table = new HeapEntry[_tableSize];
79 for(int i=0; i<_tableSize; i++) {
86 SGTimerQueue::~SGTimerQueue()
88 for(int i=0; i<_numEntries; i++) {
89 delete _table[i].timer;
98 void SGTimerQueue::update(double deltaSecs)
101 while(_numEntries && nextTime() <= _now) {
102 SGTimer* t = remove();
104 insert(t, t->interval);
105 // warning: this is not thread safe
106 // but the entire timer queue isn't either
115 void SGTimerQueue::insert(SGTimer* timer, double time)
117 if(_numEntries >= _tableSize)
121 _table[_numEntries-1].pri = -(_now + time);
122 _table[_numEntries-1].timer = timer;
124 siftUp(_numEntries-1);
127 SGTimer* SGTimerQueue::remove(SGTimer* t)
130 for(entry=0; entry<_numEntries; entry++)
131 if(_table[entry].timer == t)
133 if(entry == _numEntries)
136 // Swap in the last item in the table, and sift down
137 swap(entry, _numEntries-1);
144 SGTimer* SGTimerQueue::remove()
146 if(_numEntries == 0) {
148 } else if(_numEntries == 1) {
150 return _table[0].timer;
153 SGTimer *result = _table[0].timer;
154 _table[0] = _table[_numEntries - 1];
160 void SGTimerQueue::siftDown(int n)
162 // While we have children bigger than us, swap us with the biggest
164 while(lchild(n) < _numEntries) {
165 int bigc = lchild(n);
166 if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
168 if(pri(bigc) <= pri(n))
175 void SGTimerQueue::siftUp(int n)
177 while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
184 void SGTimerQueue::growArray()
186 _tableSize = ((_tableSize+1)<<1) - 1;
187 HeapEntry *newTable = new HeapEntry[_tableSize];
188 for(int i=0; i<_numEntries; i++) {
189 newTable[i].pri = _table[i].pri;
190 newTable[i].timer = _table[i].timer;
196 SGTimer* SGTimerQueue::findByName(const std::string& name) const
198 for (int i=0; i < _numEntries; ++i) {
199 if (_table[i].timer->name == name) {
200 return _table[i].timer;