metronome-0.1: Time Synchronized execution.

Portabilitynot portable (requires STM)
Stabilityunstable
Maintainerpaolo.veronelli@gmail.com
Safe HaskellNone

System.Metronome

Contents

Description

Synchronized execution of sequences of actions, controlled in STM

All data structures are made accessible via Data.Lens abstraction.

Actions to be executed are of type Action = STM (IO ()). At each tick, the scheduled actions are ordered by priority, binded as STM actions ignoring the retrying ones. The results, being IO actions are executed in that order.

Every Track and Metronome lives in its own thread and can be stopped or killed as such, setting a flag in its state.

Track and metronome state are exposed in TVar value to be modified at will. The only closed and inaccessible value is the synchronizing channel, written by the metronome and waited by tracks. The TrackForker returned by a metronome function is closing this channel and it's the only way to fork a track.

See System.Metronome.Practical for an simple wrapper around this module.

Synopsis

Data structures

data Track Source

State of a track.

Constructors

Track 

Fields

_sync :: Ticks

the number of ticks elapsed from the track fork

_frequency :: Frequency

calling frequency relative to metronome ticks frequency

_actions :: [Action]

the actions left to be run

_priority :: Priority

priority of this track among its peers

_muted :: Bool

muted flag, when True, actions are not scheduled, just skipped

data Thread a Source

supporting values with running and alive flag

Constructors

Thread 

Fields

_running :: Bool

stopped or running flag

_alive :: Bool

set to false to require kill thread

_core :: a

core data

data Metronome Source

State of a metronome

Constructors

Metronome 

Fields

_ticks :: [MTime]

next ticking times

_schedule :: [(Priority, Action)]

actions scheduled for the tick to come

Lenses

running :: forall a. Lens (Thread a) BoolSource

alive :: forall a. Lens (Thread a) BoolSource

core :: forall a. Lens (Thread a) aSource

Synonyms

type Control a = TVar (Thread a)Source

A Thread value cell in STM

type Priority = DoubleSource

Priority values between tracks under the same metronome.

type Frequency = IntegerSource

Number of metronome ticks between two track ticks

type Ticks = IntegerSource

Number of elapsed ticks

type Action = STM (IO ())Source

Track effect interface. Write in STM the collective and spit out the IO action to be executed when all STMs for this tick are done or retried

type MTime = DoubleSource

Time, in seconds

type TrackForker = Control Track -> IO ()Source

The action to fork a new track from a track state.

API

metronomeSource

Arguments

:: Control Metronome

initial state

-> IO TrackForker 

Fork a metronome from its initial state