]> SALOME platform Git repositories - modules/superv.git/blobdiff - src/GraphExecutor/DataFlowExecutor_FiniteStateMachine.cxx
Salome HOME
MPV: Merge V1_2d
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_FiniteStateMachine.cxx
index 544945580357759f1345efabfff1d4e523e6d0c5..e63271f2f4e9a49ea6c8b2cd75059b0a51fa676b 100644 (file)
@@ -451,6 +451,11 @@ GraphExecutor::FiniteStateMachine::FiniteStateMachine() {
     perror( msg ) ;
   }
 
+  pthread_mutex_init( &_MutexPythonWait , NULL ) ;
+  _ExecNumber = 0 ;
+  _GraphExecutingNumber = 0 ;
+  _PyInitModule = false ;
+
 //  cdebug_out << "FiniteStateMachine::FiniteStateMachine _TransitionTable "
 //             << endl;
 }
@@ -469,6 +474,117 @@ void * start_function( void *p ) {
   pthread_exit( PTHREAD_CANCELED ) ;
 }
 
+int GraphExecutor::FiniteStateMachine::ExecNumber() {
+  PyLock() ;
+  _ExecNumber += 1 ;
+  int RetVal = _ExecNumber ;
+  _GraphExecutingNumber += 1 ;
+  PyUnLock() ;
+  return RetVal ;
+}
+
+void GraphExecutor::FiniteStateMachine::Executed() {
+  PyLock() ;
+  _GraphExecutingNumber -= 1 ;
+  if ( _GraphExecutingNumber == 0 ) {
+    map< string , PyObject * >::iterator aPyFunction ;
+    for ( aPyFunction = _MapOfPyFunctions.begin() ; aPyFunction != _MapOfPyFunctions.end() ; aPyFunction++ ) {
+      if ( !strcmp( aPyFunction->first.c_str() , "PyObjRef" ) ||
+           !strcmp( aPyFunction->first.c_str() , "PyObjIor" ) ) {
+       //cout << "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " keeped ..."<< endl ;
+      }
+      else {
+       //cout << "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " erased ..."<< endl ;
+       _MapOfPyFunctions.erase( aPyFunction->first ) ;
+      }
+    }
+  }
+  else {
+    //cout << "GraphExecutor::FiniteStateMachine::Executed _GraphExecutingNumber " << _GraphExecutingNumber
+    //     << " != 0 ==> no erase" << endl ;
+  }
+  PyUnLock() ;
+  return ;
+}
+
+bool GraphExecutor::FiniteStateMachine::PyInitModule() {
+  bool InitedModule = _PyInitModule ;
+  _PyInitModule = true ;
+  return InitedModule ;
+}
+
+void GraphExecutor::FiniteStateMachine::PyLock() {
+//  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLock " << &_MutexPythonWait << endl ;
+  if ( pthread_mutex_lock( &_MutexPythonWait ) ) {
+    perror( "GraphExecutor::FiniteStateMachine::PyLock" ) ;
+    exit( 0 ) ;
+  }
+//  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLocked " << &_MutexPythonWait << endl ;
+}
+
+void GraphExecutor::FiniteStateMachine::PyUnLock() {
+//  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLock " << &_MutexPythonWait << endl ;
+  if ( pthread_mutex_unlock( &_MutexPythonWait ) ) {
+    perror( "GraphExecutor::FiniteStateMachine::PyUnLock" ) ;
+    exit( 0 ) ;
+  }
+//  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLocked " << &_MutexPythonWait << endl ;
+}
+
+PyObject * GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName ) {
+  
+  PyObject * RetVal = NULL ;
+  bool PyObjRefIor = !strcmp( aPyFuncName , "PyObjRef" ) || !strcmp( aPyFuncName , "PyObjIor" ) ;
+  PyObject * PyFunctionMapped = _MapOfPyFunctions[ aPyFuncName ] ;
+  if ( _GraphExecutingNumber > 1 && !PyObjRefIor ) {
+    RetVal = PyFunctionMapped ;
+    //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ;
+    if ( RetVal ) {
+      //cout << RetVal << " ob_refcnt " << RetVal->ob_refcnt ;
+    }
+    else {
+      //cout << " NULL" ;
+    }
+    //cout << endl ;
+  }
+  else {
+    RetVal = PyFunctionMapped ;
+    //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ;
+    if ( RetVal && PyObjRefIor ) {
+      //cout << RetVal << " " << RetVal->ob_refcnt << endl ;
+    }
+    else if ( RetVal ) {
+      //cout << RetVal << " " << RetVal->ob_refcnt << endl ;
+//      _MapOfPyFunctions.erase( aPyFuncName ) ;
+//      cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) erased --> NULL" << endl ;
+    }
+    else {
+      //cout << " NULL" << endl ;
+    }
+  }
+  
+  return RetVal ;
+}
+
+bool GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) {
+  
+  bool RetVal = false ;
+  if ( _MapOfPyFunctions[ aPyFuncName ] != NULL ) {
+    PyObject * aPyFunc = _MapOfPyFunctions[ aPyFuncName ] ;
+    //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , " << aPyFunction
+    //     << " ) ob_refcnt " << aPyFunction->ob_refcnt << " already mapped : " << aPyFunc << " ob_refcnt "
+    //     << aPyFunc->ob_refcnt << endl ;
+  }
+  else {
+    _MapOfPyFunctions[ aPyFuncName ] = aPyFunction ;
+    //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , " << aPyFunction
+    //     << " ) ob_refcnt " << aPyFunction->ob_refcnt << " mapped" << endl ;
+    RetVal = true ;
+  }
+  
+  return RetVal ;
+}
+
 void GraphExecutor::FiniteStateMachine::JoinThread() {
   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
     perror("Join pthread_mutex_lock ") ;