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,8 +90,15 @@ int Abc_RealMain( int argc, char * argv[] ) ...@@ -90,8 +90,15 @@ int Abc_RealMain( int argc, char * argv[] )
init_pyabc(); init_pyabc();
pModule = PyImport_ImportModule("pyabc"); pModule = PyImport_ImportModule("pyabc");
if (pModule)
{
Py_DECREF(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 */
// default options // default options
......
...@@ -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();
...@@ -190,6 +178,7 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha ...@@ -190,6 +178,7 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha
PyObject* args; PyObject* args;
PyObject* arglist; PyObject* arglist;
PyObject* res; PyObject* res;
long lres; long lres;
if ( !pyabc_internal_python_command_callback ) if ( !pyabc_internal_python_command_callback )
...@@ -206,12 +195,23 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha ...@@ -206,12 +195,23 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha
res = PyEval_CallObject( pyabc_internal_python_command_callback, arglist ); res = PyEval_CallObject( pyabc_internal_python_command_callback, arglist );
Py_DECREF(arglist); Py_DECREF(arglist);
if ( !res )
{
return -1;
}
lres = PyInt_AsLong(res); lres = PyInt_AsLong(res);
Py_DECREF(res); Py_DECREF(res);
return lres; 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();
...@@ -219,12 +219,57 @@ void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges ...@@ -219,12 +219,57 @@ void pyabc_internal_register_command( char * sGroup, char * sName, int fChanges
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);
}
%} %}
%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,6 +297,11 @@ int n_phases(); ...@@ -252,6 +297,11 @@ 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
%{ %{
......
...@@ -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