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.
Creates and starts 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-cancel!
procedure is called.
Two timers should run in separate context, means whenever timer A is executing a task, timer B should not be disturbed executing a task by timer A's execution.
Returns #t
if given obj is a timer object, otherwise #f
.
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.
Schedules the given thunk as the given timer's task. The when argument specifies when the task will be started. It can be either timer delta object or non negative integer. The task is scheduled on the time when the given when passed from the procedure is called. The task is executed on the dynamic environment where the timer is created.
If the optional argument period is given, which must be either timer delta object or an integer, then the given task is scheduled as periodical task. The next task is scheduled by adding when and period. If the period or when is an integer, then it is interpreted as milliseconds.
The procedure returns timer id which is a readable datum such as an integer.
The executing order of the same timing tasks are not defined.
A task should be able to cancel or reschedule other tasks. But it should not be able to cancel or reschedule itself.
If a task is rescheduled whenever it's executed, the timer doesn't stop its execution. It is rescheduled but the current execution will be continued.
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.
Removes the task associated to the given id on the given timer. It
returns #t
if a task is removed, otherwise #f
.
Returns #t
if a task associated to the given id exists, otherwise #f
.
Creates a timer delta object. n must be an integer and unit must be a symbol which represents the time unit. Implementations must support the following units:
h
: hourm
: minutes
: secondms
: millisecondus
: microsecondns
: nanosecondAnd may support other unit.
Returns #t
if given obj is a timer delta object, 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.