Debian Codegraph @

generic/tclEvent.c

raw
1 /*
2 * tclEvent.c --
3 *
4 * This file implements some general event related interfaces including
5 * background errors, exit handlers, and the "vwait" and "update" command
6 * functions.
7 *
8 * Copyright (c) 1990-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
10 * Copyright (c) 2004 by Zoran Vasiljevic.
11 *
12 * See the file "license.terms" for information on usage and redistribution of
13 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 */
15
16 #include "tclInt.h"
17
18 /*
19 * The data structure below is used to report background errors. One such
20 * structure is allocated for each error; it holds information about the
21 * interpreter and the error until an idle handler command can be invoked.
22 */
23
24 typedef struct BgError {
25 Tcl_Obj *errorMsg; /* Copy of the error message (the interp's
26 * result when the error occurred). */
27 Tcl_Obj *returnOpts; /* Active return options when the error
28 * occurred */
29 struct BgError *nextPtr; /* Next in list of all pending error reports
30 * for this interpreter, or NULL for end of
31 * list. */
32 } BgError;
33
34 /*
35 * One of the structures below is associated with the "tclBgError" assoc data
36 * for each interpreter. It keeps track of the head and tail of the list of
37 * pending background errors for the interpreter.
38 */
39
40 typedef struct {
41 Tcl_Interp *interp; /* Interpreter in which error occurred. */
42 Tcl_Obj *cmdPrefix; /* First word(s) of the handler command */
43 BgError *firstBgPtr; /* First in list of all background errors
44 * waiting to be processed for this
45 * interpreter (NULL if none). */
46 BgError *lastBgPtr; /* Last in list of all background errors
47 * waiting to be processed for this
48 * interpreter (NULL if none). */
49 } ErrAssocData;
50
51 /*
52 * For each exit handler created with a call to Tcl_Create(Late)ExitHandler
53 * there is a structure of the following type:
54 */
55
56 typedef struct ExitHandler {
57 Tcl_ExitProc *proc; /* Function to call when process exits. */
58 ClientData clientData; /* One word of information to pass to proc. */
59 struct ExitHandler *nextPtr;/* Next in list of all exit handlers for this
60 * application, or NULL for end of list. */
61 } ExitHandler;
62
63 /*
64 * There is both per-process and per-thread exit handlers. The first list is
65 * controlled by a mutex. The other is in thread local storage.
66 */
67
68 static ExitHandler *firstExitPtr = NULL;
69 /* First in list of all exit handlers for
70 * application. */
71 static ExitHandler *firstLateExitPtr = NULL;
72 /* First in list of all late exit handlers for
73 * application. */
74 TCL_DECLARE_MUTEX(exitMutex)
75
76 /*
77 * This variable is set to 1 when Tcl_Exit is called. The variable is checked
78 * by TclInExit() to allow different behavior for exit-time processing, e.g.,
79 * in closing of files and pipes.
80 */
81
82 static int inExit = 0;
83
84 static int subsystemsInitialized = 0;
85
86 /*
87 * This variable contains the application wide exit handler. It will be called
88 * by Tcl_Exit instead of the C-runtime exit if this variable is set to a
89 * non-NULL value.
90 */
91
92 static Tcl_ExitProc *appExitPtr = NULL;
93
94 typedef struct ThreadSpecificData {
95 ExitHandler *firstExitPtr; /* First in list of all exit handlers for this
96 * thread. */
97 int inExit; /* True when this thread is exiting. This is
98 * used as a hack to decide to close the
99 * standard channels. */
100 } ThreadSpecificData;
101 static Tcl_ThreadDataKey dataKey;
102
103 #ifdef TCL_THREADS
104 typedef struct {
105 Tcl_ThreadCreateProc *proc; /* Main() function of the thread */
106 ClientData clientData; /* The one argument to Main() */
107 } ThreadClientData;
108 static Tcl_ThreadCreateType NewThreadProc(ClientData clientData);
109 #endif /* TCL_THREADS */
110
111 /*
112 * Prototypes for functions referenced only in this file:
113 */
114
115 static void BgErrorDeleteProc(ClientData clientData,
116 Tcl_Interp *interp);
117 static void HandleBgErrors(ClientData clientData);
118 static char * VwaitVarProc(ClientData clientData,
119 Tcl_Interp *interp, const char *name1,
120 const char *name2, int flags);
121 static void InvokeExitHandlers(void);
122 static void FinalizeThread(int quick);
123
124 /*
125 *----------------------------------------------------------------------
126 *
127 * Tcl_BackgroundError --
128 *
129 * This function is invoked to handle errors that occur in Tcl commands
130 * that are invoked in "background" (e.g. from event or timer bindings).
131 *
132 * Results:
133 * None.
134 *
135 * Side effects:
136 * A handler command is invoked later as an idle handler to process the
137 * error, passing it the interp result and return options.
138 *
139 *----------------------------------------------------------------------
140 */
141
142 void
143 Tcl_BackgroundError(
144 Tcl_Interp *interp) /* Interpreter in which an error has
145 * occurred. */
146 {
147 Tcl_BackgroundException(interp, TCL_ERROR);
148 }
149
150 void
151 Tcl_BackgroundException(
152 Tcl_Interp *interp, /* Interpreter in which an exception has
153 * occurred. */
154 int code) /* The exception code value */
155 {
156 BgError *errPtr;
157 ErrAssocData *assocPtr;
158
159 if (code == TCL_OK) {
160 return;
161 }
162
163 errPtr = (BgError*)ckalloc(sizeof(BgError));
164 errPtr->errorMsg = Tcl_GetObjResult(interp);
165 Tcl_IncrRefCount(errPtr->errorMsg);
166 errPtr->returnOpts = Tcl_GetReturnOptions(interp, code);
167 Tcl_IncrRefCount(errPtr->returnOpts);
168 errPtr->nextPtr = NULL;
169
170 (void) TclGetBgErrorHandler(interp);
171 assocPtr = (ErrAssocData *)Tcl_GetAssocData(interp, "tclBgError", NULL);
172 if (assocPtr->firstBgPtr == NULL) {
173 assocPtr->firstBgPtr = errPtr;
174 Tcl_DoWhenIdle(HandleBgErrors, assocPtr);
175 } else {
176 assocPtr->lastBgPtr->nextPtr = errPtr;
177 }
178 assocPtr->lastBgPtr = errPtr;
179 Tcl_ResetResult(interp);
180 }
181
182 /*
183 *----------------------------------------------------------------------
184 *
185 * HandleBgErrors --
186 *
187 * This function is invoked as an idle handler to process all of the
188 * accumulated background errors.
189 *
190 * Results:
191 * None.
192 *
193 * Side effects:
194 * Depends on what actions the handler command takes for the errors.
195 *
196 *----------------------------------------------------------------------
197 */
198
199 static void
200 HandleBgErrors(
201 ClientData clientData) /* Pointer to ErrAssocData structure. */
202 {
203 ErrAssocData *assocPtr = (ErrAssocData *)clientData;
204 Tcl_Interp *interp = assocPtr->interp;
205 BgError *errPtr;
206
207 /*
208 * Not bothering to save/restore the interp state. Assume that any code
209 * that has interp state it needs to keep will make its own
210 * Tcl_SaveInterpState call before calling something like Tcl_DoOneEvent()
211 * that could lead us here.
212 */
213
214 Tcl_Preserve(assocPtr);
215 Tcl_Preserve(interp);
216 while (assocPtr->firstBgPtr != NULL) {
217 int code, prefixObjc;
218 Tcl_Obj **prefixObjv, **tempObjv;
219
220 /*
221 * Note we copy the handler command prefix each pass through, so we do
222 * support one handler setting another handler.
223 */
224
225 Tcl_Obj *copyObj = TclListObjCopy(NULL, assocPtr->cmdPrefix);
226
227 errPtr = assocPtr->firstBgPtr;
228
229 TclListObjGetElements(NULL, copyObj, &prefixObjc, &prefixObjv);
230 tempObjv = (Tcl_Obj**)ckalloc((prefixObjc+2) * sizeof(Tcl_Obj *));
231 memcpy(tempObjv, prefixObjv, prefixObjc*sizeof(Tcl_Obj *));
232 tempObjv[prefixObjc] = errPtr->errorMsg;
233 tempObjv[prefixObjc+1] = errPtr->returnOpts;
234 Tcl_AllowExceptions(interp);
235 code = Tcl_EvalObjv(interp, prefixObjc+2, tempObjv, TCL_EVAL_GLOBAL);
236
237 /*
238 * Discard the command and the information about the error report.
239 */
240
241 Tcl_DecrRefCount(copyObj);
242 Tcl_DecrRefCount(errPtr->errorMsg);
243 Tcl_DecrRefCount(errPtr->returnOpts);
244 assocPtr->firstBgPtr = errPtr->nextPtr;
245 ckfree(errPtr);
246 ckfree(tempObjv);
247
248 if (code == TCL_BREAK) {
249 /*
250 * Break means cancel any remaining error reports for this
251 * interpreter.
252 */
253
254 while (assocPtr->firstBgPtr != NULL) {
255 errPtr = assocPtr->firstBgPtr;
256 assocPtr->firstBgPtr = errPtr->nextPtr;
257 Tcl_DecrRefCount(errPtr->errorMsg);
258 Tcl_DecrRefCount(errPtr->returnOpts);
259 ckfree(errPtr);
260 }
261 } else if ((code == TCL_ERROR) && !Tcl_IsSafe(interp)) {
262 Tcl_Channel errChannel = Tcl_GetStdChannel(TCL_STDERR);
263
264 if (errChannel != NULL) {
265 Tcl_Obj *options = Tcl_GetReturnOptions(interp, code);
266 Tcl_Obj *valuePtr = NULL;
267
268 TclDictGet(NULL, options, "-errorinfo", &valuePtr);
269 Tcl_WriteChars(errChannel,
270 "error in background error handler:\n", -1);
271 if (valuePtr) {
272 Tcl_WriteObj(errChannel, valuePtr);
273 } else {
274 Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp));
275 }
276 Tcl_WriteChars(errChannel, "\n", 1);
277 Tcl_Flush(errChannel);
278 Tcl_DecrRefCount(options);
279 }
280 }
281 }
282 assocPtr->lastBgPtr = NULL;
283 Tcl_Release(interp);
284 Tcl_Release(assocPtr);
285 }
286
287 /*
288 *----------------------------------------------------------------------
289 *
290 * TclDefaultBgErrorHandlerObjCmd --
291 *
292 * This function is invoked to process the "::tcl::Bgerror" Tcl command.
293 * It is the default handler command registered with [interp bgerror] for
294 * the sake of compatibility with older Tcl releases.
295 *
296 * Results:
297 * A standard Tcl object result.
298 *
299 * Side effects:
300 * Depends on what actions the "bgerror" command takes for the errors.
301 *
302 *----------------------------------------------------------------------
303 */
304
305 int
306 TclDefaultBgErrorHandlerObjCmd(
307 ClientData dummy, /* Not used. */
308 Tcl_Interp *interp, /* Current interpreter. */
309 int objc, /* Number of arguments. */
310 Tcl_Obj *const objv[]) /* Argument objects. */
311 {
312 Tcl_Obj *valuePtr;
313 Tcl_Obj *tempObjv[2];
314 int result, code, level;
315 Tcl_InterpState saved;
316
317 if (objc != 3) {
318 Tcl_WrongNumArgs(interp, 1, objv, "msg options");
319 return TCL_ERROR;
320 }
321
322 /*
323 * Check for a valid return options dictionary.
324 */
325
326 result = TclDictGet(NULL, objv[2], "-level", &valuePtr);
327 if (result != TCL_OK || valuePtr == NULL) {
328 Tcl_SetObjResult(interp, Tcl_NewStringObj(
329 "missing return option \"-level\"", -1));
330 Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL);
331 return TCL_ERROR;
332 }
333 if (Tcl_GetIntFromObj(interp, valuePtr, &level) == TCL_ERROR) {
334 return TCL_ERROR;
335 }
336 result = TclDictGet(NULL, objv[2], "-code", &valuePtr);
337 if (result != TCL_OK || valuePtr == NULL) {
338 Tcl_SetObjResult(interp, Tcl_NewStringObj(
339 "missing return option \"-code\"", -1));
340 Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL);
341 return TCL_ERROR;
342 }
343 if (Tcl_GetIntFromObj(interp, valuePtr, &code) == TCL_ERROR) {
344 return TCL_ERROR;
345 }
346
347 if (level != 0) {
348 /*
349 * We're handling a TCL_RETURN exception.
350 */
351
352 code = TCL_RETURN;
353 }
354 if (code == TCL_OK) {
355 /*
356 * Somehow we got to exception handling with no exception. (Pass
357 * TCL_OK to Tcl_BackgroundException()?) Just return without doing
358 * anything.
359 */
360
361 return TCL_OK;
362 }
363
364 /*
365 * Construct the bgerror command.
366 */
367
368 TclNewLiteralStringObj(tempObjv[0], "bgerror");
369 Tcl_IncrRefCount(tempObjv[0]);
370
371 /*
372 * Determine error message argument. Check the return options in case
373 * a non-error exception brought us here.
374 */
375
376 switch (code) {
377 case TCL_ERROR:
378 tempObjv[1] = objv[1];
379 break;
380 case TCL_BREAK:
381 TclNewLiteralStringObj(tempObjv[1],
382 "invoked \"break\" outside of a loop");
383 break;
384 case TCL_CONTINUE:
385 TclNewLiteralStringObj(tempObjv[1],
386 "invoked \"continue\" outside of a loop");
387 break;
388 default:
389 tempObjv[1] = Tcl_ObjPrintf("command returned bad code: %d", code);
390 break;
391 }
392 Tcl_IncrRefCount(tempObjv[1]);
393
394 if (code != TCL_ERROR) {
395 Tcl_SetObjResult(interp, tempObjv[1]);
396 }
397
398 result = TclDictGet(NULL, objv[2], "-errorcode", &valuePtr);
399 if (result == TCL_OK && valuePtr != NULL) {
400 Tcl_SetObjErrorCode(interp, valuePtr);
401 }
402
403 result = TclDictGet(NULL, objv[2], "-errorinfo", &valuePtr);
404 if (result == TCL_OK && valuePtr != NULL) {
405 Tcl_AppendObjToErrorInfo(interp, valuePtr);
406 }
407
408 if (code == TCL_ERROR) {
409 Tcl_SetObjResult(interp, tempObjv[1]);
410 }
411
412 /*
413 * Save interpreter state so we can restore it if multiple handler
414 * attempts are needed.
415 */
416
417 saved = Tcl_SaveInterpState(interp, code);
418
419 /*
420 * Invoke the bgerror command.
421 */
422
423 Tcl_AllowExceptions(interp);
424 code = Tcl_EvalObjv(interp, 2, tempObjv, TCL_EVAL_GLOBAL);
425 if (code == TCL_ERROR) {
426 /*
427 * If the interpreter is safe, we look for a hidden command named
428 * "bgerror" and call that with the error information. Otherwise,
429 * simply ignore the error. The rationale is that this could be an
430 * error caused by a malicious applet trying to cause an infinite
431 * barrage of error messages. The hidden "bgerror" command can be used
432 * by a security policy to interpose on such attacks and e.g. kill the
433 * applet after a few attempts.
434 */
435
436 if (Tcl_IsSafe(interp)) {
437 Tcl_RestoreInterpState(interp, saved);
438 TclObjInvoke(interp, 2, tempObjv, TCL_INVOKE_HIDDEN);
439 } else {
440 Tcl_Channel errChannel = Tcl_GetStdChannel(TCL_STDERR);
441
442 if (errChannel != NULL) {
443 Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
444
445 Tcl_IncrRefCount(resultPtr);
446 if (Tcl_FindCommand(interp, "bgerror", NULL,
447 TCL_GLOBAL_ONLY) == NULL) {
448 Tcl_RestoreInterpState(interp, saved);
449 Tcl_WriteObj(errChannel, Tcl_GetVar2Ex(interp,
450 "errorInfo", NULL, TCL_GLOBAL_ONLY));
451 Tcl_WriteChars(errChannel, "\n", -1);
452 } else {
453 Tcl_DiscardInterpState(saved);
454 Tcl_WriteChars(errChannel,
455 "bgerror failed to handle background error.\n",-1);
456 Tcl_WriteChars(errChannel, " Original error: ", -1);
457 Tcl_WriteObj(errChannel, tempObjv[1]);
458 Tcl_WriteChars(errChannel, "\n", -1);
459 Tcl_WriteChars(errChannel, " Error in bgerror: ", -1);
460 Tcl_WriteObj(errChannel, resultPtr);
461 Tcl_WriteChars(errChannel, "\n", -1);
462 }
463 Tcl_DecrRefCount(resultPtr);
464 Tcl_Flush(errChannel);
465 } else {
466 Tcl_DiscardInterpState(saved);
467 }
468 }
469 code = TCL_OK;
470 } else {
471 Tcl_DiscardInterpState(saved);
472 }
473
474 Tcl_DecrRefCount(tempObjv[0]);
475 Tcl_DecrRefCount(tempObjv[1]);
476 Tcl_ResetResult(interp);
477 return code;
478 }
479
480 /*
481 *----------------------------------------------------------------------
482 *
483 * TclSetBgErrorHandler --
484 *
485 * This function sets the command prefix to be used to handle background
486 * errors in interp.
487 *
488 * Results:
489 * None.
490 *
491 * Side effects:
492 * Error handler is registered.
493 *
494 *----------------------------------------------------------------------
495 */
496
497 void
498 TclSetBgErrorHandler(
499 Tcl_Interp *interp,
500 Tcl_Obj *cmdPrefix)
501 {
502 ErrAssocData *assocPtr = (ErrAssocData *)Tcl_GetAssocData(interp, "tclBgError", NULL);
503
504 if (cmdPrefix == NULL) {
505 Tcl_Panic("TclSetBgErrorHandler: NULL cmdPrefix argument");
506 }
507 if (assocPtr == NULL) {
508 /*
509 * First access: initialize.
510 */
511
512 assocPtr = (ErrAssocData*)ckalloc(sizeof(ErrAssocData));
513 assocPtr->interp = interp;
514 assocPtr->cmdPrefix = NULL;
515 assocPtr->firstBgPtr = NULL;
516 assocPtr->lastBgPtr = NULL;
517 Tcl_SetAssocData(interp, "tclBgError", BgErrorDeleteProc, assocPtr);
518 }
519 if (assocPtr->cmdPrefix) {
520 Tcl_DecrRefCount(assocPtr->cmdPrefix);
521 }
522 assocPtr->cmdPrefix = cmdPrefix;
523 Tcl_IncrRefCount(assocPtr->cmdPrefix);
524 }
525
526 /*
527 *----------------------------------------------------------------------
528 *
529 * TclGetBgErrorHandler --
530 *
531 * This function retrieves the command prefix currently used to handle
532 * background errors in interp.
533 *
534 * Results:
535 * A (Tcl_Obj *) to a list of words (command prefix).
536 *
537 * Side effects:
538 * None.
539 *
540 *----------------------------------------------------------------------
541 */
542
543 Tcl_Obj *
544 TclGetBgErrorHandler(
545 Tcl_Interp *interp)
546 {
547 ErrAssocData *assocPtr = (ErrAssocData *)Tcl_GetAssocData(interp, "tclBgError", NULL);
548
549 if (assocPtr == NULL) {
550 Tcl_Obj *bgerrorObj;
551
552 TclNewLiteralStringObj(bgerrorObj, "::tcl::Bgerror");
553 TclSetBgErrorHandler(interp, bgerrorObj);
554 assocPtr = (ErrAssocData *)Tcl_GetAssocData(interp, "tclBgError", NULL);
555 }
556 return assocPtr->cmdPrefix;
557 }
558
559 /*
560 *----------------------------------------------------------------------
561 *
562 * BgErrorDeleteProc --
563 *
564 * This function is associated with the "tclBgError" assoc data for an
565 * interpreter; it is invoked when the interpreter is deleted in order to
566 * free the information associated with any pending error reports.
567 *
568 * Results:
569 * None.
570 *
571 * Side effects:
572 * Background error information is freed: if there were any pending error
573 * reports, they are canceled.
574 *
575 *----------------------------------------------------------------------
576 */
577
578 static void
579 BgErrorDeleteProc(
580 ClientData clientData, /* Pointer to ErrAssocData structure. */
581 Tcl_Interp *interp) /* Interpreter being deleted. */
582 {
583 ErrAssocData *assocPtr = (ErrAssocData *)clientData;
584 BgError *errPtr;
585
586 while (assocPtr->firstBgPtr != NULL) {
587 errPtr = assocPtr->firstBgPtr;
588 assocPtr->firstBgPtr = errPtr->nextPtr;
589 Tcl_DecrRefCount(errPtr->errorMsg);
590 Tcl_DecrRefCount(errPtr->returnOpts);
591 ckfree(errPtr);
592 }
593 Tcl_CancelIdleCall(HandleBgErrors, assocPtr);
594 Tcl_DecrRefCount(assocPtr->cmdPrefix);
595 Tcl_EventuallyFree(assocPtr, TCL_DYNAMIC);
596 }
597
598 /*
599 *----------------------------------------------------------------------
600 *
601 * Tcl_CreateExitHandler --
602 *
603 * Arrange for a given function to be invoked just before the application
604 * exits.
605 *
606 * Results:
607 * None.
608 *
609 * Side effects:
610 * Proc will be invoked with clientData as argument when the application
611 * exits.
612 *
613 *----------------------------------------------------------------------
614 */
615
616 void
617 Tcl_CreateExitHandler(
618 Tcl_ExitProc *proc, /* Function to invoke. */
619 ClientData clientData) /* Arbitrary value to pass to proc. */
620 {
621 ExitHandler *exitPtr = (ExitHandler*)ckalloc(sizeof(ExitHandler));
622
623 exitPtr->proc = proc;
624 exitPtr->clientData = clientData;
625 Tcl_MutexLock(&exitMutex);
626 exitPtr->nextPtr = firstExitPtr;
627 firstExitPtr = exitPtr;
628 Tcl_MutexUnlock(&exitMutex);
629 }
630
631 /*
632 *----------------------------------------------------------------------
633 *
634 * TclCreateLateExitHandler --
635 *
636 * Arrange for a given function to be invoked after all pre-thread
637 * cleanups.
638 *
639 * Results:
640 * None.
641 *
642 * Side effects:
643 * Proc will be invoked with clientData as argument when the application
644 * exits.
645 *
646 *----------------------------------------------------------------------
647 */
648
649 void
650 TclCreateLateExitHandler(
651 Tcl_ExitProc *proc, /* Function to invoke. */
652 ClientData clientData) /* Arbitrary value to pass to proc. */
653 {
654 ExitHandler *exitPtr = (ExitHandler*)ckalloc(sizeof(ExitHandler));
655
656 exitPtr->proc = proc;
657 exitPtr->clientData = clientData;
658 Tcl_MutexLock(&exitMutex);
659 exitPtr->nextPtr = firstLateExitPtr;
660 firstLateExitPtr = exitPtr;
661 Tcl_MutexUnlock(&exitMutex);
662 }
663
664 /*
665 *----------------------------------------------------------------------
666 *
667 * Tcl_DeleteExitHandler --
668 *
669 * This function cancels an existing exit handler matching proc and
670 * clientData, if such a handler exits.
671 *
672 * Results:
673 * None.
674 *
675 * Side effects:
676 * If there is an exit handler corresponding to proc and clientData then
677 * it is canceled; if no such handler exists then nothing happens.
678 *
679 *----------------------------------------------------------------------
680 */
681
682 void
683 Tcl_DeleteExitHandler(
684 Tcl_ExitProc *proc, /* Function that was previously registered. */
685 ClientData clientData) /* Arbitrary value to pass to proc. */
686 {
687 ExitHandler *exitPtr, *prevPtr;
688
689 Tcl_MutexLock(&exitMutex);
690 for (prevPtr = NULL, exitPtr = firstExitPtr; exitPtr != NULL;
691 prevPtr = exitPtr, exitPtr = exitPtr->nextPtr) {
692 if ((exitPtr->proc == proc)
693 && (exitPtr->clientData == clientData)) {
694 if (prevPtr == NULL) {
695 firstExitPtr = exitPtr->nextPtr;
696 } else {
697 prevPtr->nextPtr = exitPtr->nextPtr;
698 }
699 ckfree(exitPtr);
700 break;
701 }
702 }
703 Tcl_MutexUnlock(&exitMutex);
704 return;
705 }
706
707 /*
708 *----------------------------------------------------------------------
709 *
710 * TclDeleteLateExitHandler --
711 *
712 * This function cancels an existing late exit handler matching proc and
713 * clientData, if such a handler exits.
714 *
715 * Results:
716 * None.
717 *
718 * Side effects:
719 * If there is a late exit handler corresponding to proc and clientData
720 * then it is canceled; if no such handler exists then nothing happens.
721 *
722 *----------------------------------------------------------------------
723 */
724
725 void
726 TclDeleteLateExitHandler(
727 Tcl_ExitProc *proc, /* Function that was previously registered. */
728 ClientData clientData) /* Arbitrary value to pass to proc. */
729 {
730 ExitHandler *exitPtr, *prevPtr;
731
732 Tcl_MutexLock(&exitMutex);
733 for (prevPtr = NULL, exitPtr = firstLateExitPtr; exitPtr != NULL;
734 prevPtr = exitPtr, exitPtr = exitPtr->nextPtr) {
735 if ((exitPtr->proc == proc)
736 && (exitPtr->clientData == clientData)) {
737 if (prevPtr == NULL) {
738 firstLateExitPtr = exitPtr->nextPtr;
739 } else {
740 prevPtr->nextPtr = exitPtr->nextPtr;
741 }
742 ckfree(exitPtr);
743 break;
744 }
745 }
746 Tcl_MutexUnlock(&exitMutex);
747 return;
748 }
749
750 /*
751 *----------------------------------------------------------------------
752 *
753 * Tcl_CreateThreadExitHandler --
754 *
755 * Arrange for a given function to be invoked just before the current
756 * thread exits.
757 *
758 * Results:
759 * None.
760 *
761 * Side effects:
762 * Proc will be invoked with clientData as argument when the application
763 * exits.
764 *
765 *----------------------------------------------------------------------
766 */
767
768 void
769 Tcl_CreateThreadExitHandler(
770 Tcl_ExitProc *proc, /* Function to invoke. */
771 ClientData clientData) /* Arbitrary value to pass to proc. */
772 {
773 ExitHandler *exitPtr;
774 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
775
776 exitPtr = (ExitHandler*)ckalloc(sizeof(ExitHandler));
777 exitPtr->proc = proc;
778 exitPtr->clientData = clientData;
779 exitPtr->nextPtr = tsdPtr->firstExitPtr;
780 tsdPtr->firstExitPtr = exitPtr;
781 }
782
783 /*
784 *----------------------------------------------------------------------
785 *
786 * Tcl_DeleteThreadExitHandler --
787 *
788 * This function cancels an existing exit handler matching proc and
789 * clientData, if such a handler exits.
790 *
791 * Results:
792 * None.
793 *
794 * Side effects:
795 * If there is an exit handler corresponding to proc and clientData then
796 * it is canceled; if no such handler exists then nothing happens.
797 *
798 *----------------------------------------------------------------------
799 */
800
801 void
802 Tcl_DeleteThreadExitHandler(
803 Tcl_ExitProc *proc, /* Function that was previously registered. */
804 ClientData clientData) /* Arbitrary value to pass to proc. */
805 {
806 ExitHandler *exitPtr, *prevPtr;
807 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
808
809 for (prevPtr = NULL, exitPtr = tsdPtr->firstExitPtr; exitPtr != NULL;
810 prevPtr = exitPtr, exitPtr = exitPtr->nextPtr) {
811 if ((exitPtr->proc == proc)
812 && (exitPtr->clientData == clientData)) {
813 if (prevPtr == NULL) {
814 tsdPtr->firstExitPtr = exitPtr->nextPtr;
815 } else {
816 prevPtr->nextPtr = exitPtr->nextPtr;
817 }
818 ckfree(exitPtr);
819 return;
820 }
821 }
822 }
823
824 /*
825 *----------------------------------------------------------------------
826 *
827 * Tcl_SetExitProc --
828 *
829 * This function sets the application wide exit handler that will be
830 * called by Tcl_Exit in place of the C-runtime exit. If the application
831 * wide exit handler is NULL, the C-runtime exit will be used instead.
832 *
833 * Results:
834 * The previously set application wide exit handler.
835 *
836 * Side effects:
837 * Sets the application wide exit handler to the specified value.
838 *
839 *----------------------------------------------------------------------
840 */
841
842 Tcl_ExitProc *
843 Tcl_SetExitProc(
844 Tcl_ExitProc *proc) /* New exit handler for app or NULL */
845 {
846 Tcl_ExitProc *prevExitProc;
847
848 /*
849 * Swap the old exit proc for the new one, saving the old one for our
850 * return value.
851 */
852
853 Tcl_MutexLock(&exitMutex);
854 prevExitProc = appExitPtr;
855 appExitPtr = proc;
856 Tcl_MutexUnlock(&exitMutex);
857
858 return prevExitProc;
859 }
860
861
862 /*
863 *----------------------------------------------------------------------
864 *
865 * InvokeExitHandlers --
866 *
867 * Call the registered exit handlers.
868 *
869 * Results:
870 * None.
871 *
872 * Side effects:
873 * The exit handlers are invoked, and the ExitHandler struct is
874 * freed.
875 *
876 *----------------------------------------------------------------------
877 */
878 static void
879 InvokeExitHandlers(void)
880 {
881 ExitHandler *exitPtr;
882
883 Tcl_MutexLock(&exitMutex);
884 inExit = 1;
885
886 for (exitPtr = firstExitPtr; exitPtr != NULL; exitPtr = firstExitPtr) {
887 /*
888 * Be careful to remove the handler from the list before invoking its
889 * callback. This protects us against double-freeing if the callback
890 * should call Tcl_DeleteExitHandler on itself.
891 */
892
893 firstExitPtr = exitPtr->nextPtr;
894 Tcl_MutexUnlock(&exitMutex);
895 exitPtr->proc(exitPtr->clientData);
896 ckfree(exitPtr);
897 Tcl_MutexLock(&exitMutex);
898 }
899 firstExitPtr = NULL;
900 Tcl_MutexUnlock(&exitMutex);
901 }
902
903
904 /*
905 *----------------------------------------------------------------------
906 *
907 * Tcl_Exit --
908 *
909 * This function is called to terminate the application.
910 *
911 * Results:
912 * None.
913 *
914 * Side effects:
915 * All existing exit handlers are invoked, then the application ends.
916 *
917 *----------------------------------------------------------------------
918 */
919
920 TCL_NORETURN void
921 Tcl_Exit(
922 int status) /* Exit status for application; typically 0
923 * for normal return, 1 for error return. */
924 {
925 Tcl_ExitProc *currentAppExitPtr;
926
927 Tcl_MutexLock(&exitMutex);
928 currentAppExitPtr = appExitPtr;
929 Tcl_MutexUnlock(&exitMutex);
930
931 /*
932 * Warning: this function SHOULD NOT return, as there is code that depends
933 * on Tcl_Exit never returning. In fact, we will Tcl_Panic if anyone
934 * returns, so critical is this dependency.
935 *
936 * If subsystems are not (yet) initialized, proper Tcl-finalization is
937 * impossible, so fallback to system exit, see bug-[f8a33ce3db5d8cc2].
938 */
939
940 if (currentAppExitPtr) {
941
942 currentAppExitPtr(INT2PTR(status));
943
944 } else if (subsystemsInitialized) {
945
946 if (TclFullFinalizationRequested()) {
947
948 /*
949 * Thorough finalization for Valgrind et al.
950 */
951
952 Tcl_Finalize();
953
954 } else {
955
956 /*
957 * Fast and deterministic exit (default behavior)
958 */
959
960 InvokeExitHandlers();
961
962 /*
963 * Ensure the thread-specific data is initialised as it is used in
964 * Tcl_FinalizeThread()
965 */
966
967 (void) TCL_TSD_INIT(&dataKey);
968
969 /*
970 * Now finalize the calling thread only (others are not safely
971 * reachable). Among other things, this triggers a flush of the
972 * Tcl_Channels that may have data enqueued.
973 */
974
975 FinalizeThread(/* quick */ 1);
976 }
977 }
978
979 exit(status);
980 }
981
982 /*
983 *-------------------------------------------------------------------------
984 *
985 * TclInitSubsystems --
986 *
987 * Initialize various subsytems in Tcl. This should be called the first
988 * time an interp is created, or before any of the subsystems are used.
989 * This function ensures an order for the initialization of subsystems:
990 *
991 * 1. that cannot be initialized in lazy order because they are mutually
992 * dependent.
993 *
994 * 2. so that they can be finalized in a known order w/o causing the
995 * subsequent re-initialization of a subsystem in the act of shutting
996 * down another.
997 *
998 * Results:
999 * The full Tcl version.
1000 *
1001 * Side effects:
1002 * Varied, see the respective initialization routines.
1003 *
1004 *-------------------------------------------------------------------------
1005 */
1006
1007 MODULE_SCOPE const TclStubs tclStubs;
1008
1009 static const struct {
1010 const TclStubs *stubs;
1011 const char version[256];
1012 } stubInfo = {
1013 &tclStubs, {TCL_PATCH_LEVEL}
1014 };
1015
1016 const char *
1017 TclInitSubsystems(void)
1018 {
1019 if (inExit != 0) {
1020 Tcl_Panic("TclInitSubsystems called while exiting");
1021 }
1022
1023 if (subsystemsInitialized == 0) {
1024 /*
1025 * Double check inside the mutex. There are definitely calls back into
1026 * this routine from some of the functions below.
1027 */
1028
1029 TclpInitLock();
1030 if (subsystemsInitialized == 0) {
1031
1032 /*
1033 * Initialize locks used by the memory allocators before anything
1034 * interesting happens so we can use the allocators in the
1035 * implementation of self-initializing locks.
1036 */
1037
1038 TclInitThreadStorage(); /* Creates hash table for
1039 * thread local storage */
1040 #if defined(USE_TCLALLOC) && USE_TCLALLOC
1041 TclInitAlloc(); /* Process wide mutex init */
1042 #endif
1043 #ifdef TCL_MEM_DEBUG
1044 TclInitDbCkalloc(); /* Process wide mutex init */
1045 #endif
1046
1047 TclpInitPlatform(); /* Creates signal handler(s) */
1048 TclInitDoubleConversion(); /* Initializes constants for
1049 * converting to/from double. */
1050 TclInitObjSubsystem(); /* Register obj types, create
1051 * mutexes. */
1052 TclInitIOSubsystem(); /* Inits a tsd key (noop). */
1053 TclInitEncodingSubsystem(); /* Process wide encoding init. */
1054 TclInitNamespaceSubsystem();/* Register ns obj type (mutexed). */
1055 subsystemsInitialized = 1;
1056 }
1057 TclpInitUnlock();
1058 }
1059 TclInitNotifier();
1060 return stubInfo.version;
1061 }
1062
1063 /*
1064 *----------------------------------------------------------------------
1065 *
1066 * Tcl_Finalize --
1067 *
1068 * Shut down Tcl. First calls registered exit handlers, then carefully
1069 * shuts down various subsystems. Should be invoked by user before the
1070 * Tcl shared library is being unloaded in an embedded context.
1071 *
1072 * Results:
1073 * None.
1074 *
1075 * Side effects:
1076 * Varied, see the respective finalization routines.
1077 *
1078 *----------------------------------------------------------------------
1079 */
1080
1081 void
1082 Tcl_Finalize(void)
1083 {
1084 ExitHandler *exitPtr;
1085
1086 /*
1087 * Invoke exit handlers first.
1088 */
1089
1090 InvokeExitHandlers();
1091
1092 TclpInitLock();
1093 if (subsystemsInitialized == 0) {
1094 goto alreadyFinalized;
1095 }
1096 subsystemsInitialized = 0;
1097
1098 /*
1099 * Ensure the thread-specific data is initialised as it is used in
1100 * Tcl_FinalizeThread()
1101 */
1102
1103 (void) TCL_TSD_INIT(&dataKey);
1104
1105 /*
1106 * Clean up after the current thread now, after exit handlers. In
1107 * particular, the testexithandler command sets up something that writes
1108 * to standard output, which gets closed. Note that there is no
1109 * thread-local storage or IO subsystem after this call.
1110 */
1111
1112 Tcl_FinalizeThread();
1113
1114 /*
1115 * Now invoke late (process-wide) exit handlers.
1116 */
1117
1118 Tcl_MutexLock(&exitMutex);
1119 for (exitPtr = firstLateExitPtr; exitPtr != NULL;
1120 exitPtr = firstLateExitPtr) {
1121 /*
1122 * Be careful to remove the handler from the list before invoking its
1123 * callback. This protects us against double-freeing if the callback
1124 * should call Tcl_DeleteLateExitHandler on itself.
1125 */
1126
1127 firstLateExitPtr = exitPtr->nextPtr;
1128 Tcl_MutexUnlock(&exitMutex);
1129 exitPtr->proc(exitPtr->clientData);
1130 ckfree(exitPtr);
1131 Tcl_MutexLock(&exitMutex);
1132 }
1133 firstLateExitPtr = NULL;
1134 Tcl_MutexUnlock(&exitMutex);
1135
1136 /*
1137 * Now finalize the Tcl execution environment. Note that this must be done
1138 * after the exit handlers, because there are order dependencies.
1139 */
1140
1141 TclFinalizeEvaluation();
1142 TclFinalizeExecution();
1143 TclFinalizeEnvironment();
1144
1145 /*
1146 * Finalizing the filesystem must come after anything which might
1147 * conceivably interact with the 'Tcl_FS' API.
1148 */
1149
1150 TclFinalizeFilesystem();
1151
1152 /*
1153 * Undo all Tcl_ObjType registrations, and reset the global list of free
1154 * Tcl_Obj's. After this returns, no more Tcl_Obj's should be allocated or
1155 * freed.
1156 *
1157 * Note in particular that TclFinalizeObjects() must follow
1158 * TclFinalizeFilesystem() because TclFinalizeFilesystem free's the
1159 * Tcl_Obj that holds the path of the current working directory.
1160 */
1161
1162 TclFinalizeObjects();
1163
1164 /*
1165 * We must be sure the encoding finalization doesn't need to examine the
1166 * filesystem in any way. Since it only needs to clean up internal data
1167 * structures, this is fine.
1168 */
1169
1170 TclFinalizeEncodingSubsystem();
1171
1172 /*
1173 * Repeat finalization of the thread local storage once more. Although
1174 * this step is already done by the Tcl_FinalizeThread call above, series
1175 * of events happening afterwards may re-initialize TSD slots. Those need
1176 * to be finalized again, otherwise we're leaking memory chunks. Very
1177 * important to note is that things happening afterwards should not
1178 * reference anything which may re-initialize TSD's. This includes freeing
1179 * Tcl_Objs's, among other things.
1180 *
1181 * This fixes the Tcl Bug #990552.
1182 */
1183
1184 TclFinalizeThreadData(/* quick */ 0);
1185
1186 /*
1187 * Now we can free constants for conversions to/from double.
1188 */
1189
1190 TclFinalizeDoubleConversion();
1191
1192 /*
1193 * There have been several bugs in the past that cause exit handlers to be
1194 * established during Tcl_Finalize processing. Such exit handlers leave
1195 * malloc'ed memory, and Tcl_FinalizeMemorySubsystem or
1196 * Tcl_FinalizeThreadAlloc will result in a corrupted heap. The result can
1197 * be a mysterious crash on process exit. Check here that nobody's done
1198 * this.
1199 */
1200
1201 if (firstExitPtr != NULL) {
1202 Tcl_Panic("exit handlers were created during Tcl_Finalize");
1203 }
1204
1205 TclFinalizePreserve();
1206
1207 /*
1208 * Free synchronization objects. There really should only be one thread
1209 * alive at this moment.
1210 */
1211
1212 TclFinalizeSynchronization();
1213
1214 /*
1215 * Close down the thread-specific object allocator.
1216 */
1217
1218 #if defined(TCL_THREADS) && defined(USE_THREAD_ALLOC)
1219 TclFinalizeThreadAlloc();
1220 #endif
1221
1222 /*
1223 * We defer unloading of packages until very late to avoid memory access
1224 * issues. Both exit callbacks and synchronization variables may be stored
1225 * in packages.
1226 *
1227 * Note that TclFinalizeLoad unloads packages in the reverse of the order
1228 * they were loaded in (i.e. last to be loaded is the first to be
1229 * unloaded). This can be important for correct unloading when
1230 * dependencies exist.
1231 *
1232 * Once load has been finalized, we will have deleted any temporary copies
1233 * of shared libraries and can therefore reset the filesystem to its
1234 * original state.
1235 */
1236
1237 TclFinalizeLoad();
1238 TclResetFilesystem();
1239
1240 /*
1241 * At this point, there should no longer be any ckalloc'ed memory.
1242 */
1243
1244 TclFinalizeMemorySubsystem();
1245
1246 alreadyFinalized:
1247 TclFinalizeLock();
1248 }
1249
1250 /*
1251 *----------------------------------------------------------------------
1252 *
1253 * Tcl_FinalizeThread --
1254 *
1255 * Runs the exit handlers to allow Tcl to clean up its state about a
1256 * particular thread.
1257 *
1258 * Results:
1259 * None.
1260 *
1261 * Side effects:
1262 * Varied, see the respective finalization routines.
1263 *
1264 *----------------------------------------------------------------------
1265 */
1266
1267 void
1268 Tcl_FinalizeThread(void)
1269 {
1270 FinalizeThread(/* quick */ 0);
1271 }
1272
1273 void
1274 FinalizeThread(
1275 int quick)
1276 {
1277 ExitHandler *exitPtr;
1278 ThreadSpecificData *tsdPtr;
1279
1280 /*
1281 * We use TclThreadDataKeyGet here, rather than Tcl_GetThreadData, because
1282 * we don't want to initialize the data block if it hasn't been
1283 * initialized already.
1284 */
1285
1286 tsdPtr = (ThreadSpecificData*)TclThreadDataKeyGet(&dataKey);
1287 if (tsdPtr != NULL) {
1288 tsdPtr->inExit = 1;
1289
1290 for (exitPtr = tsdPtr->firstExitPtr; exitPtr != NULL;
1291 exitPtr = tsdPtr->firstExitPtr) {
1292 /*
1293 * Be careful to remove the handler from the list before invoking
1294 * its callback. This protects us against double-freeing if the
1295 * callback should call Tcl_DeleteThreadExitHandler on itself.
1296 */
1297
1298 tsdPtr->firstExitPtr = exitPtr->nextPtr;
1299 exitPtr->proc(exitPtr->clientData);
1300 ckfree(exitPtr);
1301 }
1302 TclFinalizeIOSubsystem();
1303 TclFinalizeNotifier();
1304 TclFinalizeAsync();
1305 TclFinalizeThreadObjects();
1306 }
1307
1308 /*
1309 * Blow away all thread local storage blocks.
1310 *
1311 * Note that Tcl API allows creation of threads which do not use any Tcl
1312 * interp or other Tcl subsytems. Those threads might, however, use thread
1313 * local storage, so we must unconditionally finalize it.
1314 *
1315 * Fix [Bug #571002]
1316 */
1317 TclFinalizeThreadData(quick);
1318 }
1319
1320 /*
1321 *----------------------------------------------------------------------
1322 *
1323 * TclInExit --
1324 *
1325 * Determines if we are in the middle of exit-time cleanup.
1326 *
1327 * Results:
1328 * If we are in the middle of exiting, 1, otherwise 0.
1329 *
1330 * Side effects:
1331 * None.
1332 *
1333 *----------------------------------------------------------------------
1334 */
1335
1336 int
1337 TclInExit(void)
1338 {
1339 return inExit;
1340 }
1341
1342 /*
1343 *----------------------------------------------------------------------
1344 *
1345 * TclInThreadExit --
1346 *
1347 * Determines if we are in the middle of thread exit-time cleanup.
1348 *
1349 * Results:
1350 * If we are in the middle of exiting this thread, 1, otherwise 0.
1351 *
1352 * Side effects:
1353 * None.
1354 *
1355 *----------------------------------------------------------------------
1356 */
1357
1358 int
1359 TclInThreadExit(void)
1360 {
1361 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
1362
1363 if (tsdPtr == NULL) {
1364 return 0;
1365 }
1366 return tsdPtr->inExit;
1367 }
1368
1369 /*
1370 *----------------------------------------------------------------------
1371 *
1372 * Tcl_VwaitObjCmd --
1373 *
1374 * This function is invoked to process the "vwait" Tcl command. See the
1375 * user documentation for details on what it does.
1376 *
1377 * Results:
1378 * A standard Tcl result.
1379 *
1380 * Side effects:
1381 * See the user documentation.
1382 *
1383 *----------------------------------------------------------------------
1384 */
1385
1386 int
1387 Tcl_VwaitObjCmd(
1388 ClientData clientData, /* Not used. */
1389 Tcl_Interp *interp, /* Current interpreter. */
1390 int objc, /* Number of arguments. */
1391 Tcl_Obj *const objv[]) /* Argument objects. */
1392 {
1393 int done, foundEvent;
1394 const char *nameString;
1395
1396 if (objc != 2) {
1397 Tcl_WrongNumArgs(interp, 1, objv, "name");
1398 return TCL_ERROR;
1399 }
1400 nameString = Tcl_GetString(objv[1]);
1401 if (Tcl_TraceVar2(interp, nameString, NULL,
1402 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1403 VwaitVarProc, &done) != TCL_OK) {
1404 return TCL_ERROR;
1405 };
1406 done = 0;
1407 foundEvent = 1;
1408 while (!done && foundEvent) {
1409 foundEvent = Tcl_DoOneEvent(TCL_ALL_EVENTS);
1410 if (Tcl_Canceled(interp, TCL_LEAVE_ERR_MSG) == TCL_ERROR) {
1411 break;
1412 }
1413 if (Tcl_LimitExceeded(interp)) {
1414 Tcl_ResetResult(interp);
1415 Tcl_SetObjResult(interp, Tcl_NewStringObj("limit exceeded", -1));
1416 break;
1417 }
1418 }
1419 Tcl_UntraceVar2(interp, nameString, NULL,
1420 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1421 VwaitVarProc, &done);
1422
1423 if (!foundEvent) {
1424 Tcl_ResetResult(interp);
1425 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1426 "can't wait for variable \"%s\": would wait forever",
1427 nameString));
1428 Tcl_SetErrorCode(interp, "TCL", "EVENT", "NO_SOURCES", (char *)NULL);
1429 return TCL_ERROR;
1430 }
1431 if (!done) {
1432 /*
1433 * The interpreter's result was already set to the right error message
1434 * prior to exiting the loop above.
1435 */
1436
1437 return TCL_ERROR;
1438 }
1439
1440 /*
1441 * Clear out the interpreter's result, since it may have been set by event
1442 * handlers.
1443 */
1444
1445 Tcl_ResetResult(interp);
1446 return TCL_OK;
1447 }
1448
1449 static char *
1450 VwaitVarProc(
1451 ClientData clientData, /* Pointer to integer to set to 1. */
1452 Tcl_Interp *interp, /* Interpreter containing variable. */
1453 const char *name1, /* Name of variable. */
1454 const char *name2, /* Second part of variable name. */
1455 int flags) /* Information about what happened. */
1456 {
1457 int *donePtr = (int *)clientData;
1458
1459 *donePtr = 1;
1460 Tcl_UntraceVar2(interp, name1, name2, TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1461 VwaitVarProc, clientData);
1462 return NULL;
1463 }
1464
1465 /*
1466 *----------------------------------------------------------------------
1467 *
1468 * Tcl_UpdateObjCmd --
1469 *
1470 * This function is invoked to process the "update" Tcl command. See the
1471 * user documentation for details on what it does.
1472 *
1473 * Results:
1474 * A standard Tcl result.
1475 *
1476 * Side effects:
1477 * See the user documentation.
1478 *
1479 *----------------------------------------------------------------------
1480 */
1481
1482 int
1483 Tcl_UpdateObjCmd(
1484 ClientData clientData, /* Not used. */
1485 Tcl_Interp *interp, /* Current interpreter. */
1486 int objc, /* Number of arguments. */
1487 Tcl_Obj *const objv[]) /* Argument objects. */
1488 {
1489 int optionIndex;
1490 int flags = 0; /* Initialized to avoid compiler warning. */
1491 static const char *const updateOptions[] = {"idletasks", NULL};
1492 enum updateOptionsEnum {OPT_IDLETASKS};
1493
1494 if (objc == 1) {
1495 flags = TCL_ALL_EVENTS|TCL_DONT_WAIT;
1496 } else if (objc == 2) {
1497 if (Tcl_GetIndexFromObj(interp, objv[1], updateOptions,
1498 "option", 0, &optionIndex) != TCL_OK) {
1499 return TCL_ERROR;
1500 }
1501 switch ((enum updateOptionsEnum) optionIndex) {
1502 case OPT_IDLETASKS:
1503 flags = TCL_WINDOW_EVENTS|TCL_IDLE_EVENTS|TCL_DONT_WAIT;
1504 break;
1505 default:
1506 Tcl_Panic("Tcl_UpdateObjCmd: bad option index to UpdateOptions");
1507 }
1508 } else {
1509 Tcl_WrongNumArgs(interp, 1, objv, "?idletasks?");
1510 return TCL_ERROR;
1511 }
1512
1513 while (Tcl_DoOneEvent(flags) != 0) {
1514 if (Tcl_Canceled(interp, TCL_LEAVE_ERR_MSG) == TCL_ERROR) {
1515 return TCL_ERROR;
1516 }
1517 if (Tcl_LimitExceeded(interp)) {
1518 Tcl_ResetResult(interp);
1519 Tcl_SetObjResult(interp, Tcl_NewStringObj("limit exceeded", -1));
1520 return TCL_ERROR;
1521 }
1522 }
1523
1524 /*
1525 * Must clear the interpreter's result because event handlers could have
1526 * executed commands.
1527 */
1528
1529 Tcl_ResetResult(interp);
1530 return TCL_OK;
1531 }
1532
1533 #ifdef TCL_THREADS
1534 /*
1535 *----------------------------------------------------------------------
1536 *
1537 * NewThreadProc --
1538 *
1539 * Bootstrap function of a new Tcl thread.
1540 *
1541 * Results:
1542 * None.
1543 *
1544 * Side Effects:
1545 * Initializes Tcl notifier for the current thread.
1546 *
1547 *----------------------------------------------------------------------
1548 */
1549
1550 static Tcl_ThreadCreateType
1551 NewThreadProc(
1552 ClientData clientData)
1553 {
1554 ThreadClientData *cdPtr = (ThreadClientData *)clientData;
1555 ClientData threadClientData;
1556 Tcl_ThreadCreateProc *threadProc;
1557
1558 threadProc = cdPtr->proc;
1559 threadClientData = cdPtr->clientData;
1560 ckfree(clientData); /* Allocated in Tcl_CreateThread() */
1561
1562 threadProc(threadClientData);
1563
1564 TCL_THREAD_CREATE_RETURN;
1565 }
1566 #endif
1567
1568 /*
1569 *----------------------------------------------------------------------
1570 *
1571 * Tcl_CreateThread --
1572 *
1573 * This function creates a new thread. This actually belongs to the
1574 * tclThread.c file but since we use some private data structures local
1575 * to this file, it is placed here.
1576 *
1577 * Results:
1578 * TCL_OK if the thread could be created. The thread ID is returned in a
1579 * parameter.
1580 *
1581 * Side effects:
1582 * A new thread is created.
1583 *
1584 *----------------------------------------------------------------------
1585 */
1586
1587 int
1588 Tcl_CreateThread(
1589 Tcl_ThreadId *idPtr, /* Return, the ID of the thread */
1590 Tcl_ThreadCreateProc *proc, /* Main() function of the thread */
1591 ClientData clientData, /* The one argument to Main() */
1592 int stackSize, /* Size of stack for the new thread */
1593 int flags) /* Flags controlling behaviour of the new
1594 * thread. */
1595 {
1596 #ifdef TCL_THREADS
1597 ThreadClientData *cdPtr = (ThreadClientData *)ckalloc(sizeof(ThreadClientData));
1598 int result;
1599
1600 cdPtr->proc = proc;
1601 cdPtr->clientData = clientData;
1602 result = TclpThreadCreate(idPtr, NewThreadProc, cdPtr, stackSize, flags);
1603 if (result != TCL_OK) {
1604 ckfree(cdPtr);
1605 }
1606 return result;
1607 #else
1608 (void)idPtr;
1609 (void)proc;
1610 (void)clientData;
1611 (void)stackSize;
1612 (void)flags;
1613
1614 return TCL_ERROR;
1615 #endif /* TCL_THREADS */
1616 }
1617
1618 /*
1619 * Local Variables:
1620 * mode: c
1621 * c-basic-offset: 4
1622 * fill-column: 78
1623 * End:
1624 */