Timer APIs
Takashi Kato
This SRFI defines interfaces to handle timer processes.
There is no issue.
Timer is one of the important features to implement a practical program. It is trivial to implement if implementations support thread. However not all Scheme implementations support SRFI-18 nor POSIX thread model. Some of the implementations even don't expose mutex. So it is nice to have the interface to handle timer to hide underlying implementation.
(make-timer [error-handler])
Creates a timer object. The optional argument error-handler must be a
procedure which accepts one argument. If it is given and when a timer
task raises an error, then the handler will be invoked and timer will
continue if the error-handler wouldn't raise an error. Otherwise
whenever an error is raised, timer stops and preserves the error.
The error is raised when timer-stop!
procedure is called.
(timer? obj)
Returns #t
if given obj is a timer object, otherwise #f
.
(timer-start! timer)
Starts the given timer. Timer won't do any task unless it's started. If one or more tasks are scheduled before the timer is started and the scheduled time is passed, then timer invokes the tasks.
(timer-stop! timer)
Stops the given timer. The procedure raises the preserved error if there is. Once a timer is stopped, it will never be able to start again.
(timer-schedule! timer thunk when [period])
Schedules the given thunk as the given timer's task. The when argument specifies when the task will be started. It can be either time object or non negative integer. If the when is a time object, then the task is scheduled on that time. If the when is an integer, then the task is scheduled on passed number milliseconds later.
If the optional argument period is given, which must be either a time
object which type is time-duration
or an integer, then the given task
is scheduled as periodical task. If period is a time object, then the
duration is simply added to calculated when after the execution. If it
is an integer, then it is interpreted as milliseconds and added to calculated
when as well.
The executing order of the same timing tasks are not defined.
The procedure returns timer id which is an integer.
(timer-reschedule! timer id when [period])
Reschedules the task associated to the given id on the given timer.
The when and period arguments are the same as timer-schedule!
.
Thus to cancel the periodical task, you can specify 0 as period argument.
The procedure returns given id.
(timer-remove! timer id)
Removes the task associated to the given id on the given timer. It
returns #t
if a task is removed, otherwise #f
.
(timer-exists? timer id)
Returns #t
if a task associated to the given id exists, otherwise #f
.
The sample implementation of this SRFI depends on the following SRFIs:
It is written in R7RS library system, the name is (timer)
.
Copyright (C) Takashi Kato (2015). All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.