Documentation
¶
Overview ¶
Package evtm manages the scheduling and execution of events. It depends on the package evtq for implementing the event list, and the package vrtime for implementing virtual time. mutexes are used to support concurrent access to an EventManager by multiple goroutines
Index ¶
- type Event
- type EventHandlerFunction
- type EventManager
- func (evtmgr *EventManager) CurrentSeconds() float64
- func (evtmgr *EventManager) CurrentTicks() int64
- func (evtmgr *EventManager) CurrentTime() vrtime.Time
- func (evtmgr *EventManager) RemoveEvent(eventID int) bool
- func (evtmgr *EventManager) Run(LimitTime float64)
- func (evtmgr *EventManager) Schedule(context any, data any, handler func(*EventManager, any, any) any, ...) (int, vrtime.Time)
- func (evtmgr *EventManager) SetExternal(external bool)
- func (evtmgr *EventManager) SetTime(new_time vrtime.Time)
- func (evtmgr *EventManager) SetWallclock(wallclock bool)
- func (evtmgr *EventManager) Stop()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event struct {
// Context is opaque information the event handler may need about where
// and what it is executing. The code that schedules the event
// and the code that handles the event have to be using the same
// type.
Context any
// Data is information the event handler uses to execute the event,
// e.g., a message or frame. Again, the code that schedules
// the event and the code that executes it have to agree on the format,
// because Go type checking won't do that.
Data any
// Time defines the instant (in the future) when the EventHandler
// will be called.
Time vrtime.Time
// EventHandlerFunction is the function that is called to handle
// the event.
// These all have the signature func(context any, data any) bool
EventHandler EventHandlerFunction
// EventID is a permanent identifier for this event. It can
// be used to subsequently access or delete the event.
// It is permanantly assigned when the event is scheduled.
EventID int
}
Event packages up the context and data sensitive information to be included when scheduling an event, and is used in dispatching the event handler
type EventHandlerFunction ¶
type EventHandlerFunction func(*EventManager, any, any) any
EventHandlerFunction is invoked when the corresponding event fires
type EventManager ¶
type EventManager struct {
EventList *evtq.EventQueue // order events
Time vrtime.Time // time of last event pulled off the EventList (but not necessarily yet executed completely)
EventID int // identifier needed if we aim to remove events from EventList
RunFlag bool // indicate whether the EventManager is actively in use right now
Wallclock bool // scale virtual time advance to wallclock time, approximately
StartTime time.Time // wallclock time at time of first event
External bool // if true we don't close up when the event list is empy
// contains filtered or unexported fields
}
An EventManager structure holds information needed to schedule and execute events. It has a pointer to an EventQueue (defined in package evtq), and a copy of the virtual time of the last event to have been removed from the EventQueue (which is interpreted as the virtual time of that event). It has a Boolean flag also which, if changed to false when processing an event, will inhibit the dispatch of further events until the event manager is told to run again.
func New ¶
func New() *EventManager
New creates an empty event queue, sets the virtual time to zero, initializes the 'running' flag to false.
func (*EventManager) CurrentSeconds ¶
func (evtmgr *EventManager) CurrentSeconds() float64
CurrentSeconds gives the time using the seconds units
func (*EventManager) CurrentTicks ¶
func (evtmgr *EventManager) CurrentTicks() int64
CurrentTicks returns the number of ticks since the EventManager started executing events, at tick 0
func (*EventManager) CurrentTime ¶
func (evtmgr *EventManager) CurrentTime() vrtime.Time
CurrentTime returns a copy of the simulation's current time.
func (*EventManager) RemoveEvent ¶
func (evtmgr *EventManager) RemoveEvent(eventID int) bool
RemoveEvent removes the indicated event from the event list, and returns a flag indicating whether the event was found and removed
func (*EventManager) Run ¶
func (evtmgr *EventManager) Run(LimitTime float64)
function Run(LimitTime) starts the event dispatch loop for an EventManager that has been inactive. It will stay in this processing loop until (a) there are events in queue, but none with timestamps greater than LimitTime, (b) there are no events in queue, or (c) the last event executed set the Event Manager's RunFlag to false. In case (a) the clock of the Event Manager is set to LimitTime, in cases (b) and (c) the clock is left at the time of the last event executed.
func (*EventManager) Schedule ¶
func (evtmgr *EventManager) Schedule(context any, data any, handler func(*EventManager, any, any) any, offset vrtime.Time) (int, vrtime.Time)
Schedule creates a new event and puts it on the EventManager's event queue. The call to Schedule passes all the parameters needed to create that event
- event handler function
- context where event is executed
- data used by the event execution
- how far into the virtual time future the event takes place.
Schedule returns the eventId of the new event (a handle that can be used to cancel it) and the virtual time when the execution will occur.
func (*EventManager) SetExternal ¶
func (evtmgr *EventManager) SetExternal(external bool)
SetExternal sets the flag assigns a value to the flag which when true puts the EventManager into a mode where if the event list empties before reaching the end simulation time, the thread running the EventManager suspends until the scheduling (by a different thread) of an event on the EventManager releases it.
func (*EventManager) SetTime ¶
func (evtmgr *EventManager) SetTime(new_time vrtime.Time)
SetTime sets the Event Manager's clock to a specified vrtime
func (*EventManager) SetWallclock ¶
func (evtmgr *EventManager) SetWallclock(wallclock bool)
SetWallclock assigns a value to the flag which when true puts the EventManager into a model where it runs in tandem with wallclock time
func (*EventManager) Stop ¶
func (evtmgr *EventManager) Stop()
Stop stops the event dispatch loop of the EventManager.