digraph ScilabTclThread {\

ranksep=0.1; nodesep=0.5;
splines=true

node [shape=box];

label="mutex are hexagons\ncreated signals are uptriangles\n\
 signals are downtriangles\nthreads are parallelograms"

 subgraph cluster_initialize {
  label = "Initialization";
  node [shape=box];
  "Scilab start-up and Init" [shape=ellipse peripheries=2];
  "Scilab start-up and Init" -> "create signal InterpReady" ->
    "create thread\nDaemonOpenTCLsci\nand launch it" -> 
     "Wait for InterpReady\nto be signaled" ->
     "End of Scilab Init";
     "Wait for InterpReady\nto be signaled" [shape=diamond];
     "End of Scilab Init" [shape=ellipse  peripheries=2]; 
     "create signal InterpReady" [shape=triangle]
    "create thread\nDaemonOpenTCLsci\nand launch it" [shape=parallelogram] 
     color=purple;
   }


  subgraph cluster_OpenTCLsci {
     node [shape=box];
     label = "thread OpenTCLsci performs";
    "start TCL_Library and\nTK_LIBRARY env. variables"->
    "create TCL interp\n(main global interp)" ->
    "Initialize TCL\n(-> Tcl_Init)" ->
    "Initialize Tk\n(-> Tk_Init)" ->
    "Set SciPoth Tcl variable\n(not used by scipad)"->
    "Create ScilabEval command\n(mop to TCL_EvalScilab cmd)" ->
    "Create the \".\" Tk window\n(not used by Scipad\n - win withdrawn later)"->
    "Signals InterpReady" ->
    "Calls startTclLoop\n(returns only when Scilab exits)";
    "Signals InterpReady" [shape=triangle orientation=180]
   }

  subgraph cluster_startTCLLoop {node [shape=box];
   label="Normal Running";
   "Declare handle on\nthread sleepThreadId" ->
   "creates a mutex\nsingleExecutionLock" ->
   "creates a signal wakeUp"->
   "creates a mutex\nwakeUpLock" ->
   "creates a signal workIsDone"->
   "creates a mutex\nlaunchcommand" ->
   "creates a thread sleepAndSignal\nand launch it";
   color=blue
   "Declare handle on\nthread sleepThreadId" [shape=parallelogram] 
   "creates a mutex\nsingleExecutionLock" [shape=hexagon]
   "creates a signal wakeUp" [shape=triangle]
   "creates a mutex\nwakeUpLock" [shape=hexagon]
   "creates a signal workIsDone" [shape=triangle]
   "creates a mutex\nlaunchcommand" [shape=hexagon]
   "creates a thread sleepAndSignal\nand launch it" [shape=parallelogram];
   }

  subgraph cluster_whileTk_Started {
    label="while (TK_started)"
    A [shape=diamond, 
      label="TclCommand=/=NULL | TCLFile=/=NULL\n(command or script has to be run)"]
    A -> "locks launchCommand" [label=yes]
    "locks launchCommand" [shape=hexagon]
    "locks launchCommand"  ->
    "execute command (TCL_Eval)\nor file (TCL_EvalFile() )\nin main or slave interp" ->
    "Signals workIsDone" -> "releases mutex launchCommand"
    "Signals workIsDone" [shape=triangle orientation=180]
    "releases mutex launchCommand" [shape=hexagon]
    "releases mutex launchCommand" ->  "end while(TK_Started)" 
    A -> "locks wakeUpLock" [label=no]
    "locks wakeUpLock" [shape=hexagon]
    "locks wakeUpLock"->"Tcl_Eval(\"update\")" ->
    "waits for wakeUpLock\n to be signaled"
    "waits for wakeUpLock\n to be signaled" [shape=diamond]
    "waits for wakeUpLock\n to be signaled" -> "releases wakeUpLock"
    "releases wakeUpLock"  ->   "end while(TK_Started)" 
    "releases wakeUpLock" [shape=hexagon]
    "end while(TK_Started)" [shape=ellipse]
     "end while(TK_Started)" -> A
  }

  subgraph cluster_sleepAndSignal {
    label="thread SleepAndSignal"
     "sleep 10ms" -> "locks WakeUpLock" ->
     "signals wakeUp" -> "releases wakeUpLock" ->
     "Tk_Started ?" 
     "Tk_Started ?" ->  "sleep 10ms" [label=yes] 
     "Tk_Started ?" ->  "End" [label=no] 
 }

   "end while(TK_Started)" -> "deleteTclInterp" -> "game over" 
   "game over" [shape=ellipse, peripheries=2]

   "create thread\nDaemonOpenTCLsci\nand launch it" ->
    "start TCL_Library and\nTK_LIBRARY env. variables" [style=dashed];

   "Signals InterpReady" -> "Wait for InterpReady\nto be signaled" [color=red];

   "Calls startTclLoop\n(returns only when Scilab exits)"->
       "Declare handle on\nthread sleepThreadId" [color=red];

   "creates a thread sleepAndSignal\nand launch it" -> A

  subgraph cluster_TclCommand {
  label "How TclCommand is set"
  "Send TclCommandToSlave"
  "lock singleExecutionLock" -> "lock lLaunchCommand" ->
   "set TclCommand to\n what was passed\n to TCL_EvalStr" ->
   "set TclSlave to NULL\n (->globalunity for stg else\n named interp)"
 }
}
