Implementing timer requires thread mechanism, and this feature is not always supported in scheme. All I now know is SRFI-120 provide timer based on SRFI-18, which is an implementation of POSIX thread model. However, SRFI-18 released on 2001. Although Chez Scheme also implements POSIX thread, it opened source much latter, and never claims to be consistent with SRFI-18. This makes SRFI-120 not portable to Chez Scheme.
This repository is a timer implementation based on Chez Scheme's thread mechanism. And before importing this repository, please firstly (import (chezscheme))
.
Chez Scheme and AKKU. In addition, though Chez Scheme claims its thread mechanism portable to Windows, I've not done any tests on this point.
(import (ufo-timer))
to use timer.
(let ([timer
(init-timer
(make-time 'time-duration 0 5)
(lambda () (pretty-print 'test)))])
(start-timer timer))
NOTE: (make-time 'time-duration 0 5) is to make Chez Scheme's time object.
(let ([timer
(init-interval-timer
(make-time 'time-duration 0 5)
(lambda () (pretty-print 'test)))])
(start-timer timer))
This repository is based on ufo-thread-pool, and users can also assign a pool to timers like following:
(init-timer timeout todo pool)
(init-interval-timer timeout todo stop pool)
NOTE: you need to additionally
(import (ufo-thread-pool))
.
For more related description, please refer to other cases in this text.
As shown above, interval timer have a stop
parameter which is default (lambda () #t)
and such interval timer will never stop. If you want it to react to some condition, such as stopping when reach 3 times, following closure will help a lot:
(let* ([i 0]
[timer
(init-interval-timer
(make-time 'time-duration 0 5)
(lambda ()
(set! i (+ 1 i))
(pretty-print 'test))
(lambda () (< i 3)))])
(start-timer timer))
When users do some socket programming, it's very often to set expire time. Or in other words, if a socket is inactive for server seconds, it will be closed by a timer.
(let ([timer
(init-interval-timer
(make-time 'time-duration 0 5)
(lambda () (pretty-print 'try-to-close-socket)))])
(start-timer timer)
(with-mutex (timer-mutex timer)
;Here's usually a loop body
;during the body, you can reset the timer to initial state by following
(condition-broadcast (timer-condition timer))))