ssm
Runtime Library for the Sparse Synchronous Model
|
#include "ssm.h"
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_t * | event_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_t * | act_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... | |
#define SSM_ACT_QUEUE_SIZE 1024 |
Size of the activation record queue; override as necessary.
#define SSM_EVENT_QUEUE_SIZE 2048 |
Size of the event queue; override as necessary.
#define SSM_QUEUE_HEAD 1 |
Extra entries at the start of a binary heap.
#define SSM_STATIC static |
If defined, makes normally hidden internal functions and variables available for linking, allowing whitebox testing.
#define SSM_STATIC_INLINE static inline |
typedef uint16_t q_idx_t |
Queue index type; index of 1 is the first element.
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.
hole | Where to place the given activation record |
act | Activation record to be placed in the queue |
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.
hole | Index of the hole that needs to be filled |
act | Activation record that needs to be placed in the heap |
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.
hole | Where to place the given event |
event | Event to place in the queue |
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.
hole | Index of the hole, from 1 to event_queue_len, inclusive |
var | Event to place in the queue. Its later_time field should be set to its proper value. |
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.
var | must be non-NULL |
SSM_STATIC ssm_act_t* act_queue[SSM_ACT_QUEUE_SIZE+SSM_QUEUE_HEAD] |
ctivation record queue, used to track and schedule continuations at each instant.
Managed as a binary heap sorted by a->priority
SSM_STATIC q_idx_t act_queue_len = 0 |
SSM_STATIC ssm_sv_t* event_queue[SSM_EVENT_QUEUE_SIZE+SSM_QUEUE_HEAD] |
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
SSM_STATIC q_idx_t event_queue_len = 0 |
SSM_STATIC ssm_time_t now = 0L |
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.