os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclEvent.c
First public contribution.
4 * This file implements some general event related interfaces including
5 * background errors, exit handlers, and the "vwait" and "update"
8 * Copyright (c) 1990-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
10 * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 * RCS: @(#) $Id: tclEvent.c,v 1.28.2.15 2007/03/19 17:06:25 dgp Exp $
20 #if defined(__SYMBIAN32__) && defined(__WINSCW__)
21 #include "tclSymbianGlobals.h"
22 #define dataKey getdataKey(0)
26 * The data structure below is used to report background errors. One
27 * such structure is allocated for each error; it holds information
28 * about the interpreter and the error until bgerror can be invoked
29 * later as an idle handler.
32 typedef struct BgError {
33 Tcl_Interp *interp; /* Interpreter in which error occurred. NULL
34 * means this error report has been cancelled
35 * (a previous report generated a break). */
36 char *errorMsg; /* Copy of the error message (the interp's
37 * result when the error occurred).
39 char *errorInfo; /* Value of the errorInfo variable
41 char *errorCode; /* Value of the errorCode variable
43 struct BgError *nextPtr; /* Next in list of all pending error
44 * reports for this interpreter, or NULL
49 * One of the structures below is associated with the "tclBgError"
50 * assoc data for each interpreter. It keeps track of the head and
51 * tail of the list of pending background errors for the interpreter.
54 typedef struct ErrAssocData {
55 BgError *firstBgPtr; /* First in list of all background errors
56 * waiting to be processed for this
57 * interpreter (NULL if none). */
58 BgError *lastBgPtr; /* Last in list of all background errors
59 * waiting to be processed for this
60 * interpreter (NULL if none). */
64 * For each exit handler created with a call to Tcl_CreateExitHandler
65 * there is a structure of the following type:
68 typedef struct ExitHandler {
69 Tcl_ExitProc *proc; /* Procedure to call when process exits. */
70 ClientData clientData; /* One word of information to pass to proc. */
71 struct ExitHandler *nextPtr;/* Next in list of all exit handlers for
72 * this application, or NULL for end of list. */
76 * There is both per-process and per-thread exit handlers.
77 * The first list is controlled by a mutex. The other is in
78 * thread local storage.
81 static ExitHandler *firstExitPtr = NULL;
82 /* First in list of all exit handlers for
84 TCL_DECLARE_MUTEX(exitMutex)
87 * This variable is set to 1 when Tcl_Finalize is called, and at the end of
88 * its work, it is reset to 0. The variable is checked by TclInExit() to
89 * allow different behavior for exit-time processing, e.g. in closing of
92 #if !defined(__SYMBIAN32__) || !defined(__WINSCW__)
93 static int inFinalize = 0;
94 static int subsystemsInitialized = 0;
97 typedef struct ThreadSpecificData {
98 ExitHandler *firstExitPtr; /* First in list of all exit handlers for
100 int inExit; /* True when this thread is exiting. This
101 * is used as a hack to decide to close
102 * the standard channels. */
103 Tcl_Obj *tclLibraryPath; /* Path(s) to the Tcl library */
104 } ThreadSpecificData;
105 #if !defined(__SYMBIAN32__) || !defined(__WINSCW__)
106 static Tcl_ThreadDataKey dataKey;
109 * Common string for the library path for sharing across threads.
110 * This is ckalloc'd and cleared in Tcl_Finalize.
112 static char *tclLibraryPathStr = NULL;
118 Tcl_ThreadCreateProc *proc; /* Main() function of the thread */
119 ClientData clientData; /* The one argument to Main() */
121 static Tcl_ThreadCreateType NewThreadProc _ANSI_ARGS_((
122 ClientData clientData));
126 * Prototypes for procedures referenced only in this file:
129 static void BgErrorDeleteProc _ANSI_ARGS_((ClientData clientData,
130 Tcl_Interp *interp));
131 static void HandleBgErrors _ANSI_ARGS_((ClientData clientData));
132 static char * VwaitVarProc _ANSI_ARGS_((ClientData clientData,
133 Tcl_Interp *interp, CONST char *name1,
134 CONST char *name2, int flags));
137 *----------------------------------------------------------------------
139 * Tcl_BackgroundError --
141 * This procedure is invoked to handle errors that occur in Tcl
142 * commands that are invoked in "background" (e.g. from event or
149 * The command "bgerror" is invoked later as an idle handler to
150 * process the error, passing it the error message. If that fails,
151 * then an error message is output on stderr.
153 *----------------------------------------------------------------------
157 Tcl_BackgroundError(interp)
158 Tcl_Interp *interp; /* Interpreter in which an error has
162 CONST char *errResult, *varValue;
163 ErrAssocData *assocPtr;
167 * The Tcl_AddErrorInfo call below (with an empty string) ensures that
168 * errorInfo gets properly set. It's needed in cases where the error
169 * came from a utility procedure like Tcl_GetVar instead of Tcl_Eval;
170 * in these cases errorInfo still won't have been set when this
171 * procedure is called.
174 Tcl_AddErrorInfo(interp, "");
176 errResult = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &length);
178 errPtr = (BgError *) ckalloc(sizeof(BgError));
179 errPtr->interp = interp;
180 errPtr->errorMsg = (char *) ckalloc((unsigned) (length + 1));
181 memcpy(errPtr->errorMsg, errResult, (size_t) (length + 1));
182 varValue = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
183 if (varValue == NULL) {
184 varValue = errPtr->errorMsg;
186 errPtr->errorInfo = (char *) ckalloc((unsigned) (strlen(varValue) + 1));
187 strcpy(errPtr->errorInfo, varValue);
188 varValue = Tcl_GetVar(interp, "errorCode", TCL_GLOBAL_ONLY);
189 if (varValue == NULL) {
192 errPtr->errorCode = (char *) ckalloc((unsigned) (strlen(varValue) + 1));
193 strcpy(errPtr->errorCode, varValue);
194 errPtr->nextPtr = NULL;
196 assocPtr = (ErrAssocData *) Tcl_GetAssocData(interp, "tclBgError",
197 (Tcl_InterpDeleteProc **) NULL);
198 if (assocPtr == NULL) {
201 * This is the first time a background error has occurred in
202 * this interpreter. Create associated data to keep track of
203 * pending error reports.
206 assocPtr = (ErrAssocData *) ckalloc(sizeof(ErrAssocData));
207 assocPtr->firstBgPtr = NULL;
208 assocPtr->lastBgPtr = NULL;
209 Tcl_SetAssocData(interp, "tclBgError", BgErrorDeleteProc,
210 (ClientData) assocPtr);
212 if (assocPtr->firstBgPtr == NULL) {
213 assocPtr->firstBgPtr = errPtr;
214 Tcl_DoWhenIdle(HandleBgErrors, (ClientData) assocPtr);
216 assocPtr->lastBgPtr->nextPtr = errPtr;
218 assocPtr->lastBgPtr = errPtr;
219 Tcl_ResetResult(interp);
223 *----------------------------------------------------------------------
227 * This procedure is invoked as an idle handler to process all of
228 * the accumulated background errors.
234 * Depends on what actions "bgerror" takes for the errors.
236 *----------------------------------------------------------------------
240 HandleBgErrors(clientData)
241 ClientData clientData; /* Pointer to ErrAssocData structure. */
247 ErrAssocData *assocPtr = (ErrAssocData *) clientData;
248 Tcl_Channel errChannel;
250 Tcl_Preserve((ClientData) assocPtr);
252 while (assocPtr->firstBgPtr != NULL) {
253 interp = assocPtr->firstBgPtr->interp;
254 if (interp == NULL) {
259 * Restore important state variables to what they were at
260 * the time the error occurred.
263 Tcl_SetVar(interp, "errorInfo", assocPtr->firstBgPtr->errorInfo,
265 Tcl_SetVar(interp, "errorCode", assocPtr->firstBgPtr->errorCode,
269 * Create and invoke the bgerror command.
273 argv[1] = assocPtr->firstBgPtr->errorMsg;
275 Tcl_AllowExceptions(interp);
276 Tcl_Preserve((ClientData) interp);
277 code = TclGlobalInvoke(interp, 2, argv, 0);
278 if (code == TCL_ERROR) {
281 * If the interpreter is safe, we look for a hidden command
282 * named "bgerror" and call that with the error information.
283 * Otherwise, simply ignore the error. The rationale is that
284 * this could be an error caused by a malicious applet trying
285 * to cause an infinite barrage of error messages. The hidden
286 * "bgerror" command can be used by a security policy to
287 * interpose on such attacks and e.g. kill the applet after a
291 if (Tcl_IsSafe(interp)) {
292 Tcl_SavedResult save;
294 Tcl_SaveResult(interp, &save);
295 TclGlobalInvoke(interp, 2, argv, TCL_INVOKE_HIDDEN);
296 Tcl_RestoreResult(interp, &save);
302 * We have to get the error output channel at the latest possible
303 * time, because the eval (above) might have changed the channel.
306 errChannel = Tcl_GetStdChannel(TCL_STDERR);
307 if (errChannel != (Tcl_Channel) NULL) {
311 string = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &len);
312 if (Tcl_FindCommand(interp, "bgerror", NULL, TCL_GLOBAL_ONLY) == NULL) {
313 Tcl_WriteChars(errChannel, assocPtr->firstBgPtr->errorInfo, -1);
314 Tcl_WriteChars(errChannel, "\n", -1);
316 Tcl_WriteChars(errChannel,
317 "bgerror failed to handle background error.\n",
319 Tcl_WriteChars(errChannel, " Original error: ", -1);
320 Tcl_WriteChars(errChannel, assocPtr->firstBgPtr->errorMsg,
322 Tcl_WriteChars(errChannel, "\n", -1);
323 Tcl_WriteChars(errChannel, " Error in bgerror: ", -1);
324 Tcl_WriteChars(errChannel, string, len);
325 Tcl_WriteChars(errChannel, "\n", -1);
327 Tcl_Flush(errChannel);
329 } else if (code == TCL_BREAK) {
332 * Break means cancel any remaining error reports for this
336 for (errPtr = assocPtr->firstBgPtr; errPtr != NULL;
337 errPtr = errPtr->nextPtr) {
338 if (errPtr->interp == interp) {
339 errPtr->interp = NULL;
345 * Discard the command and the information about the error report.
350 if (assocPtr->firstBgPtr) {
351 ckfree(assocPtr->firstBgPtr->errorMsg);
352 ckfree(assocPtr->firstBgPtr->errorInfo);
353 ckfree(assocPtr->firstBgPtr->errorCode);
354 errPtr = assocPtr->firstBgPtr->nextPtr;
355 ckfree((char *) assocPtr->firstBgPtr);
356 assocPtr->firstBgPtr = errPtr;
359 if (interp != NULL) {
360 Tcl_Release((ClientData) interp);
363 assocPtr->lastBgPtr = NULL;
365 Tcl_Release((ClientData) assocPtr);
369 *----------------------------------------------------------------------
371 * BgErrorDeleteProc --
373 * This procedure is associated with the "tclBgError" assoc data
374 * for an interpreter; it is invoked when the interpreter is
375 * deleted in order to free the information assoicated with any
376 * pending error reports.
382 * Background error information is freed: if there were any
383 * pending error reports, they are cancelled.
385 *----------------------------------------------------------------------
389 BgErrorDeleteProc(clientData, interp)
390 ClientData clientData; /* Pointer to ErrAssocData structure. */
391 Tcl_Interp *interp; /* Interpreter being deleted. */
393 ErrAssocData *assocPtr = (ErrAssocData *) clientData;
396 while (assocPtr->firstBgPtr != NULL) {
397 errPtr = assocPtr->firstBgPtr;
398 assocPtr->firstBgPtr = errPtr->nextPtr;
399 ckfree(errPtr->errorMsg);
400 ckfree(errPtr->errorInfo);
401 ckfree(errPtr->errorCode);
402 ckfree((char *) errPtr);
404 Tcl_CancelIdleCall(HandleBgErrors, (ClientData) assocPtr);
405 Tcl_EventuallyFree((ClientData) assocPtr, TCL_DYNAMIC);
409 *----------------------------------------------------------------------
411 * Tcl_CreateExitHandler --
413 * Arrange for a given procedure to be invoked just before the
420 * Proc will be invoked with clientData as argument when the
423 *----------------------------------------------------------------------
427 Tcl_CreateExitHandler(proc, clientData)
428 Tcl_ExitProc *proc; /* Procedure to invoke. */
429 ClientData clientData; /* Arbitrary value to pass to proc. */
431 ExitHandler *exitPtr;
433 exitPtr = (ExitHandler *) ckalloc(sizeof(ExitHandler));
434 exitPtr->proc = proc;
435 exitPtr->clientData = clientData;
436 Tcl_MutexLock(&exitMutex);
437 exitPtr->nextPtr = firstExitPtr;
438 firstExitPtr = exitPtr;
439 Tcl_MutexUnlock(&exitMutex);
443 *----------------------------------------------------------------------
445 * Tcl_DeleteExitHandler --
447 * This procedure cancels an existing exit handler matching proc
448 * and clientData, if such a handler exits.
454 * If there is an exit handler corresponding to proc and clientData
455 * then it is cancelled; if no such handler exists then nothing
458 *----------------------------------------------------------------------
462 Tcl_DeleteExitHandler(proc, clientData)
463 Tcl_ExitProc *proc; /* Procedure that was previously registered. */
464 ClientData clientData; /* Arbitrary value to pass to proc. */
466 ExitHandler *exitPtr, *prevPtr;
468 Tcl_MutexLock(&exitMutex);
469 for (prevPtr = NULL, exitPtr = firstExitPtr; exitPtr != NULL;
470 prevPtr = exitPtr, exitPtr = exitPtr->nextPtr) {
471 if ((exitPtr->proc == proc)
472 && (exitPtr->clientData == clientData)) {
473 if (prevPtr == NULL) {
474 firstExitPtr = exitPtr->nextPtr;
476 prevPtr->nextPtr = exitPtr->nextPtr;
478 ckfree((char *) exitPtr);
482 Tcl_MutexUnlock(&exitMutex);
487 *----------------------------------------------------------------------
489 * Tcl_CreateThreadExitHandler --
491 * Arrange for a given procedure to be invoked just before the
492 * current thread exits.
498 * Proc will be invoked with clientData as argument when the
501 *----------------------------------------------------------------------
505 Tcl_CreateThreadExitHandler(proc, clientData)
506 Tcl_ExitProc *proc; /* Procedure to invoke. */
507 ClientData clientData; /* Arbitrary value to pass to proc. */
509 ExitHandler *exitPtr;
510 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
512 exitPtr = (ExitHandler *) ckalloc(sizeof(ExitHandler));
513 exitPtr->proc = proc;
514 exitPtr->clientData = clientData;
515 exitPtr->nextPtr = tsdPtr->firstExitPtr;
516 tsdPtr->firstExitPtr = exitPtr;
520 *----------------------------------------------------------------------
522 * Tcl_DeleteThreadExitHandler --
524 * This procedure cancels an existing exit handler matching proc
525 * and clientData, if such a handler exits.
531 * If there is an exit handler corresponding to proc and clientData
532 * then it is cancelled; if no such handler exists then nothing
535 *----------------------------------------------------------------------
539 Tcl_DeleteThreadExitHandler(proc, clientData)
540 Tcl_ExitProc *proc; /* Procedure that was previously registered. */
541 ClientData clientData; /* Arbitrary value to pass to proc. */
543 ExitHandler *exitPtr, *prevPtr;
544 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
546 for (prevPtr = NULL, exitPtr = tsdPtr->firstExitPtr; exitPtr != NULL;
547 prevPtr = exitPtr, exitPtr = exitPtr->nextPtr) {
548 if ((exitPtr->proc == proc)
549 && (exitPtr->clientData == clientData)) {
550 if (prevPtr == NULL) {
551 tsdPtr->firstExitPtr = exitPtr->nextPtr;
553 prevPtr->nextPtr = exitPtr->nextPtr;
555 ckfree((char *) exitPtr);
562 *----------------------------------------------------------------------
566 * This procedure is called to terminate the application.
572 * All existing exit handlers are invoked, then the application
575 *----------------------------------------------------------------------
580 int status; /* Exit status for application; typically
581 * 0 for normal return, 1 for error return. */
588 *-------------------------------------------------------------------------
590 * TclSetLibraryPath --
592 * Set the path that will be used for searching for init.tcl and
593 * encodings when an interp is being created.
599 * Changing the library path will affect what directories are
600 * examined when looking for encodings for all interps from that
603 * The refcount of the new library path is incremented and the
604 * refcount of the old path is decremented.
606 *-------------------------------------------------------------------------
610 TclSetLibraryPath(pathPtr)
611 Tcl_Obj *pathPtr; /* A Tcl list object whose elements are
612 * the new library path. */
614 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
618 if (pathPtr != NULL) {
619 Tcl_IncrRefCount(pathPtr);
621 if (tsdPtr->tclLibraryPath != NULL) {
622 Tcl_DecrRefCount(tsdPtr->tclLibraryPath);
624 tsdPtr->tclLibraryPath = pathPtr;
627 * No mutex locking is needed here as up the stack we're within
630 if (tclLibraryPathStr != NULL) {
631 ckfree(tclLibraryPathStr);
633 toDupe = Tcl_GetStringFromObj(pathPtr, &size);
634 tclLibraryPathStr = ckalloc((unsigned)size+1);
635 memcpy(tclLibraryPathStr, toDupe, (unsigned)size+1);
639 *-------------------------------------------------------------------------
641 * TclGetLibraryPath --
643 * Return a Tcl list object whose elements are the library path.
644 * The caller should not modify the contents of the returned object.
652 *-------------------------------------------------------------------------
658 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
660 if (tsdPtr->tclLibraryPath == NULL) {
662 * Grab the shared string and place it into a new thread specific
665 tsdPtr->tclLibraryPath = Tcl_NewStringObj(tclLibraryPathStr, -1);
668 Tcl_IncrRefCount(tsdPtr->tclLibraryPath);
670 return tsdPtr->tclLibraryPath;
674 *-------------------------------------------------------------------------
676 * TclInitSubsystems --
678 * Initialize various subsytems in Tcl. This should be called the
679 * first time an interp is created, or before any of the subsystems
680 * are used. This function ensures an order for the initialization
683 * 1. that cannot be initialized in lazy order because they are
684 * mutually dependent.
686 * 2. so that they can be finalized in a known order w/o causing
687 * the subsequent re-initialization of a subsystem in the act of
688 * shutting down another.
694 * Varied, see the respective initialization routines.
696 *-------------------------------------------------------------------------
700 TclInitSubsystems(argv0)
701 CONST char *argv0; /* Name of executable from argv[0] to main()
702 * in native multi-byte encoding. */
704 ThreadSpecificData *tsdPtr;
706 if (inFinalize != 0) {
707 panic("TclInitSubsystems called while finalizing");
711 * Grab the thread local storage pointer before doing anything because
712 * the initialization routines will be registering exit handlers.
713 * We use this pointer to detect if this is the first time this
714 * thread has created an interpreter.
717 tsdPtr = (ThreadSpecificData *) TclThreadDataKeyGet(&dataKey);
719 if (subsystemsInitialized == 0) {
721 * Double check inside the mutex. There are definitly calls
722 * back into this routine from some of the procedures below.
726 if (subsystemsInitialized == 0) {
728 * Have to set this bit here to avoid deadlock with the
729 * routines below us that call into TclInitSubsystems.
732 subsystemsInitialized = 1;
734 tclExecutableName = NULL;
737 * Initialize locks used by the memory allocators before anything
738 * interesting happens so we can use the allocators in the
739 * implementation of self-initializing locks.
743 TclInitAlloc(); /* process wide mutex init */
746 TclInitDbCkalloc(); /* process wide mutex init */
749 TclpInitPlatform(); /* creates signal handler(s) */
750 TclInitObjSubsystem(); /* register obj types, create mutexes */
751 TclInitIOSubsystem(); /* inits a tsd key (noop) */
752 TclInitEncodingSubsystem(); /* process wide encoding init */
753 TclInitNamespaceSubsystem(); /* register ns obj type (mutexed) */
758 if (tsdPtr == NULL) {
760 * First time this thread has created an interpreter.
761 * We fetch the key again just in case no exit handlers were
762 * registered by this point.
765 (void) TCL_TSD_INIT(&dataKey);
771 *----------------------------------------------------------------------
775 * Shut down Tcl. First calls registered exit handlers, then
776 * carefully shuts down various subsystems.
777 * Called by Tcl_Exit or when the Tcl shared library is being
784 * Varied, see the respective finalization routines.
786 *----------------------------------------------------------------------
792 ExitHandler *exitPtr;
795 * Invoke exit handlers first.
798 Tcl_MutexLock(&exitMutex);
800 for (exitPtr = firstExitPtr; exitPtr != NULL; exitPtr = firstExitPtr) {
802 * Be careful to remove the handler from the list before
803 * invoking its callback. This protects us against
804 * double-freeing if the callback should call
805 * Tcl_DeleteExitHandler on itself.
808 firstExitPtr = exitPtr->nextPtr;
809 Tcl_MutexUnlock(&exitMutex);
810 (*exitPtr->proc)(exitPtr->clientData);
811 ckfree((char *) exitPtr);
812 Tcl_MutexLock(&exitMutex);
815 Tcl_MutexUnlock(&exitMutex);
818 if (subsystemsInitialized != 0) {
819 subsystemsInitialized = 0;
822 * Ensure the thread-specific data is initialised as it is
823 * used in Tcl_FinalizeThread()
826 (void) TCL_TSD_INIT(&dataKey);
829 * Clean up after the current thread now, after exit handlers.
830 * In particular, the testexithandler command sets up something
831 * that writes to standard output, which gets closed.
832 * Note that there is no thread-local storage after this call.
835 Tcl_FinalizeThread();
838 * Now finalize the Tcl execution environment. Note that this
839 * must be done after the exit handlers, because there are
840 * order dependencies.
843 TclFinalizeCompilation();
844 TclFinalizeExecution();
845 TclFinalizeEnvironment();
848 * Finalizing the filesystem must come after anything which
849 * might conceivably interact with the 'Tcl_FS' API.
852 TclFinalizeFilesystem();
855 * Undo all the Tcl_ObjType registrations, and reset the master list
856 * of free Tcl_Obj's. After this returns, no more Tcl_Obj's should
857 * be allocated or freed.
859 * Note in particular that TclFinalizeObjects() must follow
860 * TclFinalizeFilesystem() because TclFinalizeFilesystem free's
861 * the Tcl_Obj that holds the path of the current working directory.
864 TclFinalizeObjects();
867 * We must be sure the encoding finalization doesn't need
868 * to examine the filesystem in any way. Since it only
869 * needs to clean up internal data structures, this is
872 TclFinalizeEncodingSubsystem();
874 if (tclExecutableName != NULL) {
875 ckfree(tclExecutableName);
876 tclExecutableName = NULL;
878 if (tclNativeExecutableName != NULL) {
879 ckfree(tclNativeExecutableName);
880 tclNativeExecutableName = NULL;
882 if (tclDefaultEncodingDir != NULL) {
883 ckfree(tclDefaultEncodingDir);
884 tclDefaultEncodingDir = NULL;
886 if (tclLibraryPathStr != NULL) {
887 ckfree(tclLibraryPathStr);
888 tclLibraryPathStr = NULL;
891 Tcl_SetPanicProc(NULL);
894 * There have been several bugs in the past that cause
895 * exit handlers to be established during Tcl_Finalize
896 * processing. Such exit handlers leave malloc'ed memory,
897 * and Tcl_FinalizeThreadAlloc or Tcl_FinalizeMemorySubsystem
898 * will result in a corrupted heap. The result can be a
899 * mysterious crash on process exit. Check here that
900 * nobody's done this.
904 if ( firstExitPtr != NULL ) {
905 Tcl_Panic( "exit handlers were created during Tcl_Finalize" );
909 TclFinalizePreserve();
912 * Free synchronization objects. There really should only be one
913 * thread alive at this moment.
916 TclFinalizeSynchronization();
918 #if defined(TCL_THREADS) && defined(USE_THREAD_ALLOC) && !defined(TCL_MEM_DEBUG) && !defined(PURIFY)
919 TclFinalizeThreadAlloc();
923 * We defer unloading of packages until very late
924 * to avoid memory access issues. Both exit callbacks and
925 * synchronization variables may be stored in packages.
927 * Note that TclFinalizeLoad unloads packages in the reverse
928 * of the order they were loaded in (i.e. last to be loaded
929 * is the first to be unloaded). This can be important for
930 * correct unloading when dependencies exist.
932 * Once load has been finalized, we will have deleted any
933 * temporary copies of shared libraries and can therefore
934 * reset the filesystem to its original state.
938 TclResetFilesystem();
941 * At this point, there should no longer be any ckalloc'ed memory.
944 TclFinalizeMemorySubsystem();
951 *----------------------------------------------------------------------
953 * Tcl_FinalizeThread --
955 * Runs the exit handlers to allow Tcl to clean up its state
956 * about a particular thread.
962 * Varied, see the respective finalization routines.
964 *----------------------------------------------------------------------
970 ExitHandler *exitPtr;
971 ThreadSpecificData *tsdPtr;
973 tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
974 if (tsdPtr != NULL) {
978 * Clean up the library path now, before we invalidate thread-local
979 * storage or calling thread exit handlers.
982 if (tsdPtr->tclLibraryPath != NULL) {
983 Tcl_DecrRefCount(tsdPtr->tclLibraryPath);
984 tsdPtr->tclLibraryPath = NULL;
987 for (exitPtr = tsdPtr->firstExitPtr; exitPtr != NULL;
988 exitPtr = tsdPtr->firstExitPtr) {
990 * Be careful to remove the handler from the list before invoking
991 * its callback. This protects us against double-freeing if the
992 * callback should call Tcl_DeleteThreadExitHandler on itself.
995 tsdPtr->firstExitPtr = exitPtr->nextPtr;
996 (*exitPtr->proc)(exitPtr->clientData);
997 ckfree((char *) exitPtr);
999 TclFinalizeIOSubsystem();
1000 TclFinalizeNotifier();
1005 * Blow away all thread local storage blocks.
1007 * Note that Tcl API allows creation of threads which do not use any
1008 * Tcl interp or other Tcl subsytems. Those threads might, however,
1009 * use thread local storage, so we must unconditionally finalize it.
1014 TclFinalizeThreadData();
1018 *----------------------------------------------------------------------
1022 * Determines if we are in the middle of exit-time cleanup.
1025 * If we are in the middle of exiting, 1, otherwise 0.
1030 *----------------------------------------------------------------------
1040 *----------------------------------------------------------------------
1042 * TclInThreadExit --
1044 * Determines if we are in the middle of thread exit-time cleanup.
1047 * If we are in the middle of exiting this thread, 1, otherwise 0.
1052 *----------------------------------------------------------------------
1058 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1059 TclThreadDataKeyGet(&dataKey);
1060 if (tsdPtr == NULL) {
1063 return tsdPtr->inExit;
1068 *----------------------------------------------------------------------
1070 * Tcl_VwaitObjCmd --
1072 * This procedure is invoked to process the "vwait" Tcl command.
1073 * See the user documentation for details on what it does.
1076 * A standard Tcl result.
1079 * See the user documentation.
1081 *----------------------------------------------------------------------
1086 Tcl_VwaitObjCmd(clientData, interp, objc, objv)
1087 ClientData clientData; /* Not used. */
1088 Tcl_Interp *interp; /* Current interpreter. */
1089 int objc; /* Number of arguments. */
1090 Tcl_Obj *CONST objv[]; /* Argument objects. */
1092 int done, foundEvent;
1096 Tcl_WrongNumArgs(interp, 1, objv, "name");
1099 nameString = Tcl_GetString(objv[1]);
1100 if (Tcl_TraceVar(interp, nameString,
1101 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1102 VwaitVarProc, (ClientData) &done) != TCL_OK) {
1107 while (!done && foundEvent) {
1108 foundEvent = Tcl_DoOneEvent(TCL_ALL_EVENTS);
1110 Tcl_UntraceVar(interp, nameString,
1111 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1112 VwaitVarProc, (ClientData) &done);
1115 * Clear out the interpreter's result, since it may have been set
1116 * by event handlers.
1119 Tcl_ResetResult(interp);
1121 Tcl_AppendResult(interp, "can't wait for variable \"", nameString,
1122 "\": would wait forever", (char *) NULL);
1130 VwaitVarProc(clientData, interp, name1, name2, flags)
1131 ClientData clientData; /* Pointer to integer to set to 1. */
1132 Tcl_Interp *interp; /* Interpreter containing variable. */
1133 CONST char *name1; /* Name of variable. */
1134 CONST char *name2; /* Second part of variable name. */
1135 int flags; /* Information about what happened. */
1137 int *donePtr = (int *) clientData;
1140 return (char *) NULL;
1144 *----------------------------------------------------------------------
1146 * Tcl_UpdateObjCmd --
1148 * This procedure is invoked to process the "update" Tcl command.
1149 * See the user documentation for details on what it does.
1152 * A standard Tcl result.
1155 * See the user documentation.
1157 *----------------------------------------------------------------------
1162 Tcl_UpdateObjCmd(clientData, interp, objc, objv)
1163 ClientData clientData; /* Not used. */
1164 Tcl_Interp *interp; /* Current interpreter. */
1165 int objc; /* Number of arguments. */
1166 Tcl_Obj *CONST objv[]; /* Argument objects. */
1169 int flags = 0; /* Initialized to avoid compiler warning. */
1170 static CONST char *updateOptions[] = {"idletasks", (char *) NULL};
1171 enum updateOptions {REGEXP_IDLETASKS};
1174 flags = TCL_ALL_EVENTS|TCL_DONT_WAIT;
1175 } else if (objc == 2) {
1176 if (Tcl_GetIndexFromObj(interp, objv[1], updateOptions,
1177 "option", 0, &optionIndex) != TCL_OK) {
1180 switch ((enum updateOptions) optionIndex) {
1181 case REGEXP_IDLETASKS: {
1182 flags = TCL_WINDOW_EVENTS|TCL_IDLE_EVENTS|TCL_DONT_WAIT;
1186 panic("Tcl_UpdateObjCmd: bad option index to UpdateOptions");
1190 Tcl_WrongNumArgs(interp, 1, objv, "?idletasks?");
1194 while (Tcl_DoOneEvent(flags) != 0) {
1195 /* Empty loop body */
1199 * Must clear the interpreter's result because event handlers could
1200 * have executed commands.
1203 Tcl_ResetResult(interp);
1209 *-----------------------------------------------------------------------------
1213 * Bootstrap function of a new Tcl thread.
1219 * Initializes Tcl notifier for the current thread.
1221 *-----------------------------------------------------------------------------
1224 static Tcl_ThreadCreateType
1225 NewThreadProc(ClientData clientData)
1227 ThreadClientData *cdPtr;
1228 ClientData threadClientData;
1229 Tcl_ThreadCreateProc *threadProc;
1231 cdPtr = (ThreadClientData*)clientData;
1232 threadProc = cdPtr->proc;
1233 threadClientData = cdPtr->clientData;
1234 ckfree((char*)clientData); /* Allocated in Tcl_CreateThread() */
1236 (*threadProc)(threadClientData);
1238 TCL_THREAD_CREATE_RETURN;
1242 *----------------------------------------------------------------------
1244 * Tcl_CreateThread --
1246 * This procedure creates a new thread. This actually belongs
1247 * to the tclThread.c file but since we use some private
1248 * data structures local to this file, it is placed here.
1251 * TCL_OK if the thread could be created. The thread ID is
1252 * returned in a parameter.
1255 * A new thread is created.
1257 *----------------------------------------------------------------------
1261 Tcl_CreateThread(idPtr, proc, clientData, stackSize, flags)
1262 Tcl_ThreadId *idPtr; /* Return, the ID of the thread */
1263 Tcl_ThreadCreateProc proc; /* Main() function of the thread */
1264 ClientData clientData; /* The one argument to Main() */
1265 int stackSize; /* Size of stack for the new thread */
1266 int flags; /* Flags controlling behaviour of
1270 ThreadClientData *cdPtr;
1272 cdPtr = (ThreadClientData*)ckalloc(sizeof(ThreadClientData));
1274 cdPtr->clientData = clientData;
1276 return TclpThreadCreate(idPtr, NewThreadProc, (ClientData)cdPtr,
1280 #endif /* TCL_THREADS */