| 1 |
;;; |
| 2 |
;;; SRFI-120 - Timer |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2021-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; SRFI-120's Timer is just a <scheduler> |
| 35 |
|
| 36 |
(define-module srfi.120 |
| 37 |
(use control.scheduler) |
| 38 |
(use srfi.19) |
| 39 |
(export make-timer timer? timer-cancel! |
| 40 |
timer-schedule! timer-reschedule! |
| 41 |
timer-task-remove! timer-task-exists? |
| 42 |
make-timer-delta timer-delta?)) |
| 43 |
(select-module srfi.120) |
| 44 |
|
| 45 |
(define (make-timer :optional (error-handler #f)) |
| 46 |
(make <scheduler> :error-handler error-handler)) |
| 47 |
|
| 48 |
(define (%when when) |
| 49 |
(cond [(integer? when) (/ when 1e3)] ;ms |
| 50 |
[(is-a? when <time>) when] |
| 51 |
[else (error "integer (ms) or <time> required, but got:" when)])) |
| 52 |
|
| 53 |
(define (timer? obj) (is-a? obj <scheduler>)) |
| 54 |
|
| 55 |
(define (timer-cancel! timer) |
| 56 |
(scheduler-terminate! timer)) |
| 57 |
|
| 58 |
(define (timer-schedule! timer thunk when :optional (period 0)) |
| 59 |
(scheduler-schedule! timer thunk (%when when) period)) |
| 60 |
|
| 61 |
(define (timer-reschedule! timer task-id when :optional (period 0)) |
| 62 |
(scheduler-reschedule! timer task-id (%when when) period)) |
| 63 |
|
| 64 |
(define (timer-task-remove! timer task-id) |
| 65 |
(scheduler-remove! timer task-id)) |
| 66 |
|
| 67 |
(define (timer-task-exists? timer task-id) |
| 68 |
(scheduler-exists? timer task-id)) |
| 69 |
|
| 70 |
(define (make-timer-delta n unit) |
| 71 |
(define (make-duration nanosecs secs) |
| 72 |
(make-time time-duration |
| 73 |
(remainder nanosecs #e1e9) |
| 74 |
(+ secs (quotient nanosecs #e1e9)))) |
| 75 |
(case unit |
| 76 |
[(h) (make-duration 0 (* n 3600))] |
| 77 |
[(m) (make-duration 0 (* n 60))] |
| 78 |
[(s) (make-duration 0 n)] |
| 79 |
[(ms) (make-duration (* n #e1e6) 0)] |
| 80 |
[(us) (make-duration (* n #e1e3) 0)] |
| 81 |
[(ns) (make-duration n 0)] |
| 82 |
[else (error "Unknown time unit. Either one of (h m s ms us ns) \ |
| 83 |
expected, but got:" unit)])) |
| 84 |
|
| 85 |
(define (timer-delta? obj) |
| 86 |
(and (is-a? obj <time>) |
| 87 |
(eq? (time-type obj) time-duration))) |