2002-11-06  Mo DeJong  <mdejong@users.sourceforge.net>

	* generic/tclEvent.c (TclInExit, TclInThreadExit):
	Split out functionality of TclInExit to make it
	clear which one should be called in each situation.
	* generic/tclInt.decls: Declare TclInThreadExit.
	* generic/tclIntDecls.h: Regen.
	* generic/tclStubInit.c: Regen.
	* mac/tclMacChan.c (StdIOClose):
	* unix/tclUnixChan.c (FileCloseProc):
	* win/tclWinChan.c (FileCloseProc):
	* win/tclWinConsole.c (ConsoleCloseProc):
	* win/tclWinPipe.c (TclpCloseFile):
	* win/tclWinSerial.c (SerialCloseProc): Invoke the
	new TclInThreadExit method instead of TclInExit.

Index: generic/tclEvent.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclEvent.c,v
retrieving revision 1.23
diff -u -r1.23 tclEvent.c
--- generic/tclEvent.c	5 Aug 2002 03:24:40 -0000	1.23
+++ generic/tclEvent.c	7 Nov 2002 02:08:24 -0000
@@ -878,10 +878,6 @@
 	    (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
 
     if (tsdPtr != NULL) {
-	/*
-	 * Invoke thread exit handlers first.
-	 */
-
 	tsdPtr->inExit = 1;
 
 	/*
@@ -937,10 +933,32 @@
 int
 TclInExit()
 {
+    return inFinalize;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclInThreadExit --
+ *
+ *	Determines if we are in the middle of thread exit-time cleanup.
+ *
+ * Results:
+ *	If we are in the middle of exiting this thread, 1, otherwise 0.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclInThreadExit()
+{
     ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
 	    TclThreadDataKeyGet(&dataKey);
     if (tsdPtr == NULL) {
-	return inFinalize;
+	return 0;
     } else {
 	return tsdPtr->inExit;
     }
Index: generic/tclInt.decls
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclInt.decls,v
retrieving revision 1.56
diff -u -r1.56 tclInt.decls
--- generic/tclInt.decls	9 Oct 2002 11:54:09 -0000	1.56
+++ generic/tclInt.decls	7 Nov 2002 02:08:24 -0000
@@ -686,6 +686,10 @@
 	    Tcl_Obj *CONST objv[])
 }
 
+declare 172 generic {
+    int TclInThreadExit(void)
+}
+
 ##############################################################################
 
 # Define the platform specific internal Tcl interface. These functions are
Index: generic/tclIntDecls.h
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclIntDecls.h,v
retrieving revision 1.47
diff -u -r1.47 tclIntDecls.h
--- generic/tclIntDecls.h	9 Oct 2002 12:28:01 -0000	1.47
+++ generic/tclIntDecls.h	7 Nov 2002 02:08:26 -0000
@@ -496,6 +496,8 @@
 				int numChars, Command * cmdPtr, int result, 
 				int traceFlags, int objc, 
 				Tcl_Obj *CONST objv[]));
+/* 172 */
+EXTERN int		TclInThreadExit _ANSI_ARGS_((void));
 
 typedef struct TclIntStubs {
     int magic;
@@ -697,6 +699,7 @@
     int (*tclpUtfNcmp2) _ANSI_ARGS_((CONST char * s1, CONST char * s2, unsigned long n)); /* 169 */
     int (*tclCheckInterpTraces) _ANSI_ARGS_((Tcl_Interp * interp, CONST char * command, int numChars, Command * cmdPtr, int result, int traceFlags, int objc, Tcl_Obj *CONST objv[])); /* 170 */
     int (*tclCheckExecutionTraces) _ANSI_ARGS_((Tcl_Interp * interp, CONST char * command, int numChars, Command * cmdPtr, int result, int traceFlags, int objc, Tcl_Obj *CONST objv[])); /* 171 */
+    int (*tclInThreadExit) _ANSI_ARGS_((void)); /* 172 */
 } TclIntStubs;
 
 #ifdef __cplusplus
@@ -1298,6 +1301,10 @@
 #ifndef TclCheckExecutionTraces
 #define TclCheckExecutionTraces \
 	(tclIntStubsPtr->tclCheckExecutionTraces) /* 171 */
+#endif
+#ifndef TclInThreadExit
+#define TclInThreadExit \
+	(tclIntStubsPtr->tclInThreadExit) /* 172 */
 #endif
 
 #endif /* defined(USE_TCL_STUBS) && !defined(USE_TCL_STUB_PROCS) */
Index: generic/tclStubInit.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclStubInit.c,v
retrieving revision 1.76
diff -u -r1.76 tclStubInit.c
--- generic/tclStubInit.c	9 Oct 2002 11:54:48 -0000	1.76
+++ generic/tclStubInit.c	7 Nov 2002 02:08:28 -0000
@@ -243,6 +243,7 @@
     TclpUtfNcmp2, /* 169 */
     TclCheckInterpTraces, /* 170 */
     TclCheckExecutionTraces, /* 171 */
+    TclInThreadExit, /* 172 */
 };
 
 TclIntPlatStubs tclIntPlatStubs = {
Index: mac/tclMacChan.c
===================================================================
RCS file: /cvsroot/tcl/tcl/mac/tclMacChan.c,v
retrieving revision 1.18
diff -u -r1.18 tclMacChan.c
--- mac/tclMacChan.c	9 Oct 2002 11:54:16 -0000	1.18
+++ mac/tclMacChan.c	7 Nov 2002 02:08:28 -0000
@@ -425,7 +425,7 @@
      */
 
     fd = (int) ((FileState*)instanceData)->fileRef;
-    if (!TclInExit()) {
+    if (!TclInThreadExit()) {
 	if (fd == 0) {
 	    tsdPtr->stdinChannel = NULL;
 	} else if (fd == 1) {
Index: unix/tclUnixChan.c
===================================================================
RCS file: /cvsroot/tcl/tcl/unix/tclUnixChan.c,v
retrieving revision 1.39
diff -u -r1.39 tclUnixChan.c
--- unix/tclUnixChan.c	3 Sep 2002 02:01:25 -0000	1.39
+++ unix/tclUnixChan.c	7 Nov 2002 02:08:30 -0000
@@ -549,7 +549,7 @@
      * Do not close standard channels while in thread-exit.
      */
 
-    if (!TclInExit()
+    if (!TclInThreadExit()
 	    || ((fsPtr->fd != 0) && (fsPtr->fd != 1) && (fsPtr->fd != 2))) {
 	if (close(fsPtr->fd) < 0) {
 	    errorCode = errno;
Index: win/tclWinChan.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinChan.c,v
retrieving revision 1.24
diff -u -r1.24 tclWinChan.c
--- win/tclWinChan.c	8 Jul 2002 10:08:58 -0000	1.24
+++ win/tclWinChan.c	7 Nov 2002 02:08:31 -0000
@@ -397,11 +397,11 @@
 
     /*
      * Don't close the Win32 handle if the handle is a standard channel
-     * during the exit process.  Otherwise, one thread may kill the stdio
-     * of another.
+     * during the thread exit process.  Otherwise, one thread may kill
+     * the stdio of another.
      */
 
-    if (!TclInExit() 
+    if (!TclInThreadExit() 
 	    || ((GetStdHandle(STD_INPUT_HANDLE) != fileInfoPtr->handle)
 		&& (GetStdHandle(STD_OUTPUT_HANDLE) != fileInfoPtr->handle)
 		&& (GetStdHandle(STD_ERROR_HANDLE) != fileInfoPtr->handle))) {
Index: win/tclWinConsole.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinConsole.c,v
retrieving revision 1.8
diff -u -r1.8 tclWinConsole.c
--- win/tclWinConsole.c	2 Sep 2002 19:27:02 -0000	1.8
+++ win/tclWinConsole.c	7 Nov 2002 02:08:32 -0000
@@ -542,11 +542,11 @@
 
     /*
      * Don't close the Win32 handle if the handle is a standard channel
-     * during the exit process.  Otherwise, one thread may kill the stdio
-     * of another.
+     * during the thread exit process.  Otherwise, one thread may kill
+     * the stdio of another.
      */
 
-    if (!TclInExit() 
+    if (!TclInThreadExit() 
 	    || ((GetStdHandle(STD_INPUT_HANDLE) != consolePtr->handle)
 		&& (GetStdHandle(STD_OUTPUT_HANDLE) != consolePtr->handle)
 		&& (GetStdHandle(STD_ERROR_HANDLE) != consolePtr->handle))) {
Index: win/tclWinPipe.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinPipe.c,v
retrieving revision 1.25
diff -u -r1.25 tclWinPipe.c
--- win/tclWinPipe.c	17 Jun 2002 20:05:49 -0000	1.25
+++ win/tclWinPipe.c	7 Nov 2002 02:08:34 -0000
@@ -854,11 +854,11 @@
 	case WIN_FILE:
 	    /*
 	     * Don't close the Win32 handle if the handle is a standard channel
-	     * during the exit process.  Otherwise, one thread may kill the
-	     * stdio of another.
+	     * during the thread exit process.  Otherwise, one thread may kill
+	     * the stdio of another.
 	     */
 
-	    if (!TclInExit() 
+	    if (!TclInThreadExit() 
 		    || ((GetStdHandle(STD_INPUT_HANDLE) != filePtr->handle)
 			    && (GetStdHandle(STD_OUTPUT_HANDLE) != filePtr->handle)
 			    && (GetStdHandle(STD_ERROR_HANDLE) != filePtr->handle))) {
Index: win/tclWinSerial.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinSerial.c,v
retrieving revision 1.21
diff -u -r1.21 tclWinSerial.c
--- win/tclWinSerial.c	19 Jul 2002 13:59:10 -0000	1.21
+++ win/tclWinSerial.c	7 Nov 2002 02:08:35 -0000
@@ -632,11 +632,11 @@
 
     /*
      * Don't close the Win32 handle if the handle is a standard channel
-     * during the exit process.  Otherwise, one thread may kill the stdio
-     * of another.
+     * during the thread exit process.  Otherwise, one thread may kill
+     * the stdio of another.
      */
 
-    if (!TclInExit()
+    if (!TclInThreadExit()
         || ((GetStdHandle(STD_INPUT_HANDLE) != serialPtr->handle)
         && (GetStdHandle(STD_OUTPUT_HANDLE) != serialPtr->handle)
         && (GetStdHandle(STD_ERROR_HANDLE) != serialPtr->handle))) {
