Tcl命令vwait的代码实现

Tcl命令update是进入事件循环直到所有事件处理完毕。命令vwait也是进入事件循环,只不过是直到某个变量被set

参见: Tcl命令update的代码实现

vwait命令的代码实现

代码文件: generic/tclEvent.c

    nameString = Tcl_GetString(objv[1]);
    if (Tcl_TraceVar2(interp, nameString, NULL,
            TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
            VwaitVarProc, &done) != TCL_OK) {
        return TCL_ERROR;
    };
    done = 0;
    foundEvent = 1;
    while (!done && foundEvent) {
        foundEvent = Tcl_DoOneEvent(TCL_ALL_EVENTS);
        if (Tcl_Canceled(interp, TCL_LEAVE_ERR_MSG) == TCL_ERROR) {
            break;
        }
        if (Tcl_LimitExceeded(interp)) {
            Tcl_ResetResult(interp);
            Tcl_SetObjResult(interp, Tcl_NewStringObj("limit exceeded", -1));
            break;
        }
    }
    Tcl_UntraceVar2(interp, nameString, NULL,
            TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
            VwaitVarProc, &done);

可以看出其实现方法主要是注册了一个新的变量跟踪事件,相当于Tcl命令的

trace add variable $varname {write unset} ...

除此之外,其逻辑与update命令类似。