]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.cxx
Reset: event manager can be unbound.
[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 void SGEventMgr::unbind()
42 {
43     _freezeProp.clear();
44     _rtProp.clear();
45 }
46
47 void SGEventMgr::update(double delta_time_sec)
48 {
49     _simQueue.update(delta_time_sec);
50     
51     double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
52     _rtQueue.update(rt);
53 }
54
55 void SGEventMgr::removeTask(const std::string& name)
56 {
57   SGTimer* t = _simQueue.findByName(name);
58   if (t) {
59     _simQueue.remove(t);
60   } else if ((t = _rtQueue.findByName(name))) {
61     _rtQueue.remove(t);
62   } else {
63     SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
64     return;
65   }
66   if (t->running) {
67     // mark as not repeating so that the SGTimerQueue::update()
68     // will clean it up
69     t->repeat = false;
70   } else {
71     delete t;
72   }
73 }
74
75 ////////////////////////////////////////////////////////////////////////
76 // SGTimerQueue
77 // This is the priority queue implementation:
78 ////////////////////////////////////////////////////////////////////////
79
80 SGTimerQueue::SGTimerQueue(int size)
81 {
82     _now = 0;
83     _numEntries = 0;
84     _tableSize = 1;
85     while(size > _tableSize)
86         _tableSize = ((_tableSize + 1)<<1) - 1;
87
88     _table = new HeapEntry[_tableSize];
89     for(int i=0; i<_tableSize; i++) {
90         _table[i].pri = 0;
91         _table[i].timer = 0;
92     }
93 }
94
95
96 SGTimerQueue::~SGTimerQueue()
97 {
98     for(int i=0; i<_numEntries; i++) {
99         delete _table[i].timer;
100         _table[i].timer = 0;
101     }
102     _numEntries = 0;
103     delete[] _table;
104     _table = 0;
105     _tableSize = 0;
106 }
107
108 void SGTimerQueue::update(double deltaSecs)
109 {
110     _now += deltaSecs;
111     while(_numEntries && nextTime() <= _now) {
112         SGTimer* t = remove();
113         if(t->repeat)
114             insert(t, t->interval);
115         // warning: this is not thread safe
116         // but the entire timer queue isn't either
117         t->running = true;
118         t->run();
119         t->running = false;
120         if (!t->repeat)
121             delete t;
122     }
123 }
124
125 void SGTimerQueue::insert(SGTimer* timer, double time)
126 {
127     if(_numEntries >= _tableSize)
128         growArray();
129
130     _numEntries++;
131     _table[_numEntries-1].pri = -(_now + time);
132     _table[_numEntries-1].timer = timer;
133
134     siftUp(_numEntries-1);
135 }
136
137 SGTimer* SGTimerQueue::remove(SGTimer* t)
138 {
139     int entry;
140     for(entry=0; entry<_numEntries; entry++)
141         if(_table[entry].timer == t)
142             break;
143     if(entry == _numEntries)
144         return 0;
145
146     // Swap in the last item in the table, and sift down
147     swap(entry, _numEntries-1);
148     _numEntries--;
149     siftDown(entry);
150
151     return t;
152 }
153
154 SGTimer* SGTimerQueue::remove()
155 {
156     if(_numEntries == 0) {
157         return 0;
158     } else if(_numEntries == 1) {
159         _numEntries = 0;
160         return _table[0].timer;
161     }
162
163     SGTimer *result = _table[0].timer;
164     _table[0] = _table[_numEntries - 1];
165     _numEntries--;
166     siftDown(0);
167     return result;
168 }
169
170 void SGTimerQueue::siftDown(int n)
171 {
172     // While we have children bigger than us, swap us with the biggest
173     // child.
174     while(lchild(n) < _numEntries) {
175         int bigc = lchild(n);
176         if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
177             bigc = rchild(n);
178         if(pri(bigc) <= pri(n))
179             break;
180         swap(n, bigc);
181         n = bigc;
182     }
183 }
184
185 void SGTimerQueue::siftUp(int n)
186 {
187     while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
188         swap(n, parent(n));
189         n = parent(n);
190     }
191     siftDown(n);
192 }
193
194 void SGTimerQueue::growArray()
195 {
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;
201     }
202     delete[] _table;
203     _table = newTable;
204 }
205
206 SGTimer* SGTimerQueue::findByName(const std::string& name) const
207 {
208   for (int i=0; i < _numEntries; ++i) {
209     if (_table[i].timer->name == name) {
210       return _table[i].timer;
211     }
212   }
213   
214   return NULL;
215 }