ssm
Runtime Library for the Sparse Synchronous Model
Macros | Typedefs | Functions | Variables
ssm-scheduler.c File Reference
#include "ssm.h"
Include dependency graph for ssm-scheduler.c:

Macros

#define SSM_STATIC   static
 If defined, makes normally hidden internal functions and variables available for linking, allowing whitebox testing. More...
 
#define SSM_STATIC_INLINE   static inline
 
#define SSM_EVENT_QUEUE_SIZE   2048
 Size of the event queue; override as necessary. More...
 
#define SSM_ACT_QUEUE_SIZE   1024
 Size of the activation record queue; override as necessary. More...
 
#define SSM_QUEUE_HEAD   1
 Extra entries at the start of a binary heap. More...
 

Typedefs

typedef uint16_t q_idx_t
 Queue index type; index of 1 is the first element. More...
 

Functions

void ssm_reset ()
 Reset the scheduler. More...
 
bool ssm_event_on (ssm_sv_t *var)
 Return true if there is an event on the given variable in the current instant. More...
 
void ssm_sensitize (ssm_sv_t *var, ssm_trigger_t *trigger)
 Indicate writing to a variable should trigger a routine. More...
 
void ssm_desensitize (ssm_trigger_t *trigger)
 Disable a sensitized routine. More...
 
void ssm_trigger (ssm_sv_t *var, ssm_priority_t priority)
 Activate routines triggered by a variable. More...
 
SSM_STATIC_INLINE void act_queue_percolate_up (q_idx_t hole, ssm_act_t *act)
 Starting at the hole, walk up toward the root of the tree, copying parent to child until we find where we can put the new activation record. More...
 
SSM_STATIC_INLINE void act_queue_percolate_down (q_idx_t hole, ssm_act_t *act)
 Starting at the hole, walk down towards the leaves of the tree, moving the earlier child to the parent and repeating the process on that child. More...
 
void ssm_activate (ssm_act_t *act)
 Schedule a routine to run in the current instant. More...
 
ssm_time_t ssm_next_event_time ()
 Return the time of the next event in the queue or SSM_NEVER. More...
 
ssm_time_t ssm_now ()
 Return the current model time. More...
 
SSM_STATIC_INLINE q_idx_t find_queued_event (ssm_sv_t *var)
 Determine the index of an already scheduled event. More...
 
void ssm_initialize (ssm_sv_t *var, void(*update)(ssm_sv_t *))
 Initialize a scheduled variable. More...
 
SSM_STATIC_INLINE void event_queue_percolate_up (q_idx_t hole, ssm_sv_t *var)
 Starting at the hole, walk up toward the root of the tree, copying parent to child until we find where we can put the new event. More...
 
SSM_STATIC_INLINE void event_queue_percolate_down (q_idx_t hole, ssm_sv_t *event)
 Starting at the hole, walk down towards the leaves of the tree, moving the earlier child to the parent and repeating the process on that child. More...
 
void ssm_schedule (ssm_sv_t *var, ssm_time_t later)
 Schedule a future update to a variable. More...
 
void ssm_unschedule (ssm_sv_t *var)
 Unschedule any pending event on a variable. More...
 
void ssm_tick ()
 Run the system for the next scheduled instant. More...
 

Variables

SSM_STATIC ssm_sv_tevent_queue [SSM_EVENT_QUEUE_SIZE+SSM_QUEUE_HEAD]
 Event queue, used to track and schedule events between instants. More...
 
SSM_STATIC q_idx_t event_queue_len = 0
 
SSM_STATIC ssm_act_tact_queue [SSM_ACT_QUEUE_SIZE+SSM_QUEUE_HEAD]
 ctivation record queue, used to track and schedule continuations at each instant. More...
 
SSM_STATIC q_idx_t act_queue_len = 0
 
SSM_STATIC ssm_time_t now = 0L
 The current model time. More...
 

Macro Definition Documentation

◆ SSM_ACT_QUEUE_SIZE

#define SSM_ACT_QUEUE_SIZE   1024

Size of the activation record queue; override as necessary.

◆ SSM_EVENT_QUEUE_SIZE

#define SSM_EVENT_QUEUE_SIZE   2048

Size of the event queue; override as necessary.

◆ SSM_QUEUE_HEAD

#define SSM_QUEUE_HEAD   1

Extra entries at the start of a binary heap.

◆ SSM_STATIC

#define SSM_STATIC   static

If defined, makes normally hidden internal functions and variables available for linking, allowing whitebox testing.

◆ SSM_STATIC_INLINE

#define SSM_STATIC_INLINE   static inline

Typedef Documentation

◆ q_idx_t

typedef uint16_t q_idx_t

Queue index type; index of 1 is the first element.

Function Documentation

◆ act_queue_percolate_down()

SSM_STATIC_INLINE void act_queue_percolate_down ( q_idx_t  hole,
ssm_act_t act 
)

Starting at the hole, walk down towards the leaves of the tree, moving the earlier child to the parent and repeating the process on that child.

This makes the parent earlier than both children. Stop when the activation record we're trying to place has a lower priority than either children.

Parameters
holeWhere to place the given activation record
actActivation record to be placed in the queue

◆ act_queue_percolate_up()

SSM_STATIC_INLINE void act_queue_percolate_up ( q_idx_t  hole,
ssm_act_t act 
)

Starting at the hole, walk up toward the root of the tree, copying parent to child until we find where we can put the new activation record.

Parameters
holeIndex of the hole that needs to be filled
actActivation record that needs to be placed in the heap

◆ event_queue_percolate_down()

SSM_STATIC_INLINE void event_queue_percolate_down ( q_idx_t  hole,
ssm_sv_t event 
)

Starting at the hole, walk down towards the leaves of the tree, moving the earlier child to the parent and repeating the process on that child.

This makes the parent earlier than both children. Stop when the event we're trying to place is earlier than both of the children.

Parameters
holeWhere to place the given event
eventEvent to place in the queue

◆ event_queue_percolate_up()

SSM_STATIC_INLINE void event_queue_percolate_up ( q_idx_t  hole,
ssm_sv_t var 
)

Starting at the hole, walk up toward the root of the tree, copying parent to child until we find where we can put the new event.

Parameters
holeIndex of the hole, from 1 to event_queue_len, inclusive
varEvent to place in the queue. Its later_time field should be set to its proper value.

◆ find_queued_event()

SSM_STATIC_INLINE q_idx_t find_queued_event ( ssm_sv_t var)

Determine the index of an already scheduled event.

This is an inefficient linear search, so it's not meant to be used often. This is in preparation for either removing or rescheduling an event in the queue. The function should only be called on a variable that is in the queue.

Parameters
varmust be non-NULL

Variable Documentation

◆ act_queue

ctivation record queue, used to track and schedule continuations at each instant.

Managed as a binary heap sorted by a->priority

◆ act_queue_len

SSM_STATIC q_idx_t act_queue_len = 0

◆ event_queue

Event queue, used to track and schedule events between instants.

Managed as a binary heap sorted by later_time

Since the first element (QUEUE_HEAD) is unusued, event_queue[event_queue_len] is the last element in the queue provided event_queue_len is non-zero

◆ event_queue_len

SSM_STATIC q_idx_t event_queue_len = 0

◆ now

The current model time.

Read with ssm_now(); user programs should not manipuate this directly.

This starts out initialized to 0; can be reset by ssm_reset(). ssm_tick() advances it monotonically.