1/*
2 * tclUnixEvent.c --
3 *
4 *	This file implements Unix specific event related routines.
5 *
6 * Copyright (c) 1997 by Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution of
9 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tclUnixEvent.c,v 1.9.10.1 2009/04/10 18:02:42 das Exp $
12 */
13
14#include "tclInt.h"
15#ifndef HAVE_COREFOUNDATION	/* Darwin/Mac OS X CoreFoundation notifier is
16				 * in tclMacOSXNotify.c */
17
18/*
19 *----------------------------------------------------------------------
20 *
21 * Tcl_Sleep --
22 *
23 *	Delay execution for the specified number of milliseconds.
24 *
25 * Results:
26 *	None.
27 *
28 * Side effects:
29 *	Time passes.
30 *
31 *----------------------------------------------------------------------
32 */
33
34void
35Tcl_Sleep(
36    int ms)			/* Number of milliseconds to sleep. */
37{
38    struct timeval delay;
39    Tcl_Time before, after, vdelay;
40
41    /*
42     * The only trick here is that select appears to return early under some
43     * conditions, so we have to check to make sure that the right amount of
44     * time really has elapsed.  If it's too early, go back to sleep again.
45     */
46
47    Tcl_GetTime(&before);
48    after = before;
49    after.sec += ms/1000;
50    after.usec += (ms%1000)*1000;
51    if (after.usec > 1000000) {
52	after.usec -= 1000000;
53	after.sec += 1;
54    }
55    while (1) {
56	/*
57	 * TIP #233: Scale from virtual time to real-time for select.
58	 */
59
60	vdelay.sec  = after.sec  - before.sec;
61	vdelay.usec = after.usec - before.usec;
62
63	if (vdelay.usec < 0) {
64	    vdelay.usec += 1000000;
65	    vdelay.sec  -= 1;
66	}
67
68	if ((vdelay.sec != 0) || (vdelay.usec != 0)) {
69	    (*tclScaleTimeProcPtr) (&vdelay, tclTimeClientData);
70	}
71
72	delay.tv_sec  = vdelay.sec;
73	delay.tv_usec = vdelay.usec;
74
75	/*
76	 * Special note: must convert delay.tv_sec to int before comparing to
77	 * zero, since delay.tv_usec is unsigned on some platforms.
78	 */
79
80	if ((((int) delay.tv_sec) < 0)
81		|| ((delay.tv_usec == 0) && (delay.tv_sec == 0))) {
82	    break;
83	}
84	(void) select(0, (SELECT_MASK *) 0, (SELECT_MASK *) 0,
85		(SELECT_MASK *) 0, &delay);
86	Tcl_GetTime(&before);
87    }
88}
89
90#endif /* HAVE_COREFOUNDATION */
91/*
92 * Local Variables:
93 * mode: c
94 * c-basic-offset: 4
95 * fill-column: 78
96 * End:
97 */
98