Bug 375: Ctrl-C does not always work when running Python simulations
This commit is contained in:
@@ -294,11 +294,6 @@ def register_Ns3Simulator_methods(root_module, cls):
|
||||
'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',
|
||||
|
||||
@@ -279,3 +279,50 @@ _wrap_CommandLine_AddValue(PyNs3CommandLine *self, PyObject *args, PyObject *kwa
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -287,6 +287,9 @@ def Simulator_customizations(module):
|
||||
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']
|
||||
|
||||
@@ -150,11 +150,11 @@ def pre_scan_hook(dummy_module_parser,
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user