diff options
author | Baruch Sterin <baruchs@gmail.com> | 2011-01-13 22:11:37 +0200 |
---|---|---|
committer | Baruch Sterin <baruchs@gmail.com> | 2011-01-13 22:11:37 +0200 |
commit | c85a763444f696e0125445b5adc675020d736c5d (patch) | |
tree | 03d28593a06c5549949e35106939589ef05cb853 | |
parent | f4066b5be3b5473f5c64ab71d1983df6ca7aec76 (diff) | |
download | abc-c85a763444f696e0125445b5adc675020d736c5d.tar.gz abc-c85a763444f696e0125445b5adc675020d736c5d.tar.bz2 abc-c85a763444f696e0125445b5adc675020d736c5d.zip |
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
-rw-r--r-- | .hgignore | 2 | ||||
-rw-r--r-- | src/base/main/main.c | 9 | ||||
-rw-r--r-- | src/python/pyabc.i | 172 | ||||
-rw-r--r-- | src/python/setup.py | 2 |
4 files changed, 122 insertions, 63 deletions
@@ -33,7 +33,7 @@ abcext.dsp src/python/build src/python/bdist src/python/pyabc.py -src.python/pyabc_wrap.* +src/python/pyabc_wrap.* syntax: regexp diff --git a/src/base/main/main.c b/src/base/main/main.c index 42ea255d..0b6294c4 100644 --- a/src/base/main/main.c +++ b/src/base/main/main.c @@ -90,7 +90,14 @@ int Abc_RealMain( int argc, char * argv[] ) init_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 */ diff --git a/src/python/pyabc.i b/src/python/pyabc.i index 9dabc3db..56ef5cb0 100644 --- a/src/python/pyabc.i +++ b/src/python/pyabc.i @@ -25,12 +25,8 @@ #include <main.h> #include <stdlib.h> #include <signal.h> - -void sigint_signal_handler(int sig) -{ - _exit(1); -} - +#include <hash.h> +#include <hashPtr.h> int n_ands() { @@ -85,14 +81,6 @@ int n_latches() 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() { Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame(); @@ -180,43 +168,99 @@ void pyabc_internal_set_command_callback( PyObject* callback ) Py_XINCREF(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) { - int i; - - PyObject* args; - PyObject* arglist; - PyObject* res; - long lres; - - if ( !pyabc_internal_python_command_callback ) - return 0; - - args = PyList_New(argc); - - for( i=0 ; i<argc ; i++ ) - PyList_SetItem(args, i, PyString_FromString(argv[i]) ); - - arglist = Py_BuildValue("(O)", args); - Py_INCREF(arglist); - - res = PyEval_CallObject( pyabc_internal_python_command_callback, arglist ); - Py_DECREF(arglist); - - lres = PyInt_AsLong(res); - Py_DECREF(res); - - return lres; + int i; + + PyObject* args; + PyObject* arglist; + PyObject* res; + + long lres; + + if ( !pyabc_internal_python_command_callback ) + return 0; + + args = PyList_New(argc); + + for( i=0 ; i<argc ; i++ ) + PyList_SetItem(args, i, PyString_FromString(argv[i]) ); + + arglist = Py_BuildValue("(O)", args); + Py_INCREF(arglist); + + res = PyEval_CallObject( pyabc_internal_python_command_callback, arglist ); + Py_DECREF(arglist); + + if ( !res ) + { + 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 ) { 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 %init %{ Abc_Start(); - signal(SIGINT, sigint_signal_handler); + active_pid_hash = Hash_PtrAlloc(1); + signal(SIGINT, sigint_handler); %} int n_ands(); @@ -252,31 +297,36 @@ int n_phases(); void pyabc_internal_set_command_callback( PyObject* callback ); 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 %{ _registered_commands = {} def _cmd_callback(args): - try: - assert len(args) > 0 - - cmd = args[0] - assert cmd in _registered_commands - - res = _registered_commands[cmd](args) - - assert type(res) == int, "User-defined Python command must return an integer." - - return res - - except Exception, e: - print "Python error: ", e - - except SystemExit, se: - pass - - return 0 + try: + assert len(args) > 0 + + cmd = args[0] + assert cmd in _registered_commands + + res = _registered_commands[cmd](args) + + assert type(res) == int, "User-defined Python command must return an integer." + + return res + + except Exception, e: + print "Python error: ", e + + except SystemExit, se: + pass + + return 0 pyabc_internal_set_command_callback( _cmd_callback ) diff --git a/src/python/setup.py b/src/python/setup.py index abc07afa..af2f5547 100644 --- a/src/python/setup.py +++ b/src/python/setup.py @@ -6,6 +6,7 @@ from distutils import util include_dirs = [ '../aig/hop', + '../aig/gia', '../base/abc', '../base/cmd', '../base/io', @@ -18,6 +19,7 @@ include_dirs = [ '../misc/st', '../misc/util', '../misc/vec', + '../misc/hash', ] define_macros = [] |