Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: d4dc4c3fd4b0bea16ad773f4c60b0999457a4b3d
Ticket: 2953b1b26b58c887435b74923ccef1712ad4b614
Add "after at" to schedule for a specific time
User & Date: SpaceManiac on 2016-11-18 12:03:18
Changes

  1. comment changed to:
    This patch adds an "after at" subcommand which acts like "after" but accepts an absolute number of milliseconds (same terms as "clock milliseconds") to wait until rather than a relative number of milliseconds to wait for. Both command and commandless (synchronous pausing) forms are handled.
    
    Calculation of end time is deduplicated between AfterDelay and Tcl_AfterObjCommand and switches on whether "after at" was called. This means Tcl_GetTime is called marginally earlier in the code; perhaps this is undesirable if applications are extremely sensitive to microsecond changes in timing.
    
    I am a first-time Tcl contributor, but a long-time casual user; my apologies if I have inadvertently done a runaround on how patches are usually contributed, or if this patch is in the wrong format. Just let me know.
    
    Patch follows.
    ------
    
    diff --git a/generic/tclTimer.c b/generic/tclTimer.c
    index 6d3938b..e196cf5 100644
    --- a/generic/tclTimer.c
    +++ b/generic/tclTimer.c
    @@ -152,7 +152,7 @@ static Tcl_ThreadDataKey dataKey;
     
     static void		AfterCleanupProc(ClientData clientData,
     			    Tcl_Interp *interp);
    -static int		AfterDelay(Tcl_Interp *interp, Tcl_WideInt ms);
    +static int		AfterDelay(Tcl_Interp *interp, Tcl_Time endTime);
     static void		AfterProc(ClientData clientData);
     static void		FreeAfterPtr(AfterInfo *afterPtr);
     static AfterInfo *	GetAfterEvent(AfterAssocData *assocPtr,
    @@ -791,9 +791,9 @@ Tcl_AfterObjCmd(
         int length;
         int index;
         static const char *const afterSubCmds[] = {
    -	"cancel", "idle", "info", NULL
    +	"cancel", "idle", "info", "at", NULL
         };
    -    enum afterSubCmds {AFTER_CANCEL, AFTER_IDLE, AFTER_INFO};
    +    enum afterSubCmds {AFTER_CANCEL, AFTER_IDLE, AFTER_INFO, AFTER_AT};
         ThreadSpecificData *tsdPtr = InitTimer();
     
         if (objc < 2) {
    @@ -831,7 +831,26 @@ Tcl_AfterObjCmd(
     
     	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
                         "bad argument \"%s\": must be"
    -                    " cancel, idle, info, or an integer", arg));
    +                    " cancel, idle, info, at, or an integer", arg));
    +            Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INDEX", "argument",
    +                    arg, NULL);
    +	    return TCL_ERROR;
    +	}
    +    }
    +
    +    /*
    +     * If the "at" subcommand was used, parse milliseconds or fail, and then
    +     * advance objv to allow sharing of code with normal case.
    +     */
    +
    +    if (index == AFTER_AT) {
    +	objv++;
    +	objc--;
    +	if (Tcl_GetWideIntFromObj(NULL, objv[1], &ms) != TCL_OK) {
    +            const char *arg = Tcl_GetString(objv[1]);
    +
    +	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
    +                    "bad argument \"%s\": must be an integer", arg));
                 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INDEX", "argument",
                         arg, NULL);
     	    return TCL_ERROR;
    @@ -844,12 +863,27 @@ Tcl_AfterObjCmd(
          */
     
         switch (index) {
    +    case AFTER_AT:
         case -1: {
     	if (ms < 0) {
     	    ms = 0;
     	}
    +
    +	if (index == AFTER_AT) {
    +	    wakeup.sec = (long)(ms / 1000);
    +	    wakeup.usec = ((long)(ms % 1000)) * 1000;
    +	} else {
    +	    Tcl_GetTime(&wakeup);
    +	    wakeup.sec += (long)(ms / 1000);
    +	    wakeup.usec += ((long)(ms % 1000)) * 1000;
    +	    if (wakeup.usec > 1000000) {
    +		wakeup.sec++;
    +		wakeup.usec -= 1000000;
    +	    }
    +	}
    +
     	if (objc == 2) {
    -	    return AfterDelay(interp, ms);
    +	    return AfterDelay(interp, wakeup);
     	}
     	afterPtr = ckalloc(sizeof(AfterInfo));
     	afterPtr->assocPtr = assocPtr;
    @@ -872,13 +906,6 @@ Tcl_AfterObjCmd(
     
     	afterPtr->id = tsdPtr->afterId;
     	tsdPtr->afterId += 1;
    -	Tcl_GetTime(&wakeup);
    -	wakeup.sec += (long)(ms / 1000);
    -	wakeup.usec += ((long)(ms % 1000)) * 1000;
    -	if (wakeup.usec > 1000000) {
    -	    wakeup.sec++;
    -	    wakeup.usec -= 1000000;
    -	}
     	afterPtr->token = TclCreateAbsoluteTimerHandler(&wakeup,
     		AfterProc, afterPtr);
     	afterPtr->nextPtr = assocPtr->firstAfterPtr;
    @@ -1010,21 +1037,14 @@ Tcl_AfterObjCmd(
     static int
     AfterDelay(
         Tcl_Interp *interp,
    -    Tcl_WideInt ms)
    +    Tcl_Time endTime)
     {
         Interp *iPtr = (Interp *) interp;
     
    -    Tcl_Time endTime, now;
    +    Tcl_Time now;
         Tcl_WideInt diff;
     
         Tcl_GetTime(&now);
    -    endTime = now;
    -    endTime.sec += (long)(ms/1000);
    -    endTime.usec += ((int)(ms%1000))*1000;
    -    if (endTime.usec >= 1000000) {
    -	endTime.sec++;
    -	endTime.usec -= 1000000;
    -    }
     
         do {
     	if (Tcl_AsyncReady()) {
    -- 
    2.9.1
    
  2. login: "SpaceManiac"
  3. mimetype: "text/plain"