tclUnixEvent.c

Go to the documentation of this file.
00001 /*
00002  * tclUnixEvent.c --
00003  *
00004  *      This file implements Unix specific event related routines.
00005  *
00006  * Copyright (c) 1997 by Sun Microsystems, Inc.
00007  *
00008  * See the file "license.terms" for information on usage and redistribution of
00009  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  *
00011  * RCS: @(#) $Id: tclUnixEvent.c,v 1.9 2005/11/02 23:26:50 dkf Exp $
00012  */
00013 
00014 #include "tclInt.h"
00015 
00016 /*
00017  *----------------------------------------------------------------------
00018  *
00019  * Tcl_Sleep --
00020  *
00021  *      Delay execution for the specified number of milliseconds.
00022  *
00023  * Results:
00024  *      None.
00025  *
00026  * Side effects:
00027  *      Time passes.
00028  *
00029  *----------------------------------------------------------------------
00030  */
00031 
00032 void
00033 Tcl_Sleep(
00034     int ms)                     /* Number of milliseconds to sleep. */
00035 {
00036     struct timeval delay;
00037     Tcl_Time before, after, vdelay;
00038 
00039     /*
00040      * The only trick here is that select appears to return early under some
00041      * conditions, so we have to check to make sure that the right amount of
00042      * time really has elapsed.  If it's too early, go back to sleep again.
00043      */
00044 
00045     Tcl_GetTime(&before);
00046     after = before;
00047     after.sec += ms/1000;
00048     after.usec += (ms%1000)*1000;
00049     if (after.usec > 1000000) {
00050         after.usec -= 1000000;
00051         after.sec += 1;
00052     }
00053     while (1) {
00054         /*
00055          * TIP #233: Scale from virtual time to real-time for select.
00056          */
00057 
00058         vdelay.sec  = after.sec  - before.sec;
00059         vdelay.usec = after.usec - before.usec;
00060 
00061         if (vdelay.usec < 0) {
00062             vdelay.usec += 1000000;
00063             vdelay.sec  -= 1;
00064         }
00065 
00066         if ((vdelay.sec != 0) || (vdelay.usec != 0)) {
00067             (*tclScaleTimeProcPtr) (&vdelay, tclTimeClientData);
00068         }
00069 
00070         delay.tv_sec  = vdelay.sec;
00071         delay.tv_usec = vdelay.usec;
00072 
00073         /*
00074          * Special note: must convert delay.tv_sec to int before comparing to
00075          * zero, since delay.tv_usec is unsigned on some platforms.
00076          */
00077 
00078         if ((((int) delay.tv_sec) < 0)
00079                 || ((delay.tv_usec == 0) && (delay.tv_sec == 0))) {
00080             break;
00081         }
00082         (void) select(0, (SELECT_MASK *) 0, (SELECT_MASK *) 0,
00083                 (SELECT_MASK *) 0, &delay);
00084         Tcl_GetTime(&before);
00085     }
00086 }
00087 
00088 /*
00089  * Local Variables:
00090  * mode: c
00091  * c-basic-offset: 4
00092  * fill-column: 78
00093  * End:
00094  */



Generated on Wed Mar 12 12:18:26 2008 by  doxygen 1.5.1