fix tcl freeze when an error occured at initialization
[scilab.git] / scilab / modules / tclsci / src / c / InitTclTk.c
index add308a..98381b3 100644 (file)
@@ -41,6 +41,13 @@ BOOL TK_Started = FALSE;
 
 /*--------------------------------------------------------------------------*/
 static char *GetSciPath(void);
+static char *releaseTclInterpOnError(void)
+{
+    __LockSignal(&InterpReadyLock);
+    __Signal(&InterpReady);
+    __UnLockSignal(&InterpReadyLock);
+    setTkStarted(FALSE);
+}
 /*--------------------------------------------------------------------------*/
 static void *DaemonOpenTCLsci(void* in)
 /* Checks if tcl/tk has already been initialised and if not */
@@ -68,6 +75,7 @@ static void *DaemonOpenTCLsci(void* in)
     if (SciPath == NULL)
     {
         sciprint(_("The SCI environment variable is not set.\nTCL initialisation failed !\n"));
+        releaseTclInterpOnError();
         return (0);
     }
 
@@ -115,6 +123,7 @@ static void *DaemonOpenTCLsci(void* in)
         SciPathShort = NULL;
         FREE(TkScriptpathShort);
         TkScriptpathShort = NULL;
+        releaseTclInterpOnError();
         return (0);
     }
     else
@@ -132,6 +141,7 @@ static void *DaemonOpenTCLsci(void* in)
         SciPathShort = NULL;
         FREE(TkScriptpathShort);
         TkScriptpathShort = NULL;
+        releaseTclInterpOnError();
         return (0);
     }
     else
@@ -153,11 +163,13 @@ static void *DaemonOpenTCLsci(void* in)
 
         if ( getTclInterp() == NULL )
         {
+            releaseTclInterp();
             Scierror(999, _("Tcl Error: Unable to create Tcl interpreter (Tcl_CreateInterp).\n"));
             FREE(SciPathShort);
             SciPathShort = NULL;
             FREE(TkScriptpathShort);
             TkScriptpathShort = NULL;
+            releaseTclInterpOnError();
             return (0);
         }
         releaseTclInterp();
@@ -166,10 +178,12 @@ static void *DaemonOpenTCLsci(void* in)
         {
             releaseTclInterp();
             Scierror(999, _("Tcl Error: Error during the Tcl initialization (Tcl_Init): %s\n"), Tcl_GetStringResult(getTclInterp()));
+            releaseTclInterp();
             FREE(SciPathShort);
             SciPathShort = NULL;
             FREE(TkScriptpathShort);
             TkScriptpathShort = NULL;
+            releaseTclInterpOnError();
             return (0);
         }
         releaseTclInterp();
@@ -181,7 +195,10 @@ static void *DaemonOpenTCLsci(void* in)
             if ( Tk_Init(getTclInterp()) == TCL_ERROR)
             {
                 releaseTclInterp();
+                releaseTclInterpOnError();
                 Scierror(999, _("Tcl Error: Error during the TK initialization (Tk_Init): %s\n"), Tcl_GetStringResult(getTclInterp()));
+                releaseTclInterp();
+                return (0);
             }
             else
             {
@@ -197,6 +214,7 @@ static void *DaemonOpenTCLsci(void* in)
         {
             releaseTclInterp();
             Scierror(999, _("Tcl Error: Error during the Scilab/Tcl init process. Could not set SciPath: %s\n"), Tcl_GetStringResult(getTclInterp()));
+            releaseTclInterp();
             FREE(SciPathShort);
             SciPathShort = NULL;
             FREE(TkScriptpathShort);
@@ -221,8 +239,10 @@ static void *DaemonOpenTCLsci(void* in)
         {
             releaseTclInterp();
             Scierror(999, _("Tcl Error: Error during the Scilab/TK init process. Error while loading %s: %s\n"), TkScriptpathShort, Tcl_GetStringResult(getTclInterp()));
+            releaseTclInterp();
             FREE(TkScriptpathShort);
             TkScriptpathShort = NULL;
+            releaseTclInterpOnError();
             return (0);
         }
         releaseTclInterp();
@@ -247,7 +267,6 @@ int OpenTCLsci(void)
     // Allows all Tcl application not to freeze nor decrease Scilab speed.
     // Causes also Scilab let those application live their own lifes.
 
-
     __CreateThread(&TclThread, &key, &DaemonOpenTCLsci);
     // Wait to be sure initialisation is complete.
     __LockSignal(&InterpReadyLock);