| 1 | \ ========== Copyright Header Begin ========================================== |
| 2 | \ |
| 3 | \ Hypervisor Software File: timer.fth |
| 4 | \ |
| 5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 6 | \ |
| 7 | \ - Do no alter or remove copyright notices |
| 8 | \ |
| 9 | \ - Redistribution and use of this software in source and binary forms, with |
| 10 | \ or without modification, are permitted provided that the following |
| 11 | \ conditions are met: |
| 12 | \ |
| 13 | \ - Redistribution of source code must retain the above copyright notice, |
| 14 | \ this list of conditions and the following disclaimer. |
| 15 | \ |
| 16 | \ - Redistribution in binary form must reproduce the above copyright notice, |
| 17 | \ this list of conditions and the following disclaimer in the |
| 18 | \ documentation and/or other materials provided with the distribution. |
| 19 | \ |
| 20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors |
| 21 | \ may be used to endorse or promote products derived from this software |
| 22 | \ without specific prior written permission. |
| 23 | \ |
| 24 | \ This software is provided "AS IS," without a warranty of any kind. |
| 25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN |
| 28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR |
| 29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR |
| 30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN |
| 31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR |
| 32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE |
| 33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, |
| 34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF |
| 35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 36 | \ |
| 37 | \ You acknowledge that this software is not designed, licensed or |
| 38 | \ intended for use in the design, construction, operation or maintenance of |
| 39 | \ any nuclear facility. |
| 40 | \ |
| 41 | \ ========== Copyright Header End ============================================ |
| 42 | id: @(#)timer.fth 1.1 04/09/07 |
| 43 | purpose: Timer utility functions |
| 44 | copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved |
| 45 | copyright: Use is subject to license terms. |
| 46 | |
| 47 | headerless |
| 48 | |
| 49 | struct |
| 50 | /l field >tm-start \ Time this entry was made |
| 51 | /l field >tm-expire \ Time this timer expires |
| 52 | constant /timer |
| 53 | |
| 54 | \ Set timer event. |
| 55 | : set-timer ( timer interval -- ) |
| 56 | get-msecs tuck + rot tuck >tm-expire l! >tm-start l! |
| 57 | ; |
| 58 | |
| 59 | \ Clear timer event. Return time elapsed since event entry was made. |
| 60 | : clear-timer ( timer -- timespent ) |
| 61 | get-msecs over >tm-start l@ - swap ( timespent timer ) |
| 62 | 0 over >tm-expire l! ( timespent timer ) |
| 63 | 0 swap >tm-start l! ( timespent ) |
| 64 | ; |
| 65 | |
| 66 | \ Get time elapsed since the timer event entry was made. |
| 67 | : get-timer ( timer -- timespent ) |
| 68 | >tm-start l@ get-msecs swap - |
| 69 | ; |
| 70 | |
| 71 | \ Check if timer has expired. |
| 72 | : timer-expired? ( timer -- flag ) |
| 73 | >tm-expire l@ ?dup if get-msecs < else false then |
| 74 | ; |
| 75 | |
| 76 | \ Check if timer is in use. |
| 77 | : timer-running? ( timer -- flag ) >tm-expire l@ 0<> ; |
| 78 | |
| 79 | headers |