]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.cxx
37ef4e13bdc18d5082d77e4a95e97b80d1a18d8a
[simgear.git] / simgear / structure / event_mgr.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <simgear_config.h>
3 #endif
4
5 #include "event_mgr.hxx"
6
7 #include <simgear/debug/logstream.hxx>
8
9 void SGEventMgr::add(const std::string& name, SGCallback* cb,
10                      double interval, double delay,
11                      bool repeat, bool simtime)
12 {
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...
17
18     SGTimer* t = new SGTimer;
19     t->interval = interval;
20     t->callback = cb;
21     t->repeat = repeat;
22     t->name = name;
23     t->running = false;
24     
25     SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
26
27     q->insert(t, delay);
28 }
29
30 SGTimer::~SGTimer()
31 {
32     delete callback;
33     callback = NULL;
34 }
35
36 void SGTimer::run()
37 {
38     (*callback)();
39 }
40
41 SGEventMgr::SGEventMgr()
42 {
43     
44 }
45
46 SGEventMgr::~SGEventMgr()
47 {
48     
49 }
50
51 void SGEventMgr::unbind()
52 {
53     _freezeProp.clear();
54     _rtProp.clear();
55 }
56
57 void SGEventMgr::update(double delta_time_sec)
58 {
59     _simQueue.update(delta_time_sec);
60     
61     double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
62     _rtQueue.update(rt);
63 }
64
65 void SGEventMgr::removeTask(const std::string& name)
66 {
67   SGTimer* t = _simQueue.findByName(name);
68   if (t) {
69     _simQueue.remove(t);
70   } else if ((t = _rtQueue.findByName(name))) {
71     _rtQueue.remove(t);
72   } else {
73     SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
74     return;
75   }
76   if (t->running) {
77     // mark as not repeating so that the SGTimerQueue::update()
78     // will clean it up
79     t->repeat = false;
80   } else {
81     delete t;
82   }
83 }
84
85 ////////////////////////////////////////////////////////////////////////
86 // SGTimerQueue
87 // This is the priority queue implementation:
88 ////////////////////////////////////////////////////////////////////////
89
90 SGTimerQueue::SGTimerQueue(int size)
91 {
92     _now = 0;
93     _numEntries = 0;
94     _tableSize = 1;
95     while(size > _tableSize)
96         _tableSize = ((_tableSize + 1)<<1) - 1;
97
98     _table = new HeapEntry[_tableSize];
99     for(int i=0; i<_tableSize; i++) {
100         _table[i].pri = 0;
101         _table[i].timer = 0;
102     }
103 }
104
105
106 SGTimerQueue::~SGTimerQueue()
107 {
108     for(int i=0; i<_numEntries; i++) {
109         delete _table[i].timer;
110         _table[i].timer = 0;
111     }
112     _numEntries = 0;
113     delete[] _table;
114     _table = 0;
115     _tableSize = 0;
116 }
117
118 void SGTimerQueue::update(double deltaSecs)
119 {
120     _now += deltaSecs;
121     while(_numEntries && nextTime() <= _now) {
122         SGTimer* t = remove();
123         if(t->repeat)
124             insert(t, t->interval);
125         // warning: this is not thread safe
126         // but the entire timer queue isn't either
127         t->running = true;
128         t->run();
129         t->running = false;
130         if (!t->repeat)
131             delete t;
132     }
133 }
134
135 void SGTimerQueue::insert(SGTimer* timer, double time)
136 {
137     if(_numEntries >= _tableSize)
138         growArray();
139
140     _numEntries++;
141     _table[_numEntries-1].pri = -(_now + time);
142     _table[_numEntries-1].timer = timer;
143
144     siftUp(_numEntries-1);
145 }
146
147 SGTimer* SGTimerQueue::remove(SGTimer* t)
148 {
149     int entry;
150     for(entry=0; entry<_numEntries; entry++)
151         if(_table[entry].timer == t)
152             break;
153     if(entry == _numEntries)
154         return 0;
155
156     // Swap in the last item in the table, and sift down
157     swap(entry, _numEntries-1);
158     _numEntries--;
159     siftDown(entry);
160
161     return t;
162 }
163
164 SGTimer* SGTimerQueue::remove()
165 {
166     if(_numEntries == 0) {
167         return 0;
168     } else if(_numEntries == 1) {
169         _numEntries = 0;
170         return _table[0].timer;
171     }
172
173     SGTimer *result = _table[0].timer;
174     _table[0] = _table[_numEntries - 1];
175     _numEntries--;
176     siftDown(0);
177     return result;
178 }
179
180 void SGTimerQueue::siftDown(int n)
181 {
182     // While we have children bigger than us, swap us with the biggest
183     // child.
184     while(lchild(n) < _numEntries) {
185         int bigc = lchild(n);
186         if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
187             bigc = rchild(n);
188         if(pri(bigc) <= pri(n))
189             break;
190         swap(n, bigc);
191         n = bigc;
192     }
193 }
194
195 void SGTimerQueue::siftUp(int n)
196 {
197     while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
198         swap(n, parent(n));
199         n = parent(n);
200     }
201     siftDown(n);
202 }
203
204 void SGTimerQueue::growArray()
205 {
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;
211     }
212     delete[] _table;
213     _table = newTable;
214 }
215
216 SGTimer* SGTimerQueue::findByName(const std::string& name) const
217 {
218   for (int i=0; i < _numEntries; ++i) {
219     if (_table[i].timer->name == name) {
220       return _table[i].timer;
221     }
222   }
223   
224   return NULL;
225 }