Title

Timer APIs

Author

Takashi Kato

Status

This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-120@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

Abstract

This SRFI defines interfaces to handle timer processes.

Issues

There is no issue.

Rationale

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.

Specification

(make-timer [error-handler])

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.

(timer? obj)

Returns #t if given obj is a timer object, otherwise #f.

(timer-cancel! 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 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 task 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.

(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.

It is an error if the given task id is not associated with the given timer or if the task is already executed and not scheduled anymore.

(timer-task-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-task-exists? timer id)

Returns #t if a task associated to the given id exists, otherwise #f.

(make-timer-delta n unit)

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:

And may support other unit.

(timer-delta? obj)

Returns #t if given obj is a timer delta object, otherwise #f.

Implementation

The sample implementation of this SRFI depends on the following SRFIs:

It is written in R7RS library system, the name is (timer).

Copyright

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.


Editor: Michael Sperber