16 RRLL: Ticker
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?
procedure
(tiqueue-send! q v) → void?
q : tiqueue? v : any?
procedure
(tiqueue-event q) → evt?
q : tiqueue?
procedure
(tiqueue-ready? q) → boolean?
q : tiqueue?
procedure
(tiqueue-recv! q) → any?
q : tiqueue?
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
→ (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
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
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
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
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
procedure
(ticker-yield duration) → void?
duration : real?
procedure
procedure
(ticker-spawn proc) → procedure?
proc : procedure?
procedure
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?
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.