{ ==================================================================== counter.f Timer functions Copyright (C) 2001 FORTH, Inc.
Rick VanNorman rvn@forth.com ==================================================================== } { -------------------------------------------------------------------- COUNTER returns the current 32-bit millisecond counter. Wraps around every 49.7 days. TIMER displays elapsed milliseconds since the give COUNTER. EXPIRED returns true if COUNTER has exceeded n . There is a 1 day window in which this routine must be called to allow for slow monitoring routines. uCOUNTER and uTIMER are microsecond variants of COUNTER TIMER. They are not very accurate because there is no native microsecond time reference in windows. DCOUNTER and DTIMER are performance-counter variants of COUNTER TIMER. They produce much more accurate results, but they do not have human interpretable units. -------------------------------------------------------------------- } ?( Timer access) : COUNTER ( -- ms ) GetTickCount ; : TIMER ( ms -- ) COUNTER SWAP - U. ; : EXPIRED ( ms -- t) COUNTER - -86400000 1 WITHIN ; : DCOUNTER ( -- d ) 0 0 SP@ QueryPerformanceCounter DROP SWAP ; : uCOUNTER ( -- d ) DCOUNTER 1000000 0 0 SP@ QueryPerformanceFrequency DROP NIP M*/ ; : (DTIMER) ( d -- d ) DCOUNTER 2SWAP D- ; : (uTIMER) ( d -- d ) uCOUNTER 2SWAP D- ; : DTIMER ( d -- ) (DTIMER) D. ; : uTIMER ( d -- ) (uTIMER) D. ; { -------------------------------------------------------------------- MS provides a delay of the given milliseconds. -------------------------------------------------------------------- } : MS ( n -- ) ?DUP IF COUNTER + BEGIN PAUSE 1 Sleep DROP DUP EXPIRED UNTIL DROP THEN ;