]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/event_mgr.cxx
Reset: event manager can be unbound.
[simgear.git] / simgear / structure / event_mgr.cxx
index 6753a38f41de7cf5b22e0b3160911cefb9441f6e..b855fae2db941d9e16c58dbe4ba6ab5ff7ad388b 100644 (file)
@@ -1,6 +1,9 @@
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+
 #include "event_mgr.hxx"
 
-#include <simgear/math/SGMath.hxx>
 #include <simgear/debug/logstream.hxx>
 
 void SGEventMgr::add(const std::string& name, SGCallback* cb,
@@ -9,32 +12,36 @@ void SGEventMgr::add(const std::string& name, SGCallback* cb,
 {
     // Clamp the delay value to 1 usec, so that user code can use
     // "zero" as a synonym for "next frame".
-    if(delay <= 0) delay = 0.000001;
+    if(delay <= 0) delay = 1e-6;
+    if(interval <= 0) interval = 1e-6; // No timer endless loops please...
 
     SGTimer* t = new SGTimer;
     t->interval = interval;
     t->callback = cb;
-    t->mgr = this;
     t->repeat = repeat;
-    t->simtime = simtime;
     t->name = name;
+    t->running = false;
     
     SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
 
     q->insert(t, delay);
 }
 
+SGTimer::~SGTimer()
+{
+    delete callback;
+    callback = NULL;
+}
+
 void SGTimer::run()
 {
     (*callback)();
+}
 
-    if(repeat) {
-        SGTimerQueue* q = simtime ? &mgr->_simQueue : &mgr->_rtQueue;
-        q->insert(this, interval);
-    } else {
-        delete callback;
-        delete this;
-    }
+void SGEventMgr::unbind()
+{
+    _freezeProp.clear();
+    _rtProp.clear();
 }
 
 void SGEventMgr::update(double delta_time_sec)
@@ -56,8 +63,13 @@ void SGEventMgr::removeTask(const std::string& name)
     SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
     return;
   }
-  
-  delete t;
+  if (t->running) {
+    // mark as not repeating so that the SGTimerQueue::update()
+    // will clean it up
+    t->repeat = false;
+  } else {
+    delete t;
+  }
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -98,7 +110,15 @@ void SGTimerQueue::update(double deltaSecs)
     _now += deltaSecs;
     while(_numEntries && nextTime() <= _now) {
         SGTimer* t = remove();
+        if(t->repeat)
+            insert(t, t->interval);
+        // warning: this is not thread safe
+        // but the entire timer queue isn't either
+        t->running = true;
         t->run();
+        t->running = false;
+        if (!t->repeat)
+            delete t;
     }
 }
 
@@ -193,4 +213,3 @@ SGTimer* SGTimerQueue::findByName(const std::string& name) const
   
   return NULL;
 }
-