On this page:
16.1 Introduction
16.2 Ticker Queue
make-tiqueue
tiqueue?
tiqueue-send!
tiqueue-event
tiqueue-ready?
tiqueue-recv!
16.3 Ticker Environment Core
current-ticker-yield
current-ticker-procedure
current-ticker-spawner
current-ticker-clock
16.4 Ticker Environment
within-ticker?
ticker-yield
ticker-timestamp
ticker-spawn
ticker-procedure
16.5 Ticker Scheduler
run-ticker
8.10

16 RRLL: Ticker

Dominik Pantůček <dominik.pantucek@trustica.cz>

Racket Rogue-Like Library: Ticker

Continuations-based cooperative scheduler.

16.1 Introduction

The ticker environment provides the facilities required for cooperative multitasking within single racket thread. It contains the runtime environment run-ticker, messaging capability using tiqueue? that works both within the environment and also allows messaging from and to this environment and environment access module rrll/ticker/env that must be used by the scheduled procedures to coordinate evaluation.

16.2 Ticker Queue

 (require rrll/ticker/queue) package: rrll-ticker

Special queue that can be used as communication channel between procedures scheduled by run-ticker as well as from outside of this environment into it and vice-versa.

There should always be only one consumer of the queue that receives the messages. There may be many producers that send messages to given queue.

procedure

(make-tiqueue)  tiqueue?

Creates a new empty ticker queue.

procedure

(tiqueue? v)  boolean?

  v : any/c
Returns #t if given argument v is an instance of the ticker queue.

procedure

(tiqueue-send! q v)  void?

  q : tiqueue?
  v : any?
Enqueues given value in the queue and increments its internal counter. If the queue was empty, its event becomes ready for synchronization.

procedure

(tiqueue-event q)  evt?

  q : tiqueue?
Returns an event that is ready for synchronization when given queue is not empty.

procedure

(tiqueue-ready? q)  boolean?

  q : tiqueue?
Returns #t if there is at least one element in the queue.

procedure

(tiqueue-recv! q)  any?

  q : tiqueue?
If there is nothing in the queue, does not return until something becomes available.

When run under run-ticker environment, does not actually block the current thread but rather yields with (tiqueue-event q) which ensures the evaluation is resumed when the queue becomes ready. Outside of such environment it just blocks the current thread.

Removes the next element from the queue and returns it.

16.3 Ticker Environment Core

 (require rrll/ticker/private/env) package: rrll-ticker

This module contains the parameter?s used by the ticker environment. All of these must not be used directly by other modules as they are used internally by the ticker environment. All of these are used by the scheduled procedures through procedures defined in rrll/ticker/env.

parameter

(current-ticker-yield)

  (or/c #f (-> (or/c real? (listof evt?)) void?))
(current-ticker-yield yield)  void?
  yield : (or/c #f (-> (or/c real? (listof evt?)) void?))
 = #f
All the procedures scheduled by the ticker runtime environment use this parameter? to suspend its evaluation and yield to other scheduled procedures.

Outside of the ticker runtime environment, its value is #f.

Inside the environment this value is procedure? accepting either a number of milliseconds to wait or a list of events. In the former case the evaluation is suspened for at least given number of milliseconds - although it may be longer. In the latter case the evaluation of current procedure is suspended until at least one of the events becomes ready for synchronization.

parameter

(current-ticker-procedure)  (or/c #f (-> void?))

(current-ticker-procedure proc)  void?
  proc : (or/c #f (-> void?))
 = #f
Inside any scheduled procedure this parameter? holds a unique value representing this procedure as it is scheduled by the ticker environment.

This value can be used for identifying the current procedure and the same value is passed to optional #:cleanup argument of the ticker-run procedure.

Outside of the environment its value is #f.

parameter

(current-ticker-spawner)  (or/c #f (-> (-> void?) void?))

(current-ticker-spawner proc)  void?
  proc : (or/c #f (-> (-> void?) void?))
 = #f
For all the procedures scheduled within the ticker environment, this parameter? holds a procedure that can add new procedures to be scheduled. Their evaluation starts immediately when next scheduling iteration begins.

Outside the environment it is #f.

parameter

(current-ticker-clock)  (or/c #f (-> real?))

(current-ticker-clock proc)  void?
  proc : (or/c #f (-> real?))
 = #f
This parameter? holds the procedure that can report the current internal timestamp derived from (current-inexact-milliseconds) and it can be used for timestamping.

Outside of the environment it contains #f.

16.4 Ticker Environment

 (require rrll/ticker/env) package: rrll-ticker

This module provides procedures to use this environment from within scheduled procedures.

procedure

(within-ticker?)  boolean?

Returns #t if called from within the with-ticker-yield environment - that is from a procedure that is currently scheduled under run-ticker.

procedure

(ticker-yield duration)  void?

  duration : real?
Yields to other ticked procedures. The duration can either be a number or a list of events. The evaluation resumes if the duration time has passed or any of the events become ready for synchronization.

Returns the current internal timestamp in milliseconds of the run-ticker instance running the caller.

procedure

(ticker-spawn proc)  procedure?

  proc : procedure?
Wraps given procedure and immediately spawns it within the current run-ticker environment. Returns the wrapped procedure.

procedure

(ticker-procedure)  procedure?

Returns the currently scheduled procedure. It must not be called, but it may be used as key for registering resources to be cleaned up. See run-ticker.

16.5 Ticker Scheduler

 (require rrll/ticker) package: rrll-ticker

The actual scheduler implementation.

procedure

(run-ticker [#:initial-wait initial-wait    
  #:cleanup cleanup]    
  init-proc ...)  procedure?
  initial-wait : boolean? = #f
  cleanup : boolean? = #f
  init-proc : procedure?
Creates and runs a new ticker scheduler in separate thread. Controlling procedure is returned.

If initial-wait is #t and no init-procs are given, the ticker thread waits for the first procedure to be started.

The cleanup must be #f or a procedure? accepting a single argument that will be called whenever a scheduled procedure finishes. This can be used for implementing cleanup of resources created by the scheduled procedure. See ticker-this.