]> 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 eeefaac99f76e5be79648ed3e07189552acb8188..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,13 +12,15 @@ 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->repeat = repeat;
     t->name = name;
+    t->running = false;
     
     SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
 
@@ -33,6 +38,12 @@ void SGTimer::run()
     (*callback)();
 }
 
+void SGEventMgr::unbind()
+{
+    _freezeProp.clear();
+    _rtProp.clear();
+}
+
 void SGEventMgr::update(double delta_time_sec)
 {
     _simQueue.update(delta_time_sec);
@@ -52,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;
+  }
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -96,7 +112,11 @@ void SGTimerQueue::update(double deltaSecs)
         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;
     }