Index: generic/threadCmd.c ================================================================== --- generic/threadCmd.c +++ generic/threadCmd.c @@ -2832,22 +2832,10 @@ * Process events until signaled to stop. */ while (canrun) { - /* - * About to service another event. - * Wake-up eventual sleepers. - */ - - if (tsdPtr->maxEventsCount) { - Tcl_MutexLock(&threadMutex); - tsdPtr->eventsPending--; - Tcl_ConditionNotify(&tsdPtr->doOneEvent); - Tcl_MutexUnlock(&threadMutex); - } - /* * Attempt to process one event, blocking forever until an * event is actually received. The event processed may cause * a script in progress to be canceled or exceed its limit; * therefore, check for these conditions if we are able to @@ -3210,10 +3198,19 @@ tsdPtr->flags |= THREAD_FLAGS_STOPPED; } } Tcl_MutexUnlock(&threadMutex); } + + if (tsdPtr->maxEventsCount) { + Tcl_MutexLock(&threadMutex); + tsdPtr->eventsPending--; + if (tsdPtr->eventsPending <= tsdPtr->maxEventsCount) { + Tcl_ConditionNotify(&tsdPtr->doOneEvent); + } + Tcl_MutexUnlock(&threadMutex); + } return 1; } /* @@ -3404,10 +3401,17 @@ Tcl_AppendResult(interp, "expected integer but got \"", value, "\"", NULL); Tcl_MutexUnlock(&threadMutex); return TCL_ERROR; } + if (tsdPtr->maxEventsCount == 0) { + tsdPtr->eventsPending = 0; + } + if (tsdPtr->maxEventsCount == 0 || + tsdPtr->eventsPending <= tsdPtr->maxEventsCount) { + Tcl_ConditionNotify(&tsdPtr->doOneEvent); + } } else if (len > 2 && option[1] == 'u' && !strncmp(option,"-unwindonerror", len)) { int flag = 0; if (Tcl_GetBoolean(interp, value, &flag) != TCL_OK) { Tcl_MutexUnlock(&threadMutex);