Bug 375: Ctrl-C does not always work when running Python simulations
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Tue, 13 Jan 2009 19:28:26 +0000
changeset 4086 37dbf76b4c66
parent 4085 31945ecb17e2
child 4087 1c88eb82a9b0
Bug 375: Ctrl-C does not always work when running Python simulations
bindings/python/ns3_module_simulator.py
bindings/python/ns3module_helpers.cc
bindings/python/ns3modulegen_core_customizations.py
bindings/python/ns3modulescan.py
--- a/bindings/python/ns3_module_simulator.py	Tue Jan 13 19:27:44 2009 +0000
+++ b/bindings/python/ns3_module_simulator.py	Tue Jan 13 19:28:26 2009 +0000
@@ -294,11 +294,6 @@
                    'ns3::Time', 
                    [], 
                    is_static=True)
-    ## simulator.h: static void ns3::Simulator::Run() [member function]
-    cls.add_method('Run', 
-                   'void', 
-                   [], 
-                   is_static=True, unblock_threads=True)
     ## simulator.h: static void ns3::Simulator::RunOneEvent() [member function]
     cls.add_method('RunOneEvent', 
                    'void', 
--- a/bindings/python/ns3module_helpers.cc	Tue Jan 13 19:27:44 2009 +0000
+++ b/bindings/python/ns3module_helpers.cc	Tue Jan 13 19:28:26 2009 +0000
@@ -279,3 +279,50 @@
     return Py_None;
 }
 
+
+PyObject *
+_wrap_Simulator_Run(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs,
+                    PyObject **return_exception)
+{
+    const char *keywords[] = {"signal_check_frequency", NULL};
+    int signal_check_frequency = 100;
+    
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "|i", (char **) keywords, &signal_check_frequency)) {
+        PyObject *exc_type, *traceback;
+        PyErr_Fetch(&exc_type, return_exception, &traceback);
+        Py_XDECREF(exc_type);
+        Py_XDECREF(traceback);
+        return NULL;
+    }
+
+    PyThreadState *py_thread_state = NULL;
+
+    if (signal_check_frequency == -1)
+    {
+        if (PyEval_ThreadsInitialized ())
+            py_thread_state = PyEval_SaveThread();
+        ns3::Simulator::Run();
+        if (py_thread_state)
+            PyEval_RestoreThread(py_thread_state);
+    } else {
+        while (!ns3::Simulator::IsFinished())
+        {        
+            if (PyEval_ThreadsInitialized())
+                py_thread_state = PyEval_SaveThread();
+
+            for (int n = signal_check_frequency; n > 0 && !ns3::Simulator::IsFinished(); --n)
+            {        
+                ns3::Simulator::RunOneEvent();
+            }
+            
+            if (py_thread_state)
+                PyEval_RestoreThread(py_thread_state);
+            PyErr_CheckSignals();
+            if (PyErr_Occurred())
+                return NULL;
+        }
+    }
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
--- a/bindings/python/ns3modulegen_core_customizations.py	Tue Jan 13 19:27:44 2009 +0000
+++ b/bindings/python/ns3modulegen_core_customizations.py	Tue Jan 13 19:28:26 2009 +0000
@@ -287,6 +287,9 @@
     Simulator.add_custom_method_wrapper("ScheduleDestroy", "_wrap_Simulator_ScheduleDestroy",
                                         flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
 
+    Simulator.add_custom_method_wrapper("Run", "_wrap_Simulator_Run",
+                                        flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
+
 
 def CommandLine_customizations(module):
     CommandLine = module['ns3::CommandLine']
--- a/bindings/python/ns3modulescan.py	Tue Jan 13 19:27:44 2009 +0000
+++ b/bindings/python/ns3modulescan.py	Tue Jan 13 19:28:26 2009 +0000
@@ -150,11 +150,11 @@
             and pygccxml_definition.name.startswith('Schedule'):
         global_annotations['ignore'] = None
 
-    # unblock python threads for Simulator::Run
+    # manually wrapped
     if isinstance(pygccxml_definition, member_function_t) \
             and pygccxml_definition.parent.name == 'Simulator' \
             and pygccxml_definition.name == 'Run':
-        global_annotations['unblock_threads'] = True
+        global_annotations['ignore'] = True
 
 
     ## classes