Commit c85a7634 by Baruch Sterin

pyabc: make the SIGINT signal handler clean up by sending SIGINT to child…

pyabc: make the SIGINT signal handler clean up by sending SIGINT to child processes registered by the python code. Also provide functions to block SIGINT and unblock it, to allow for critical sections where signals are blocked
parent f4066b5b
...@@ -33,7 +33,7 @@ abcext.dsp ...@@ -33,7 +33,7 @@ abcext.dsp
src/python/build src/python/build
src/python/bdist src/python/bdist
src/python/pyabc.py src/python/pyabc.py
src.python/pyabc_wrap.* src/python/pyabc_wrap.*
syntax: regexp syntax: regexp
......
...@@ -90,7 +90,14 @@ int Abc_RealMain( int argc, char * argv[] ) ...@@ -90,7 +90,14 @@ int Abc_RealMain( int argc, char * argv[] )
init_pyabc(); init_pyabc();
pModule = PyImport_ImportModule("pyabc"); pModule = PyImport_ImportModule("pyabc");
Py_DECREF(pModule); if (pModule)
{
Py_DECREF(pModule);
}
else
{
fprintf( pAbc->Err, "error: pyabc.py not found. PYTHONPATH may not be set properly.\n");
}
} }
#endif /* ABC_PYTHON_EMBED */ #endif /* ABC_PYTHON_EMBED */
......
...@@ -25,12 +25,8 @@ ...@@ -25,12 +25,8 @@
#include <main.h> #include <main.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <hash.h>
void sigint_signal_handler(int sig) #include <hashPtr.h>
{
_exit(1);
}
int n_ands() int n_ands()
{ {
...@@ -85,14 +81,6 @@ int n_latches() ...@@ -85,14 +81,6 @@ int n_latches()
return -1; return -1;
} }
int run_command(char* cmd)
{
Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame();
int fStatus = Cmd_CommandExecute(pAbc, cmd);
return fStatus;
}
bool has_comb_model() bool has_comb_model()
{ {
Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame(); Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame();
...@@ -180,43 +168,99 @@ void pyabc_internal_set_command_callback( PyObject* callback ) ...@@ -180,43 +168,99 @@ void pyabc_internal_set_command_callback( PyObject* callback )
Py_XINCREF(callback); Py_XINCREF(callback);
Py_XDECREF(pyabc_internal_python_command_callback); Py_XDECREF(pyabc_internal_python_command_callback);
pyabc_internal_python_command_callback = callback; pyabc_internal_python_command_callback = callback;
} }
static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, char ** argv) static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, char ** argv)
{ {
int i; int i;
PyObject* args; PyObject* args;
PyObject* arglist; PyObject* arglist;
PyObject* res; PyObject* res;
long lres;
long lres;
if ( !pyabc_internal_python_command_callback )
return 0; if ( !pyabc_internal_python_command_callback )
return 0;
args = PyList_New(argc);
args = PyList_New(argc);
for( i=0 ; i<argc ; i++ )
PyList_SetItem(args, i, PyString_FromString(argv[i]) ); for( i=0 ; i<argc ; i++ )
PyList_SetItem(args, i, PyString_FromString(argv[i]) );
arglist = Py_BuildValue("(O)", args);
Py_INCREF(arglist); arglist = Py_BuildValue("(O)", args);
Py_INCREF(arglist);
res = PyEval_CallObject( pyabc_internal_python_command_callback, arglist );
Py_DECREF(arglist); res = PyEval_CallObject( pyabc_internal_python_command_callback, arglist );
Py_DECREF(arglist);
lres = PyInt_AsLong(res);
Py_DECREF(res); if ( !res )
{
return lres; return -1;
}
lres = PyInt_AsLong(res);
Py_DECREF(res);
return lres;
}
int run_command(char* cmd)
{
Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame();
return Cmd_CommandExecute(pAbc, cmd);
} }
void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges ) void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges )
{ {
Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame(); Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame();
Cmd_CommandAdd( pAbc, sGroup, sName, (void*)pyabc_internal_abc_command_callback, fChanges); Cmd_CommandAdd( pAbc, sGroup, sName, (void*)pyabc_internal_abc_command_callback, fChanges);
}
static Hash_Ptr_t* active_pid_hash = NULL;
void sigint_handler(int signum)
{
int i;
Hash_Ptr_Entry_t* pEntry;
assert( signum == SIGINT );
Hash_PtrForEachEntry(active_pid_hash, pEntry, i)
{
int pid = pEntry->key;
kill(pid, SIGINT);
}
_exit(1);
}
void add_child_pid(int pid)
{
Hash_PtrWriteEntry(active_pid_hash, pid, NULL);
}
void remove_child_pid(int pid)
{
Hash_PtrRemove(active_pid_hash, pid);
}
static sigset_t old_procmask;
void block_sigint()
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask(SIG_BLOCK, &set, &old_procmask);
}
void restore_sigint_block()
{
sigprocmask(SIG_SETMASK, &old_procmask, NULL);
} }
%} %}
...@@ -224,7 +268,8 @@ void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges ...@@ -224,7 +268,8 @@ void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges
%init %init
%{ %{
Abc_Start(); Abc_Start();
signal(SIGINT, sigint_signal_handler); active_pid_hash = Hash_PtrAlloc(1);
signal(SIGINT, sigint_handler);
%} %}
int n_ands(); int n_ands();
...@@ -252,31 +297,36 @@ int n_phases(); ...@@ -252,31 +297,36 @@ int n_phases();
void pyabc_internal_set_command_callback( PyObject* callback ); void pyabc_internal_set_command_callback( PyObject* callback );
void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges ); void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges );
void block_sigint();
void restore_sigint_block();
void add_child_pid(int pid);
void remove_child_pid(int pid);
%pythoncode %pythoncode
%{ %{
_registered_commands = {} _registered_commands = {}
def _cmd_callback(args): def _cmd_callback(args):
try: try:
assert len(args) > 0 assert len(args) > 0
cmd = args[0] cmd = args[0]
assert cmd in _registered_commands assert cmd in _registered_commands
res = _registered_commands[cmd](args) res = _registered_commands[cmd](args)
assert type(res) == int, "User-defined Python command must return an integer." assert type(res) == int, "User-defined Python command must return an integer."
return res return res
except Exception, e: except Exception, e:
print "Python error: ", e print "Python error: ", e
except SystemExit, se: except SystemExit, se:
pass pass
return 0 return 0
pyabc_internal_set_command_callback( _cmd_callback ) pyabc_internal_set_command_callback( _cmd_callback )
......
...@@ -6,6 +6,7 @@ from distutils import util ...@@ -6,6 +6,7 @@ from distutils import util
include_dirs = [ include_dirs = [
'../aig/hop', '../aig/hop',
'../aig/gia',
'../base/abc', '../base/abc',
'../base/cmd', '../base/cmd',
'../base/io', '../base/io',
...@@ -18,6 +19,7 @@ include_dirs = [ ...@@ -18,6 +19,7 @@ include_dirs = [
'../misc/st', '../misc/st',
'../misc/util', '../misc/util',
'../misc/vec', '../misc/vec',
'../misc/hash',
] ]
define_macros = [] define_macros = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment