Tcl Source Code

View Ticket
Login
Ticket UUID: 652530
Title: Invalidated Tk_Uids
Type: Bug Created on: 2002-12-12 07:52:43
Submitter: nobody Assigned to: andreas_kupries
Subsystem: 49. Threading Severity:
Priority: 5 Medium Last modified: 2003-01-04 05:46:38
Status: Closed Closed by: hobbs
Resolution: Duplicate Closed on: 2003-01-03 22:46:38
Version: obsolete: 8.4.1
Description:
While playing around with Tk 8.4.1 and
multi-threading on Win32 I observed some
strange behaviour w.r.t. Tk_Uids:

1. Tk_ConfigureWidget() converts the
name/class/default values in the given
Tk_ConfigSpec to UIDs.

2. Tk_GetUid() installs a per-thread
exit handler (FreeUidThreadExitProc())
which destroys the per-thread UID
table (and the UIDs, too) on thread
termination, i.e. after the thread
using Tk exited, some Tk_ConfigSpec
fields point to free'd memory.

3. When Tk is re-animated later (Tk_Init())
in a new thread, arbitrary  Bad Things
happen as consequence to invalidated UIDs
from point 2.

Questions:

- Is it really necessary to convert
name/class/default values to UIDs
in Tk_ConfigureWidget ?

- Is it really necessary to have a
per-thread UID table ?

--

In the meantime I tried to temporarily fix it by:

1. Making dbName/dbClass/defValue fields in 
Tk_ConfigSpec to normal char pointer instead
of Tk_Uids
2. Using a mutex locked global (= per process)
Uid table
3. Making a temporary copy of the Tk_ConfigSpec
array in Tk_ConfigureWidget

See the attached patch. However, this isn't
perfect since the TK_CONFIG_OPTION_SPECIFIED
bit is not reflected to callers of
Tk_ConfigureWidget for threaded builds of Tk.
User Comments:
nobody added on 2002-12-12 14:58:54:
Logged In: NO 

--- generic/tkOldConfig.c.ORIG  Wed Aug  7 19:45:38 2002
+++ generic/tkOldConfig.c       Thu Dec 12 00:21:46 2002
@@ -18,17 +18,6 @@
 #include "tk.h"
 
 /*
- * Values for "flags" field of Tk_ConfigSpec structures. 
Be sure
- * to coordinate these values with those defined in tk.h
- * (TK_CONFIG_COLOR_ONLY, etc.).  There must not be
overlap!
- *
- * INIT -              Non-zero means (char *) things have
been
- *                     converted to Tk_Uid's.
- */
-
-#define INIT           0x20
-
-/*
  * Forward declarations for procedures defined later in
this file:
  */
 
@@ -89,6 +78,11 @@
                                 * or else they are not
considered. */
     int hateFlags;             /* If a spec contains any
bits here, it's
                                 * not considered. */
+    int result = TCL_ERROR;    /* Return value. */
+#ifdef TCL_THREADS
+    int nOpts;                 /* Number of entries in
specs. */
+    Tk_ConfigSpec *specCopy = NULL;    /* Copy of specs,
malloc'ed. */
+#endif
 
     if (tkwin == NULL) {
        /*
@@ -96,7 +90,7 @@
         * we're on our way out of the application
         */
        Tcl_AppendResult(interp, "NULL main window", (char
*)NULL);
-       return TCL_ERROR;
+       goto done;
     }
 
     needFlags = flags & ~(TK_CONFIG_USER_BIT - 1);
@@ -106,26 +100,28 @@
        hateFlags = TK_CONFIG_MONO_ONLY;
     }
 
+#ifdef TCL_THREADS
     /*
-     * Pass one:  scan through all the option specs,
replacing strings
-     * with Tk_Uid structs (if this hasn't been done
already) and
+     * When threads are used, work on a copy of specs.
+     */
+
+    nOpts = 1;
+    for (specPtr = specs; specPtr->type != TK_CONFIG_END;
specPtr++) {
+        nOpts++;
+    }
+    nOpts *= sizeof(Tk_ConfigSpec);
+    specCopy = (Tk_ConfigSpec *)ckalloc(nOpts);
+    memcpy((VOID *)specCopy, (VOID *)specs, nOpts);
+    specs = specCopy;
+#endif
+
+    /*
+     * Pass one:  scan through all the option specs, 
      * clearing the TK_CONFIG_OPTION_SPECIFIED flags.
      */
 
     for (specPtr = specs; specPtr->type != TK_CONFIG_END;
specPtr++) {
-       if (!(specPtr->specFlags & INIT) &&
(specPtr->argvName != NULL)) {
-           if (specPtr->dbName != NULL) {
-               specPtr->dbName =
Tk_GetUid(specPtr->dbName);
-           }
-           if (specPtr->dbClass != NULL) {
-               specPtr->dbClass =
Tk_GetUid(specPtr->dbClass);
-           }
-           if (specPtr->defValue != NULL) {
-               specPtr->defValue =
Tk_GetUid(specPtr->defValue);
-           }
-       }
-       specPtr->specFlags = (specPtr->specFlags &
~TK_CONFIG_OPTION_SPECIFIED)
-               | INIT;
+       specPtr->specFlags = specPtr->specFlags &
~TK_CONFIG_OPTION_SPECIFIED;
     }
 
     /*
@@ -143,7 +139,7 @@
        }
        specPtr = FindConfigSpec(interp, specs, arg,
needFlags, hateFlags);
        if (specPtr == NULL) {
-           return TCL_ERROR;
+           goto done;
        }
 
        /*
@@ -153,7 +149,7 @@
        if (argc < 2) {
            Tcl_AppendResult(interp, "value for \"", arg,
                    "\" missing", (char *) NULL);
-           return TCL_ERROR;
+           goto done;
        }
        if (flags & TK_CONFIG_OBJS) {
            arg = Tcl_GetString((Tcl_Obj *) argv[1]);
@@ -166,7 +162,7 @@
            sprintf(msg, "\n    (processing \"%.40s\"
option)",
                    specPtr->argvName);
            Tcl_AddErrorInfo(interp, msg);
-           return TCL_ERROR;
+           goto done;
        }
        specPtr->specFlags |= TK_CONFIG_OPTION_SPECIFIED;
     }
@@ -202,7 +198,7 @@
                            "database entry for",
                            specPtr->dbName,
Tk_PathName(tkwin));
                    Tcl_AddErrorInfo(interp, msg);
-                   return TCL_ERROR;
+                   goto done;
                }
            } else {
                if (specPtr->defValue != NULL) {
@@ -221,14 +217,21 @@
                                "default value for",
                                specPtr->dbName,
Tk_PathName(tkwin));
                        Tcl_AddErrorInfo(interp, msg);
-                       return TCL_ERROR;
+                       goto done;
                    }
                }
            }
        }
     }
 
-    return TCL_OK;
+    result = TCL_OK;
+done:
+#ifdef TCL_THREADS
+    if (specCopy != NULL) {
+       ckfree((char *) specCopy);
+    }
+#endif
+    return result;
 }
 
 /*
@@ -315,7 +318,7 @@
                        argvName, "\"", (char *) NULL);
                return (Tk_ConfigSpec *) NULL;
            }
-           if ((specPtr->dbName == matchPtr->dbName) 
+           if (!strcmp(specPtr->dbName, matchPtr->dbName) 
                    && (specPtr->type != TK_CONFIG_SYNONYM)
                    && ((specPtr->specFlags & needFlags) ==
needFlags)
                    && !(specPtr->specFlags & hateFlags)) {
--- generic/tk.h.ORIG   Thu Oct 10 02:31:14 2002
+++ generic/tk.h        Wed Dec 11 12:52:14 2002
@@ -366,9 +366,9 @@
                                 * table must have type
TK_CONFIG_END. */
     char *argvName;            /* Switch used to specify
option in argv.
                                 * NULL means this spec is
part of a group. */
-    Tk_Uid dbName;             /* Name for option in option
database. */
-    Tk_Uid dbClass;            /* Class for option in
database. */
-    Tk_Uid defValue;           /* Default value for option
if not
+    char *dbName;              /* Name for option in option
database. */
+    char *dbClass;             /* Class for option in
database. */
+    char *defValue;            /* Default value for option
if not
                                 * specified in command line
or database. */
     int offset;                        /* Where in widget
record to store value;
                                 * use Tk_Offset macro to
generate values
--- generic/tkGet.c.ORIG        Wed Aug  7 19:45:36 2002
+++ generic/tkGet.c     Wed Dec 11 10:25:28 2002
@@ -20,19 +20,17 @@
 #include "tkPort.h"
 
 /*
- * One of these structures is created per thread to store 
- * thread-specific data.  In this case, it is used to house
the 
- * Tk_Uid structs used by each thread.  The "dataKey" below
is 
- * used to locate the ThreadSpecificData for the current
thread.
+ * The Mutex below is used to lock access to the Uid table.
  */
 
-typedef struct ThreadSpecificData {
-    int initialized;
-    Tcl_HashTable uidTable;
-} ThreadSpecificData;
-static Tcl_ThreadDataKey dataKey;
+TCL_DECLARE_MUTEX(uidMutex)
 
-static void    FreeUidThreadExitProc
_ANSI_ARGS_((ClientData clientData));
+/*
+ * Hash table for Tk_Uid structs.
+ */
+
+static int uidInitialized = 0;
+static Tcl_HashTable uidTable;
 
 /*
  * The following tables defines the string values for
reliefs, which are
@@ -486,32 +484,6 @@
 /*
 
*----------------------------------------------------------------------
  *
- * FreeUidThreadExitProc --
- *
- *     Cleans up memory used for Tk_Uids in the thread.
- *
- * Results:
- *     None.
- *
- * Side effects:
- *     All information in the identifier table is deleted.
- *
-
*----------------------------------------------------------------------
- */
-
-static void
-FreeUidThreadExitProc(clientData)
-    ClientData clientData;             /* Not used. */
-{
-    ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
-            Tcl_GetThreadData(&dataKey,
sizeof(ThreadSpecificData));
-    Tcl_DeleteHashTable(&tsdPtr->uidTable);
-    tsdPtr->initialized = 0;
-}
-
-/*
-
*----------------------------------------------------------------------
- *
  * Tk_GetUid --
  *
  *     Given a string, this procedure returns a unique
identifier
@@ -537,17 +509,21 @@
     CONST char *string;                /* String to
convert. */
 {
     int dummy;
-    ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
-            Tcl_GetThreadData(&dataKey,
sizeof(ThreadSpecificData));
-    Tcl_HashTable *tablePtr = &tsdPtr->uidTable;
-
-    if (!tsdPtr->initialized) {
-       Tcl_InitHashTable(tablePtr, TCL_STRING_KEYS);
-       Tcl_CreateThreadExitHandler(FreeUidThreadExitProc,
NULL);
-       tsdPtr->initialized = 1;
-    }
-    return (Tk_Uid) Tcl_GetHashKey(tablePtr,
-           Tcl_CreateHashEntry(tablePtr, string, &dummy));
+    Tk_Uid uid;
+
+    if (!uidInitialized) {
+       Tcl_MutexLock(&uidMutex);
+       if (!uidInitialized) {
+           Tcl_InitHashTable(&uidTable, TCL_STRING_KEYS);
+           uidInitialized = 1;
+       }
+       Tcl_MutexUnlock(&uidMutex);
+    }
+    Tcl_MutexLock(&uidMutex);
+    uid = (Tk_Uid) Tcl_GetHashKey(&uidTable,
+                      Tcl_CreateHashEntry(&uidTable,
string, &dummy));
+    Tcl_MutexUnlock(&uidMutex);
+    return uid;
 }
 
 /*

hobbs added on 2003-01-04 05:46:38:
Logged In: YES 
user_id=72656

See Tk bug 659129