]> SALOME platform Git repositories - modules/superv.git/blobdiff - src/GraphExecutor/DataFlowExecutor_InNode.cxx
Salome HOME
MPV: Merge V1_2d
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_InNode.cxx
index 8663bad398e2d1a1ff9f102c7881f8798db16078..45ebcad37193a08a3b81db394c2a531ddf8dfeab 100644 (file)
@@ -68,6 +68,8 @@ static void InitInNode( int &_RewindStack ,
                         bool &_ResumeSync ,
                         pthread_cond_t &_KillWait ,
                         bool &_KillSync ,
+                        pthread_cond_t &_ThreadStartedWait ,
+                        bool &_ThreadStartedSync ,
                         pthread_cond_t &_StopWait ,
                         GraphExecutor::FiniteStateMachine ** _Automaton ,
                         GraphExecutor::FiniteStateMachine * theAutomaton ,
@@ -113,6 +115,11 @@ static void InitInNode( int &_RewindStack ,
     exit( 0 ) ;
   }
   _KillSync = false ;
+  if ( pthread_cond_init( &_ThreadStartedWait , NULL ) ) {
+    perror("pthread_cond_init( &_ThreadStartedWait , NULL )") ;
+    exit( 0 ) ;
+  }
+  _ThreadStartedSync = false ;
   if ( pthread_cond_init( &_StopWait , NULL ) ) {
     perror("pthread_cond_init( &_StopWait , NULL )") ;
     exit( 0 ) ;
@@ -121,8 +128,7 @@ static void InitInNode( int &_RewindStack ,
   *_Orb = CORBA::ORB::_nil();
 }
 
-GraphExecutor::FiniteStateMachine * theAutomaton = new
-                                        GraphExecutor::FiniteStateMachine() ;
+GraphExecutor::FiniteStateMachine * theAutomaton = new GraphExecutor::FiniteStateMachine() ;
 
 //GraphExecutor::InNode::InNode() :
 //     GraphBase::FactoryNode() {
@@ -146,6 +152,8 @@ GraphExecutor::InNode::InNode() {
               _ResumeSync ,
               _KillWait ,
               _KillSync ,
+              _ThreadStartedWait ,
+              _ThreadStartedSync ,
               _StopWait ,
               &_Automaton ,
               theAutomaton ,
@@ -153,26 +161,26 @@ GraphExecutor::InNode::InNode() {
               CORBA::ORB::_nil() ) ;
 }
 
-GraphExecutor::InNode::InNode(CORBA::ORB_ptr ORB,
-               SALOME_NamingService* ptrNamingService ,
-               const SALOME_ModuleCatalog::Service& aService ,
-               const char * ComponentName ,
-               const char * NodeInterfaceName ,
-               const char * NodeName ,
-               const SUPERV::KindOfNode akind ,
-               GraphBase::ListOfFuncName aFuncName ,
-               GraphBase::ListOfPythonFunctions aPythonFunction ,
-               const SUPERV::SDate NodeFirstCreation ,
-               const SUPERV::SDate NodeLastModification  ,
-               const char * NodeEditorRelease ,
-               const char * NodeAuthor ,
-               const char * NodeComputer ,
-               const char * NodeComment ,
-               const bool   GeneratedName ,
-               const int NodeX ,
-               const int NodeY ,
-               int * Graph_prof_debug,
-               ofstream * Graph_fdebug) {
+GraphExecutor::InNode::InNode( CORBA::ORB_ptr ORB,
+                               SALOME_NamingService* ptrNamingService ,
+                               const SALOME_ModuleCatalog::Service& aService ,
+                               const char * ComponentName ,
+                               const char * NodeInterfaceName ,
+                               const char * NodeName ,
+                               const SUPERV::KindOfNode akind ,
+                               GraphBase::ListOfFuncName aFuncName ,
+                               GraphBase::ListOfPythonFunctions aPythonFunction ,
+                               const SUPERV::SDate NodeFirstCreation ,
+                               const SUPERV::SDate NodeLastModification  ,
+                               const char * NodeEditorRelease ,
+                               const char * NodeAuthor ,
+                               const char * NodeComputer ,
+                               const char * NodeComment ,
+                               const bool   GeneratedName ,
+                               const int NodeX ,
+                               const int NodeY ,
+                               int * Graph_prof_debug,
+                               ofstream * Graph_fdebug) {
 //               ostream * Graph_fdebug = NULL ) :
 //             GraphBase::FactoryNode( ORB , ptrNamingService , aService ,
 //                                     ComponentName , NodeInterfaceName ,
@@ -201,6 +209,8 @@ GraphExecutor::InNode::InNode(CORBA::ORB_ptr ORB,
               _ResumeSync ,
               _KillWait ,
               _KillSync ,
+              _ThreadStartedWait ,
+              _ThreadStartedSync ,
               _StopWait ,
               &_Automaton ,
               theAutomaton ,
@@ -425,6 +435,9 @@ bool GraphExecutor::InNode::Suspend() {
   bool RetVal ;
   if ( IsDone() ) {
     ControlState( SUPERV::VoidState ) ;
+    if ( _OutNode->IsDone() ) {
+      ControlState( SUPERV::VoidState ) ;
+    }
     RetVal = false ;
   }
   else if ( IsWaiting() || IsReady() ) {
@@ -433,12 +446,16 @@ bool GraphExecutor::InNode::Suspend() {
   }
   else  if ( IsRunning() ) {
     ControlState( SUPERV::ToSuspendState ) ;
-    if ( IsFactoryNode() ) {
+    if ( IsFactoryNode() || IsComputingNode() ) {
       if ( !CORBA::is_nil( Component() ) ) {
         RetVal = Component()->Suspend_impl() ;
         if ( RetVal ) {
           if ( IsRunning() ) {
+            cdebug << pthread_self() << "GraphExecutor::InNode::Suspend_impl " << Name()
+                   << " --> thread" << ThreadNo() << " SuspendEvent " << endl;
             SendEvent( GraphExecutor::SuspendEvent ) ;
+            cdebug << pthread_self() << "GraphExecutor::InNode::Suspended_impl in Container"
+                   << Name() << " --> thread" << ThreadNo() << endl;
           }
           else if ( IsDone() ) {
             ControlState( SUPERV::VoidState ) ;
@@ -465,7 +482,8 @@ bool GraphExecutor::InNode::Suspend() {
            << endl ;
     RetVal = false ;
   }
-  cdebug_out << "GraphExecutor::InNode::Suspend " << RetVal << endl ;
+  cdebug_out << "GraphExecutor::InNode::Suspend " << RetVal << " "
+             << Automaton()->StateName( State() ) << endl ;
   return RetVal ;
 }
 
@@ -475,7 +493,7 @@ bool GraphExecutor::InNode::ContainerKill() {
   bool RetVal ;
   if ( IsFactoryNode() ) {
     Kill() ;
-    Container()->Kill_impl() ;
+    RetVal = Container()->Kill_impl() ;
   }
   cdebug_out << "GraphExecutor::InNode::ContainerKill" << endl ;
   return RetVal ;
@@ -485,47 +503,54 @@ bool GraphExecutor::InNode::Kill() {
   cdebug_in << "GraphExecutor::InNode::Kill " << Name() << " " << ThreadNo() << " " 
             << Automaton()->StateName( State() ) << endl;
   bool RetVal ;
-  if ( ControlState() == SUPERV::ToKillState || IsDone() ) {
+  if ( IsDone() ) {
+    ControlState( SUPERV::ToKillState ) ; // if loop
+    if ( _OutNode->IsDone() ) {
+      ControlState( SUPERV::VoidState ) ;
+    }
     RetVal = false ;
   }
   else {
     ControlState( SUPERV::ToKillState ) ;
     if ( IsDone() ) {
-      ControlState( SUPERV::VoidState ) ;
+      if ( _OutNode->IsDone() ) {
+        ControlState( SUPERV::VoidState ) ;
+      }
       RetVal = false ;
     }
     else {
       if ( IsRunning() ) {
-        if ( IsFactoryNode() ) {
+        if ( IsFactoryNode() || IsComputingNode() ) {
           if ( !CORBA::is_nil( Component() ) ) {
             RetVal = Component()->Kill_impl() ;
-            if ( RetVal ) {
-              if ( IsRunning() ) {
-                SendEvent( GraphExecutor::KillEvent ) ;
-             }
-              else if ( IsDone() ) {
-                ControlState( SUPERV::VoidState ) ;
-                RetVal = false ; // Too late ...
-             }
-              else {
-                cdebug << "component Killed and !IsDone and !IsRunning !"
-                       << endl ;
-             }
+            cdebug << "Component()->Kill_impl() returns status " << RetVal << endl ;
+            RetVal = true ;
+            if ( IsRunning() ) {
+              cdebug << pthread_self() << "GraphExecutor::InNode::Kill_impl " << Name()
+                     << " --> thread" << ThreadNo() << " SuspendEvent " << endl;
+              SendEvent( GraphExecutor::KillEvent ) ;
+              cdebug << pthread_self() << "GraphExecutor::InNode::Killed_impl in Container"
+                     << Name() << " --> thread" << ThreadNo() << endl;
+           }
+            else if ( IsDone() ) {
+              ControlState( SUPERV::VoidState ) ;
+              RetVal = false ; // Too late ...
+           }
+            else {
+              cdebug << "component Killed and !IsDone and !IsRunning !"
+                     << endl ;
            }
          }
           else {
-            cdebug << "Kill cannot Kill component !" << endl ;
+            cdebug << "Kill with nilComponent cannot Kill component !" << endl ;
             RetVal = false ;
          }
        }
-        else {
-          cdebug << "Kill with nilComponent while RunningState !" << endl ;
-          SendEvent( GraphExecutor::KillEvent ) ;
-          RetVal = IsKilled() ;
-        }
       }
       else if ( IsSuspended() ) {
-        SendEvent( GraphExecutor::KillEvent ) ;
+        cdebug << pthread_self() << "GraphExecutor::InNode::Kill " << Name()
+               << " --> thread" << ThreadNo() << " Resume()" << endl;
+        Resume() ;
         RetVal = true ;
       }
       else if ( IsWaiting() ) {
@@ -552,43 +577,20 @@ bool GraphExecutor::InNode::KillDone() {
   else {
     ControlState( SUPERV::ToKillDoneState ) ;
     if ( IsDone() ) {
-      ControlState( SUPERV::VoidState ) ;
+      if ( _OutNode->IsDone() ) {
+        ControlState( SUPERV::VoidState ) ;
+      }
       RetVal = false ;
     }
     else {
       if ( IsRunning() ) {
-        if ( IsFactoryNode() ) {
-          if ( !CORBA::is_nil( Component() ) ) {
-            RetVal = Component()->Kill_impl() ;
-            if ( RetVal ) {
-              if ( IsRunning() ) {
-                SendEvent( GraphExecutor::SuspendEvent ) ;
-             }
-              else if ( IsDone() ) {
-                ControlState( SUPERV::VoidState ) ;
-                RetVal = false ; // Too late ...
-             }
-              else {
-                cdebug << "component Suspended and !IsDone and !IsRunning !"
-                       << endl ;
-             }
-           }
-         }
-          else {
-            cdebug << "Suspend cannot Suspend component !" << endl ;
-            RetVal = false ;
-         }
-       }
-        else {
-          cdebug << "Suspend with nilComponent while RunningState !" << endl ;
-          RetVal = false ;
-        }
+        RetVal = true ;
       }
       else if ( IsWaiting() ) {
         RetVal = true ;
       }
       else {
-        cdebug << "Suspend and !IsDone and !IsRunning and !IsWaiting ?"
+        cdebug << "KillDone and !IsDone and !IsRunning and !IsWaiting ?"
                << endl ;
         RetVal = false ;
       }
@@ -608,17 +610,19 @@ bool GraphExecutor::InNode::Stop() {
   else {
     ControlState( SUPERV::ToStopState ) ;
     if ( IsDone() ) {
-      ControlState( SUPERV::VoidState ) ;
+      if ( _OutNode->IsDone() ) {
+        ControlState( SUPERV::VoidState ) ;
+      }
       RetVal = false ;
     }
     else {
       if ( IsRunning() ) {
-        if ( IsFactoryNode() ) {
+        if ( IsFactoryNode() || IsComputingNode() ) {
           if ( !CORBA::is_nil( Component() ) ) {
             RetVal = Component()->Stop_impl() ;
             if ( RetVal ) {
               if ( IsRunning() ) {
-                SendEvent( GraphExecutor::SuspendEvent ) ;
+                SendEvent( GraphExecutor::StopEvent ) ;
              }
               else if ( IsDone() ) {
                 ControlState( SUPERV::VoidState ) ;
@@ -664,7 +668,9 @@ bool GraphExecutor::InNode::SuspendDone() {
   else {
     ControlState( SUPERV::ToSuspendDoneState ) ;
     if ( IsDone() ) {
-      ControlState( SUPERV::VoidState ) ;
+      if ( _OutNode->IsDone() ) {
+        ControlState( SUPERV::VoidState ) ;
+      }
       RetVal = false ;
     }
     else {
@@ -686,16 +692,16 @@ bool GraphExecutor::InNode::Resume() {
       RetVal = true ;
     }
     else if ( State() == SUPERV::SuspendedExecutingState ) {
-      if ( IsFactoryNode() ) {
+      if ( IsFactoryNode() || IsComputingNode() ) {
         RetVal = Component()->Resume_impl() ;
       }
     }
     else if ( State() == SUPERV::SuspendedSuccessedState ) {
-      ResumeAction( GraphExecutor::ToResumeEvent ) ;
+      ResumeAction( GraphExecutor::ResumeEvent ) ;
       RetVal = true ;
     }
     else if ( State() == SUPERV::SuspendedErroredState ) {
-      ResumeAction( GraphExecutor::ToResumeEvent ) ;
+      ResumeAction( GraphExecutor::ResumeEvent ) ;
       RetVal = true ;
     }
     else {
@@ -1118,41 +1124,6 @@ void GraphExecutor::InNode::InitialState( GraphExecutor::OutNode * theOutNode )
              << anOutPort->IsPortConnected() << ") DataConnected("
              << anOutPort->IsDataConnected() << ")" << endl ;
     }
-
-    if ( !PyFuncRunned() ) {
-      bool Err = false ;
-      if ( IsLoopNode()  ) {
-        PyObject * PyRunMethod = InitPyDynInvoke( InLineNode()->PyFuncName() ,
-                                                  InLineNode()->PythonFunction() ) ;
-        InLineNode()->PyRunMethod( PyRunMethod ) ;
-        PyObject * PyMoreMethod = NULL ;
-        if ( PyRunMethod ) {
-          PyMoreMethod = InitPyDynInvoke( LoopNode()->PyMoreName() ,
-                                          LoopNode()->MorePythonFunction() ) ;
-          LoopNode()->PyMoreMethod( PyMoreMethod ) ;
-        }
-        PyObject * PyNextMethod = NULL ;
-        if ( PyMoreMethod ) {
-          PyNextMethod = InitPyDynInvoke( LoopNode()->PyNextName() ,
-                                          LoopNode()->NextPythonFunction() ) ;
-          LoopNode()->PyNextMethod( PyNextMethod ) ;
-       }
-        Err = !PyRunMethod || !PyMoreMethod || !PyNextMethod ;
-      }
-      else if ( IsInLineNode() || IsSwitchNode() ) {
-        PyObject * PyRunMethod = InitPyDynInvoke( InLineNode()->PyFuncName() ,
-                                                  InLineNode()->PythonFunction() ) ;
-        InLineNode()->PyRunMethod( PyRunMethod ) ;
-        Err = !PyRunMethod ;
-      }
-      else if ( ( IsEndSwitchNode() || IsGOTONode() ) &&
-                (*InLineNode()->PythonFunction()).length() ) {
-        PyObject * PyRunMethod = InitPyDynInvoke( InLineNode()->PyFuncName() ,
-                                                  InLineNode()->PythonFunction() ) ;
-        InLineNode()->PyRunMethod( PyRunMethod ) ;
-        Err = !PyRunMethod ;
-      }
-    }
   }
 
   _currentState = Pc > 0 ? SUPERV::DataWaitingState 
@@ -1175,6 +1146,74 @@ void GraphExecutor::InNode::InitialState( GraphExecutor::OutNode * theOutNode )
   cdebug_out << "GraphExecutor::InNode::InitialState" << endl;
 }
 
+bool GraphExecutor::InNode::InitPythonFunctions(bool WithErr ) {
+  cdebug_in << "GraphExecutor::InNode::InitPythonFunctions " << Name() << endl;
+  bool Err = false ;
+  if ( !PyFuncRunned() && IsOneOfInLineNodes() ) {
+    if ( IsLoopNode() ) {
+      PyObject * PyRunMethod = InLineNode()->PyRunMethod() ;
+      if ( PyRunMethod ) {
+      }
+      else {
+        PyObject * PyRunMethod = InitPyDynInvoke( InLineNode()->PyFuncName() ,
+                                                  InLineNode()->PythonFunction() ) ;
+        InLineNode()->PyRunMethod( PyRunMethod ) ;
+      }
+      PyObject * PyMoreMethod = LoopNode()->PyMoreMethod() ;
+      if ( PyMoreMethod ) {
+      }
+      else {
+        PyMoreMethod = InitPyDynInvoke( LoopNode()->PyMoreName() ,
+                                        LoopNode()->MorePythonFunction() ) ;
+        LoopNode()->PyMoreMethod( PyMoreMethod ) ;
+      }
+      PyObject * PyNextMethod = LoopNode()->PyNextMethod() ;
+      if ( PyNextMethod ) {
+      }
+      else {
+        PyNextMethod = InitPyDynInvoke( LoopNode()->PyNextName() ,
+                                        LoopNode()->NextPythonFunction() ) ;
+        LoopNode()->PyNextMethod( PyNextMethod ) ;
+      }
+      cdebug << "GraphExecutor::InNode::InitPythonFunctions " << Name() << " PyRunMethod(Init) " << PyRunMethod
+             << " PyMoreMethod " << PyMoreMethod << " PyNextMethod " << PyNextMethod << endl;
+      Err = !PyRunMethod || !PyMoreMethod || !PyNextMethod ;
+    }
+    else if ( IsInLineNode() || IsSwitchNode() ) {
+      PyObject * PyRunMethod = InLineNode()->PyRunMethod() ;
+      if ( PyRunMethod ) {
+      }
+      else {
+       PyRunMethod = InitPyDynInvoke( InLineNode()->PyFuncName() ,
+                                       InLineNode()->PythonFunction() ) ;
+        InLineNode()->PyRunMethod( PyRunMethod ) ;
+      }
+      cdebug << "GraphExecutor::InNode::InitPythonFunctions " << Name() << " PyRunMethod " << PyRunMethod << endl;
+      Err = !PyRunMethod ;
+    }
+    else if ( ( IsEndLoopNode() || IsEndSwitchNode() || IsGOTONode() ) &&
+              (*InLineNode()->PythonFunction()).length() ) {
+      PyObject * PyRunMethod = InLineNode()->PyRunMethod() ;
+      if ( PyRunMethod ) {
+      }
+      else {
+        PyRunMethod = InitPyDynInvoke( InLineNode()->PyFuncName() ,
+                                       InLineNode()->PythonFunction() ) ;
+        InLineNode()->PyRunMethod( PyRunMethod ) ;
+      }
+      cdebug << "GraphExecutor::InNode::InitPythonFunctions " << Name() << " PyRunMethod " << PyRunMethod << endl;
+      Err = !PyRunMethod ;
+    }
+  }
+  Err = WithErr && Err ;
+  cdebug_out << "GraphExecutor::InNode::InitPythonFunctions " << Name() ;
+  if ( Err ) {
+    cdebug << " Error " << Err ;
+  }
+  cdebug << endl;
+  return Err ;
+}
+
 const long GraphExecutor::InNode::CpuUsed( bool tot ) {
   CORBA::Long cpu = 0 ;
 //  cdebug_in << "GraphExecutor::InNode::CpuUsed( " << tot << " )" << Name() << endl ;