Module time

Alarm queues allow scheduling items. Items are Prolog terms and are copied. An alarm queue can be created by the predicate alarm_new/1. An alarm queue need not be explicitly destroyed, it will automatically be reclaimed by the Java GC when not anymore used. Threads waiting for an alarm queue can be interrupted.

Example:
?- time_out((repeat, write('Hello World!'), nl,
thread_sleep(1000), fail), 3000).
Hello World!
Hello World!
Hello World!
Error: Execution aborted since time limit exceeded.
thread_sleep/1
time_out/2

An item can be scheduled with the predicate alarm_schedue/4 giving a delay in milliseconds. The predicate alarm_next/2 allows getting an item from a queue. The predicate will block for the earliest item. The predicate alarm_cancel/2 will remove an item from the queue.

The predicate time_out/2 uses a predefined alarm queue which is served by a predefined thread. The predicate executes the given goal once in the given timeout. When the timeout is reached before the goal completes an exception is thrown.

The following time predicates are provided:
alarm_new(A):
The predicate succeeds for a new alarm queue A.
alarm_schedule(A, O, T, E):
The predicate succeeds for a new alarm entry E that schedules a copy of the term O on the alarm queue A with a delay of T.
alarm_next(A, O):
The predicate succeeds for the next term O on the alarm queue A. The predicate blocks for the earliest item.
alarm_cancel(A, E):
The predicate succeeds for cancelling the alarm entry E from the alarm queue A.
time_out(G, T):
The predicate succeeds when G succeeds in the timeout T. The predicate fails when G fails in the timeout T. Otherwise the predicate throws the message system_error(timelimit_exceeded).

Comments